17330f729Sjoerg //===- ASTStructuralEquivalence.cpp ---------------------------------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file implement StructuralEquivalenceContext class and helper functions
107330f729Sjoerg // for layout matching.
117330f729Sjoerg //
127330f729Sjoerg // The structural equivalence check could have been implemented as a parallel
137330f729Sjoerg // BFS on a pair of graphs. That must have been the original approach at the
147330f729Sjoerg // beginning.
157330f729Sjoerg // Let's consider this simple BFS algorithm from the `s` source:
167330f729Sjoerg // ```
177330f729Sjoerg // void bfs(Graph G, int s)
187330f729Sjoerg // {
197330f729Sjoerg // Queue<Integer> queue = new Queue<Integer>();
207330f729Sjoerg // marked[s] = true; // Mark the source
217330f729Sjoerg // queue.enqueue(s); // and put it on the queue.
227330f729Sjoerg // while (!q.isEmpty()) {
237330f729Sjoerg // int v = queue.dequeue(); // Remove next vertex from the queue.
247330f729Sjoerg // for (int w : G.adj(v))
257330f729Sjoerg // if (!marked[w]) // For every unmarked adjacent vertex,
267330f729Sjoerg // {
277330f729Sjoerg // marked[w] = true;
287330f729Sjoerg // queue.enqueue(w);
297330f729Sjoerg // }
307330f729Sjoerg // }
317330f729Sjoerg // }
327330f729Sjoerg // ```
337330f729Sjoerg // Indeed, it has it's queue, which holds pairs of nodes, one from each graph,
34*e038c9c4Sjoerg // this is the `DeclsToCheck` member. `VisitedDecls` plays the role of the
35*e038c9c4Sjoerg // marking (`marked`) functionality above, we use it to check whether we've
36*e038c9c4Sjoerg // already seen a pair of nodes.
377330f729Sjoerg //
387330f729Sjoerg // We put in the elements into the queue only in the toplevel decl check
397330f729Sjoerg // function:
407330f729Sjoerg // ```
417330f729Sjoerg // static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
427330f729Sjoerg // Decl *D1, Decl *D2);
437330f729Sjoerg // ```
447330f729Sjoerg // The `while` loop where we iterate over the children is implemented in
457330f729Sjoerg // `Finish()`. And `Finish` is called only from the two **member** functions
467330f729Sjoerg // which check the equivalency of two Decls or two Types. ASTImporter (and
477330f729Sjoerg // other clients) call only these functions.
487330f729Sjoerg //
497330f729Sjoerg // The `static` implementation functions are called from `Finish`, these push
507330f729Sjoerg // the children nodes to the queue via `static bool
517330f729Sjoerg // IsStructurallyEquivalent(StructuralEquivalenceContext &Context, Decl *D1,
527330f729Sjoerg // Decl *D2)`. So far so good, this is almost like the BFS. However, if we
537330f729Sjoerg // let a static implementation function to call `Finish` via another **member**
547330f729Sjoerg // function that means we end up with two nested while loops each of them
557330f729Sjoerg // working on the same queue. This is wrong and nobody can reason about it's
567330f729Sjoerg // doing. Thus, static implementation functions must not call the **member**
577330f729Sjoerg // functions.
587330f729Sjoerg //
597330f729Sjoerg //===----------------------------------------------------------------------===//
607330f729Sjoerg
617330f729Sjoerg #include "clang/AST/ASTStructuralEquivalence.h"
627330f729Sjoerg #include "clang/AST/ASTContext.h"
637330f729Sjoerg #include "clang/AST/ASTDiagnostic.h"
647330f729Sjoerg #include "clang/AST/Decl.h"
657330f729Sjoerg #include "clang/AST/DeclBase.h"
667330f729Sjoerg #include "clang/AST/DeclCXX.h"
677330f729Sjoerg #include "clang/AST/DeclFriend.h"
687330f729Sjoerg #include "clang/AST/DeclObjC.h"
69*e038c9c4Sjoerg #include "clang/AST/DeclOpenMP.h"
707330f729Sjoerg #include "clang/AST/DeclTemplate.h"
717330f729Sjoerg #include "clang/AST/ExprCXX.h"
72*e038c9c4Sjoerg #include "clang/AST/ExprConcepts.h"
73*e038c9c4Sjoerg #include "clang/AST/ExprObjC.h"
74*e038c9c4Sjoerg #include "clang/AST/ExprOpenMP.h"
757330f729Sjoerg #include "clang/AST/NestedNameSpecifier.h"
76*e038c9c4Sjoerg #include "clang/AST/StmtObjC.h"
77*e038c9c4Sjoerg #include "clang/AST/StmtOpenMP.h"
787330f729Sjoerg #include "clang/AST/TemplateBase.h"
797330f729Sjoerg #include "clang/AST/TemplateName.h"
807330f729Sjoerg #include "clang/AST/Type.h"
817330f729Sjoerg #include "clang/Basic/ExceptionSpecificationType.h"
827330f729Sjoerg #include "clang/Basic/IdentifierTable.h"
837330f729Sjoerg #include "clang/Basic/LLVM.h"
847330f729Sjoerg #include "clang/Basic/SourceLocation.h"
857330f729Sjoerg #include "llvm/ADT/APInt.h"
867330f729Sjoerg #include "llvm/ADT/APSInt.h"
877330f729Sjoerg #include "llvm/ADT/None.h"
887330f729Sjoerg #include "llvm/ADT/Optional.h"
897330f729Sjoerg #include "llvm/Support/Casting.h"
907330f729Sjoerg #include "llvm/Support/Compiler.h"
917330f729Sjoerg #include "llvm/Support/ErrorHandling.h"
927330f729Sjoerg #include <cassert>
937330f729Sjoerg #include <utility>
947330f729Sjoerg
957330f729Sjoerg using namespace clang;
967330f729Sjoerg
977330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
987330f729Sjoerg QualType T1, QualType T2);
997330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1007330f729Sjoerg Decl *D1, Decl *D2);
1017330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1027330f729Sjoerg const TemplateArgument &Arg1,
1037330f729Sjoerg const TemplateArgument &Arg2);
1047330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1057330f729Sjoerg NestedNameSpecifier *NNS1,
1067330f729Sjoerg NestedNameSpecifier *NNS2);
1077330f729Sjoerg static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
1087330f729Sjoerg const IdentifierInfo *Name2);
1097330f729Sjoerg
IsStructurallyEquivalent(StructuralEquivalenceContext & Context,const DeclarationName Name1,const DeclarationName Name2)1107330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1117330f729Sjoerg const DeclarationName Name1,
1127330f729Sjoerg const DeclarationName Name2) {
1137330f729Sjoerg if (Name1.getNameKind() != Name2.getNameKind())
1147330f729Sjoerg return false;
1157330f729Sjoerg
1167330f729Sjoerg switch (Name1.getNameKind()) {
1177330f729Sjoerg
1187330f729Sjoerg case DeclarationName::Identifier:
1197330f729Sjoerg return IsStructurallyEquivalent(Name1.getAsIdentifierInfo(),
1207330f729Sjoerg Name2.getAsIdentifierInfo());
1217330f729Sjoerg
1227330f729Sjoerg case DeclarationName::CXXConstructorName:
1237330f729Sjoerg case DeclarationName::CXXDestructorName:
1247330f729Sjoerg case DeclarationName::CXXConversionFunctionName:
1257330f729Sjoerg return IsStructurallyEquivalent(Context, Name1.getCXXNameType(),
1267330f729Sjoerg Name2.getCXXNameType());
1277330f729Sjoerg
1287330f729Sjoerg case DeclarationName::CXXDeductionGuideName: {
1297330f729Sjoerg if (!IsStructurallyEquivalent(
1307330f729Sjoerg Context, Name1.getCXXDeductionGuideTemplate()->getDeclName(),
1317330f729Sjoerg Name2.getCXXDeductionGuideTemplate()->getDeclName()))
1327330f729Sjoerg return false;
1337330f729Sjoerg return IsStructurallyEquivalent(Context,
1347330f729Sjoerg Name1.getCXXDeductionGuideTemplate(),
1357330f729Sjoerg Name2.getCXXDeductionGuideTemplate());
1367330f729Sjoerg }
1377330f729Sjoerg
1387330f729Sjoerg case DeclarationName::CXXOperatorName:
1397330f729Sjoerg return Name1.getCXXOverloadedOperator() == Name2.getCXXOverloadedOperator();
1407330f729Sjoerg
1417330f729Sjoerg case DeclarationName::CXXLiteralOperatorName:
1427330f729Sjoerg return IsStructurallyEquivalent(Name1.getCXXLiteralIdentifier(),
1437330f729Sjoerg Name2.getCXXLiteralIdentifier());
1447330f729Sjoerg
1457330f729Sjoerg case DeclarationName::CXXUsingDirective:
1467330f729Sjoerg return true; // FIXME When do we consider two using directives equal?
1477330f729Sjoerg
1487330f729Sjoerg case DeclarationName::ObjCZeroArgSelector:
1497330f729Sjoerg case DeclarationName::ObjCOneArgSelector:
1507330f729Sjoerg case DeclarationName::ObjCMultiArgSelector:
1517330f729Sjoerg return true; // FIXME
1527330f729Sjoerg }
1537330f729Sjoerg
1547330f729Sjoerg llvm_unreachable("Unhandled kind of DeclarationName");
1557330f729Sjoerg return true;
1567330f729Sjoerg }
1577330f729Sjoerg
158*e038c9c4Sjoerg namespace {
159*e038c9c4Sjoerg /// Encapsulates Stmt comparison logic.
160*e038c9c4Sjoerg class StmtComparer {
161*e038c9c4Sjoerg StructuralEquivalenceContext &Context;
1627330f729Sjoerg
163*e038c9c4Sjoerg // IsStmtEquivalent overloads. Each overload compares a specific statement
164*e038c9c4Sjoerg // and only has to compare the data that is specific to the specific statement
165*e038c9c4Sjoerg // class. Should only be called from TraverseStmt.
166*e038c9c4Sjoerg
IsStmtEquivalent(const AddrLabelExpr * E1,const AddrLabelExpr * E2)167*e038c9c4Sjoerg bool IsStmtEquivalent(const AddrLabelExpr *E1, const AddrLabelExpr *E2) {
168*e038c9c4Sjoerg return IsStructurallyEquivalent(Context, E1->getLabel(), E2->getLabel());
169*e038c9c4Sjoerg }
170*e038c9c4Sjoerg
IsStmtEquivalent(const AtomicExpr * E1,const AtomicExpr * E2)171*e038c9c4Sjoerg bool IsStmtEquivalent(const AtomicExpr *E1, const AtomicExpr *E2) {
172*e038c9c4Sjoerg return E1->getOp() == E2->getOp();
173*e038c9c4Sjoerg }
174*e038c9c4Sjoerg
IsStmtEquivalent(const BinaryOperator * E1,const BinaryOperator * E2)175*e038c9c4Sjoerg bool IsStmtEquivalent(const BinaryOperator *E1, const BinaryOperator *E2) {
176*e038c9c4Sjoerg return E1->getOpcode() == E2->getOpcode();
177*e038c9c4Sjoerg }
178*e038c9c4Sjoerg
IsStmtEquivalent(const CallExpr * E1,const CallExpr * E2)179*e038c9c4Sjoerg bool IsStmtEquivalent(const CallExpr *E1, const CallExpr *E2) {
180*e038c9c4Sjoerg // FIXME: IsStructurallyEquivalent requires non-const Decls.
181*e038c9c4Sjoerg Decl *Callee1 = const_cast<Decl *>(E1->getCalleeDecl());
182*e038c9c4Sjoerg Decl *Callee2 = const_cast<Decl *>(E2->getCalleeDecl());
183*e038c9c4Sjoerg
184*e038c9c4Sjoerg // Compare whether both calls know their callee.
185*e038c9c4Sjoerg if (static_cast<bool>(Callee1) != static_cast<bool>(Callee2))
1867330f729Sjoerg return false;
187*e038c9c4Sjoerg
188*e038c9c4Sjoerg // Both calls have no callee, so nothing to do.
189*e038c9c4Sjoerg if (!static_cast<bool>(Callee1))
190*e038c9c4Sjoerg return true;
191*e038c9c4Sjoerg
192*e038c9c4Sjoerg assert(Callee2);
193*e038c9c4Sjoerg return IsStructurallyEquivalent(Context, Callee1, Callee2);
194*e038c9c4Sjoerg }
195*e038c9c4Sjoerg
IsStmtEquivalent(const CharacterLiteral * E1,const CharacterLiteral * E2)196*e038c9c4Sjoerg bool IsStmtEquivalent(const CharacterLiteral *E1,
197*e038c9c4Sjoerg const CharacterLiteral *E2) {
198*e038c9c4Sjoerg return E1->getValue() == E2->getValue() && E1->getKind() == E2->getKind();
199*e038c9c4Sjoerg }
200*e038c9c4Sjoerg
IsStmtEquivalent(const ChooseExpr * E1,const ChooseExpr * E2)201*e038c9c4Sjoerg bool IsStmtEquivalent(const ChooseExpr *E1, const ChooseExpr *E2) {
202*e038c9c4Sjoerg return true; // Semantics only depend on children.
203*e038c9c4Sjoerg }
204*e038c9c4Sjoerg
IsStmtEquivalent(const CompoundStmt * E1,const CompoundStmt * E2)205*e038c9c4Sjoerg bool IsStmtEquivalent(const CompoundStmt *E1, const CompoundStmt *E2) {
206*e038c9c4Sjoerg // Number of children is actually checked by the generic children comparison
207*e038c9c4Sjoerg // code, but a CompoundStmt is one of the few statements where the number of
208*e038c9c4Sjoerg // children frequently differs and the number of statements is also always
209*e038c9c4Sjoerg // precomputed. Directly comparing the number of children here is thus
210*e038c9c4Sjoerg // just an optimization.
211*e038c9c4Sjoerg return E1->size() == E2->size();
212*e038c9c4Sjoerg }
213*e038c9c4Sjoerg
IsStmtEquivalent(const DependentScopeDeclRefExpr * DE1,const DependentScopeDeclRefExpr * DE2)214*e038c9c4Sjoerg bool IsStmtEquivalent(const DependentScopeDeclRefExpr *DE1,
215*e038c9c4Sjoerg const DependentScopeDeclRefExpr *DE2) {
2167330f729Sjoerg if (!IsStructurallyEquivalent(Context, DE1->getDeclName(),
2177330f729Sjoerg DE2->getDeclName()))
2187330f729Sjoerg return false;
2197330f729Sjoerg return IsStructurallyEquivalent(Context, DE1->getQualifier(),
2207330f729Sjoerg DE2->getQualifier());
2217330f729Sjoerg }
222*e038c9c4Sjoerg
IsStmtEquivalent(const Expr * E1,const Expr * E2)223*e038c9c4Sjoerg bool IsStmtEquivalent(const Expr *E1, const Expr *E2) {
224*e038c9c4Sjoerg return IsStructurallyEquivalent(Context, E1->getType(), E2->getType());
225*e038c9c4Sjoerg }
226*e038c9c4Sjoerg
IsStmtEquivalent(const ExpressionTraitExpr * E1,const ExpressionTraitExpr * E2)227*e038c9c4Sjoerg bool IsStmtEquivalent(const ExpressionTraitExpr *E1,
228*e038c9c4Sjoerg const ExpressionTraitExpr *E2) {
229*e038c9c4Sjoerg return E1->getTrait() == E2->getTrait() && E1->getValue() == E2->getValue();
230*e038c9c4Sjoerg }
231*e038c9c4Sjoerg
IsStmtEquivalent(const FloatingLiteral * E1,const FloatingLiteral * E2)232*e038c9c4Sjoerg bool IsStmtEquivalent(const FloatingLiteral *E1, const FloatingLiteral *E2) {
233*e038c9c4Sjoerg return E1->isExact() == E2->isExact() && E1->getValue() == E2->getValue();
234*e038c9c4Sjoerg }
235*e038c9c4Sjoerg
IsStmtEquivalent(const GenericSelectionExpr * E1,const GenericSelectionExpr * E2)236*e038c9c4Sjoerg bool IsStmtEquivalent(const GenericSelectionExpr *E1,
237*e038c9c4Sjoerg const GenericSelectionExpr *E2) {
238*e038c9c4Sjoerg for (auto Pair : zip_longest(E1->getAssocTypeSourceInfos(),
239*e038c9c4Sjoerg E2->getAssocTypeSourceInfos())) {
240*e038c9c4Sjoerg Optional<TypeSourceInfo *> Child1 = std::get<0>(Pair);
241*e038c9c4Sjoerg Optional<TypeSourceInfo *> Child2 = std::get<1>(Pair);
242*e038c9c4Sjoerg // Skip this case if there are a different number of associated types.
243*e038c9c4Sjoerg if (!Child1 || !Child2)
244*e038c9c4Sjoerg return false;
245*e038c9c4Sjoerg
246*e038c9c4Sjoerg if (!IsStructurallyEquivalent(Context, (*Child1)->getType(),
247*e038c9c4Sjoerg (*Child2)->getType()))
248*e038c9c4Sjoerg return false;
249*e038c9c4Sjoerg }
250*e038c9c4Sjoerg
251*e038c9c4Sjoerg return true;
252*e038c9c4Sjoerg }
253*e038c9c4Sjoerg
IsStmtEquivalent(const ImplicitCastExpr * CastE1,const ImplicitCastExpr * CastE2)254*e038c9c4Sjoerg bool IsStmtEquivalent(const ImplicitCastExpr *CastE1,
255*e038c9c4Sjoerg const ImplicitCastExpr *CastE2) {
256*e038c9c4Sjoerg return IsStructurallyEquivalent(Context, CastE1->getType(),
257*e038c9c4Sjoerg CastE2->getType());
258*e038c9c4Sjoerg }
259*e038c9c4Sjoerg
IsStmtEquivalent(const IntegerLiteral * E1,const IntegerLiteral * E2)260*e038c9c4Sjoerg bool IsStmtEquivalent(const IntegerLiteral *E1, const IntegerLiteral *E2) {
261*e038c9c4Sjoerg return E1->getValue() == E2->getValue();
262*e038c9c4Sjoerg }
263*e038c9c4Sjoerg
IsStmtEquivalent(const MemberExpr * E1,const MemberExpr * E2)264*e038c9c4Sjoerg bool IsStmtEquivalent(const MemberExpr *E1, const MemberExpr *E2) {
265*e038c9c4Sjoerg return IsStructurallyEquivalent(Context, E1->getFoundDecl(),
266*e038c9c4Sjoerg E2->getFoundDecl());
267*e038c9c4Sjoerg }
268*e038c9c4Sjoerg
IsStmtEquivalent(const ObjCStringLiteral * E1,const ObjCStringLiteral * E2)269*e038c9c4Sjoerg bool IsStmtEquivalent(const ObjCStringLiteral *E1,
270*e038c9c4Sjoerg const ObjCStringLiteral *E2) {
271*e038c9c4Sjoerg // Just wraps a StringLiteral child.
272*e038c9c4Sjoerg return true;
273*e038c9c4Sjoerg }
274*e038c9c4Sjoerg
IsStmtEquivalent(const Stmt * S1,const Stmt * S2)275*e038c9c4Sjoerg bool IsStmtEquivalent(const Stmt *S1, const Stmt *S2) { return true; }
276*e038c9c4Sjoerg
IsStmtEquivalent(const SourceLocExpr * E1,const SourceLocExpr * E2)277*e038c9c4Sjoerg bool IsStmtEquivalent(const SourceLocExpr *E1, const SourceLocExpr *E2) {
278*e038c9c4Sjoerg return E1->getIdentKind() == E2->getIdentKind();
279*e038c9c4Sjoerg }
280*e038c9c4Sjoerg
IsStmtEquivalent(const StmtExpr * E1,const StmtExpr * E2)281*e038c9c4Sjoerg bool IsStmtEquivalent(const StmtExpr *E1, const StmtExpr *E2) {
282*e038c9c4Sjoerg return E1->getTemplateDepth() == E2->getTemplateDepth();
283*e038c9c4Sjoerg }
284*e038c9c4Sjoerg
IsStmtEquivalent(const StringLiteral * E1,const StringLiteral * E2)285*e038c9c4Sjoerg bool IsStmtEquivalent(const StringLiteral *E1, const StringLiteral *E2) {
286*e038c9c4Sjoerg return E1->getBytes() == E2->getBytes();
287*e038c9c4Sjoerg }
288*e038c9c4Sjoerg
IsStmtEquivalent(const SubstNonTypeTemplateParmExpr * E1,const SubstNonTypeTemplateParmExpr * E2)289*e038c9c4Sjoerg bool IsStmtEquivalent(const SubstNonTypeTemplateParmExpr *E1,
290*e038c9c4Sjoerg const SubstNonTypeTemplateParmExpr *E2) {
291*e038c9c4Sjoerg return IsStructurallyEquivalent(Context, E1->getParameter(),
292*e038c9c4Sjoerg E2->getParameter());
293*e038c9c4Sjoerg }
294*e038c9c4Sjoerg
IsStmtEquivalent(const SubstNonTypeTemplateParmPackExpr * E1,const SubstNonTypeTemplateParmPackExpr * E2)295*e038c9c4Sjoerg bool IsStmtEquivalent(const SubstNonTypeTemplateParmPackExpr *E1,
296*e038c9c4Sjoerg const SubstNonTypeTemplateParmPackExpr *E2) {
297*e038c9c4Sjoerg return IsStructurallyEquivalent(Context, E1->getArgumentPack(),
298*e038c9c4Sjoerg E2->getArgumentPack());
299*e038c9c4Sjoerg }
300*e038c9c4Sjoerg
IsStmtEquivalent(const TypeTraitExpr * E1,const TypeTraitExpr * E2)301*e038c9c4Sjoerg bool IsStmtEquivalent(const TypeTraitExpr *E1, const TypeTraitExpr *E2) {
302*e038c9c4Sjoerg if (E1->getTrait() != E2->getTrait())
303*e038c9c4Sjoerg return false;
304*e038c9c4Sjoerg
305*e038c9c4Sjoerg for (auto Pair : zip_longest(E1->getArgs(), E2->getArgs())) {
306*e038c9c4Sjoerg Optional<TypeSourceInfo *> Child1 = std::get<0>(Pair);
307*e038c9c4Sjoerg Optional<TypeSourceInfo *> Child2 = std::get<1>(Pair);
308*e038c9c4Sjoerg // Different number of args.
309*e038c9c4Sjoerg if (!Child1 || !Child2)
310*e038c9c4Sjoerg return false;
311*e038c9c4Sjoerg
312*e038c9c4Sjoerg if (!IsStructurallyEquivalent(Context, (*Child1)->getType(),
313*e038c9c4Sjoerg (*Child2)->getType()))
314*e038c9c4Sjoerg return false;
315*e038c9c4Sjoerg }
316*e038c9c4Sjoerg return true;
317*e038c9c4Sjoerg }
318*e038c9c4Sjoerg
IsStmtEquivalent(const UnaryExprOrTypeTraitExpr * E1,const UnaryExprOrTypeTraitExpr * E2)319*e038c9c4Sjoerg bool IsStmtEquivalent(const UnaryExprOrTypeTraitExpr *E1,
320*e038c9c4Sjoerg const UnaryExprOrTypeTraitExpr *E2) {
321*e038c9c4Sjoerg if (E1->getKind() != E2->getKind())
322*e038c9c4Sjoerg return false;
323*e038c9c4Sjoerg return IsStructurallyEquivalent(Context, E1->getTypeOfArgument(),
324*e038c9c4Sjoerg E2->getTypeOfArgument());
325*e038c9c4Sjoerg }
326*e038c9c4Sjoerg
IsStmtEquivalent(const UnaryOperator * E1,const UnaryOperator * E2)327*e038c9c4Sjoerg bool IsStmtEquivalent(const UnaryOperator *E1, const UnaryOperator *E2) {
328*e038c9c4Sjoerg return E1->getOpcode() == E2->getOpcode();
329*e038c9c4Sjoerg }
330*e038c9c4Sjoerg
IsStmtEquivalent(const VAArgExpr * E1,const VAArgExpr * E2)331*e038c9c4Sjoerg bool IsStmtEquivalent(const VAArgExpr *E1, const VAArgExpr *E2) {
332*e038c9c4Sjoerg // Semantics only depend on children.
333*e038c9c4Sjoerg return true;
334*e038c9c4Sjoerg }
335*e038c9c4Sjoerg
336*e038c9c4Sjoerg /// End point of the traversal chain.
TraverseStmt(const Stmt * S1,const Stmt * S2)337*e038c9c4Sjoerg bool TraverseStmt(const Stmt *S1, const Stmt *S2) { return true; }
338*e038c9c4Sjoerg
339*e038c9c4Sjoerg // Create traversal methods that traverse the class hierarchy and return
340*e038c9c4Sjoerg // the accumulated result of the comparison. Each TraverseStmt overload
341*e038c9c4Sjoerg // calls the TraverseStmt overload of the parent class. For example,
342*e038c9c4Sjoerg // the TraverseStmt overload for 'BinaryOperator' calls the TraverseStmt
343*e038c9c4Sjoerg // overload of 'Expr' which then calls the overload for 'Stmt'.
344*e038c9c4Sjoerg #define STMT(CLASS, PARENT) \
345*e038c9c4Sjoerg bool TraverseStmt(const CLASS *S1, const CLASS *S2) { \
346*e038c9c4Sjoerg if (!TraverseStmt(static_cast<const PARENT *>(S1), \
347*e038c9c4Sjoerg static_cast<const PARENT *>(S2))) \
348*e038c9c4Sjoerg return false; \
349*e038c9c4Sjoerg return IsStmtEquivalent(S1, S2); \
350*e038c9c4Sjoerg }
351*e038c9c4Sjoerg #include "clang/AST/StmtNodes.inc"
352*e038c9c4Sjoerg
353*e038c9c4Sjoerg public:
StmtComparer(StructuralEquivalenceContext & C)354*e038c9c4Sjoerg StmtComparer(StructuralEquivalenceContext &C) : Context(C) {}
355*e038c9c4Sjoerg
356*e038c9c4Sjoerg /// Determine whether two statements are equivalent. The statements have to
357*e038c9c4Sjoerg /// be of the same kind. The children of the statements and their properties
358*e038c9c4Sjoerg /// are not compared by this function.
IsEquivalent(const Stmt * S1,const Stmt * S2)359*e038c9c4Sjoerg bool IsEquivalent(const Stmt *S1, const Stmt *S2) {
360*e038c9c4Sjoerg if (S1->getStmtClass() != S2->getStmtClass())
361*e038c9c4Sjoerg return false;
362*e038c9c4Sjoerg
363*e038c9c4Sjoerg // Each TraverseStmt walks the class hierarchy from the leaf class to
364*e038c9c4Sjoerg // the root class 'Stmt' (e.g. 'BinaryOperator' -> 'Expr' -> 'Stmt'). Cast
365*e038c9c4Sjoerg // the Stmt we have here to its specific subclass so that we call the
366*e038c9c4Sjoerg // overload that walks the whole class hierarchy from leaf to root (e.g.,
367*e038c9c4Sjoerg // cast to 'BinaryOperator' so that 'Expr' and 'Stmt' is traversed).
368*e038c9c4Sjoerg switch (S1->getStmtClass()) {
369*e038c9c4Sjoerg case Stmt::NoStmtClass:
370*e038c9c4Sjoerg llvm_unreachable("Can't traverse NoStmtClass");
371*e038c9c4Sjoerg #define STMT(CLASS, PARENT) \
372*e038c9c4Sjoerg case Stmt::StmtClass::CLASS##Class: \
373*e038c9c4Sjoerg return TraverseStmt(static_cast<const CLASS *>(S1), \
374*e038c9c4Sjoerg static_cast<const CLASS *>(S2));
375*e038c9c4Sjoerg #define ABSTRACT_STMT(S)
376*e038c9c4Sjoerg #include "clang/AST/StmtNodes.inc"
377*e038c9c4Sjoerg }
378*e038c9c4Sjoerg llvm_unreachable("Invalid statement kind");
379*e038c9c4Sjoerg }
380*e038c9c4Sjoerg };
381*e038c9c4Sjoerg } // namespace
382*e038c9c4Sjoerg
383*e038c9c4Sjoerg /// Determine structural equivalence of two statements.
IsStructurallyEquivalent(StructuralEquivalenceContext & Context,const Stmt * S1,const Stmt * S2)384*e038c9c4Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
385*e038c9c4Sjoerg const Stmt *S1, const Stmt *S2) {
386*e038c9c4Sjoerg if (!S1 || !S2)
387*e038c9c4Sjoerg return S1 == S2;
388*e038c9c4Sjoerg
389*e038c9c4Sjoerg // Compare the statements itself.
390*e038c9c4Sjoerg StmtComparer Comparer(Context);
391*e038c9c4Sjoerg if (!Comparer.IsEquivalent(S1, S2))
392*e038c9c4Sjoerg return false;
393*e038c9c4Sjoerg
394*e038c9c4Sjoerg // Iterate over the children of both statements and also compare them.
395*e038c9c4Sjoerg for (auto Pair : zip_longest(S1->children(), S2->children())) {
396*e038c9c4Sjoerg Optional<const Stmt *> Child1 = std::get<0>(Pair);
397*e038c9c4Sjoerg Optional<const Stmt *> Child2 = std::get<1>(Pair);
398*e038c9c4Sjoerg // One of the statements has a different amount of children than the other,
399*e038c9c4Sjoerg // so the statements can't be equivalent.
400*e038c9c4Sjoerg if (!Child1 || !Child2)
401*e038c9c4Sjoerg return false;
402*e038c9c4Sjoerg if (!IsStructurallyEquivalent(Context, *Child1, *Child2))
403*e038c9c4Sjoerg return false;
404*e038c9c4Sjoerg }
4057330f729Sjoerg return true;
4067330f729Sjoerg }
4077330f729Sjoerg
4087330f729Sjoerg /// Determine whether two identifiers are equivalent.
IsStructurallyEquivalent(const IdentifierInfo * Name1,const IdentifierInfo * Name2)4097330f729Sjoerg static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
4107330f729Sjoerg const IdentifierInfo *Name2) {
4117330f729Sjoerg if (!Name1 || !Name2)
4127330f729Sjoerg return Name1 == Name2;
4137330f729Sjoerg
4147330f729Sjoerg return Name1->getName() == Name2->getName();
4157330f729Sjoerg }
4167330f729Sjoerg
4177330f729Sjoerg /// Determine whether two nested-name-specifiers are equivalent.
IsStructurallyEquivalent(StructuralEquivalenceContext & Context,NestedNameSpecifier * NNS1,NestedNameSpecifier * NNS2)4187330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
4197330f729Sjoerg NestedNameSpecifier *NNS1,
4207330f729Sjoerg NestedNameSpecifier *NNS2) {
4217330f729Sjoerg if (NNS1->getKind() != NNS2->getKind())
4227330f729Sjoerg return false;
4237330f729Sjoerg
4247330f729Sjoerg NestedNameSpecifier *Prefix1 = NNS1->getPrefix(),
4257330f729Sjoerg *Prefix2 = NNS2->getPrefix();
4267330f729Sjoerg if ((bool)Prefix1 != (bool)Prefix2)
4277330f729Sjoerg return false;
4287330f729Sjoerg
4297330f729Sjoerg if (Prefix1)
4307330f729Sjoerg if (!IsStructurallyEquivalent(Context, Prefix1, Prefix2))
4317330f729Sjoerg return false;
4327330f729Sjoerg
4337330f729Sjoerg switch (NNS1->getKind()) {
4347330f729Sjoerg case NestedNameSpecifier::Identifier:
4357330f729Sjoerg return IsStructurallyEquivalent(NNS1->getAsIdentifier(),
4367330f729Sjoerg NNS2->getAsIdentifier());
4377330f729Sjoerg case NestedNameSpecifier::Namespace:
4387330f729Sjoerg return IsStructurallyEquivalent(Context, NNS1->getAsNamespace(),
4397330f729Sjoerg NNS2->getAsNamespace());
4407330f729Sjoerg case NestedNameSpecifier::NamespaceAlias:
4417330f729Sjoerg return IsStructurallyEquivalent(Context, NNS1->getAsNamespaceAlias(),
4427330f729Sjoerg NNS2->getAsNamespaceAlias());
4437330f729Sjoerg case NestedNameSpecifier::TypeSpec:
4447330f729Sjoerg case NestedNameSpecifier::TypeSpecWithTemplate:
4457330f729Sjoerg return IsStructurallyEquivalent(Context, QualType(NNS1->getAsType(), 0),
4467330f729Sjoerg QualType(NNS2->getAsType(), 0));
4477330f729Sjoerg case NestedNameSpecifier::Global:
4487330f729Sjoerg return true;
4497330f729Sjoerg case NestedNameSpecifier::Super:
4507330f729Sjoerg return IsStructurallyEquivalent(Context, NNS1->getAsRecordDecl(),
4517330f729Sjoerg NNS2->getAsRecordDecl());
4527330f729Sjoerg }
4537330f729Sjoerg return false;
4547330f729Sjoerg }
4557330f729Sjoerg
IsStructurallyEquivalent(StructuralEquivalenceContext & Context,const TemplateName & N1,const TemplateName & N2)4567330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
4577330f729Sjoerg const TemplateName &N1,
4587330f729Sjoerg const TemplateName &N2) {
4597330f729Sjoerg TemplateDecl *TemplateDeclN1 = N1.getAsTemplateDecl();
4607330f729Sjoerg TemplateDecl *TemplateDeclN2 = N2.getAsTemplateDecl();
4617330f729Sjoerg if (TemplateDeclN1 && TemplateDeclN2) {
4627330f729Sjoerg if (!IsStructurallyEquivalent(Context, TemplateDeclN1, TemplateDeclN2))
4637330f729Sjoerg return false;
4647330f729Sjoerg // If the kind is different we compare only the template decl.
4657330f729Sjoerg if (N1.getKind() != N2.getKind())
4667330f729Sjoerg return true;
4677330f729Sjoerg } else if (TemplateDeclN1 || TemplateDeclN2)
4687330f729Sjoerg return false;
4697330f729Sjoerg else if (N1.getKind() != N2.getKind())
4707330f729Sjoerg return false;
4717330f729Sjoerg
4727330f729Sjoerg // Check for special case incompatibilities.
4737330f729Sjoerg switch (N1.getKind()) {
4747330f729Sjoerg
4757330f729Sjoerg case TemplateName::OverloadedTemplate: {
4767330f729Sjoerg OverloadedTemplateStorage *OS1 = N1.getAsOverloadedTemplate(),
4777330f729Sjoerg *OS2 = N2.getAsOverloadedTemplate();
4787330f729Sjoerg OverloadedTemplateStorage::iterator I1 = OS1->begin(), I2 = OS2->begin(),
4797330f729Sjoerg E1 = OS1->end(), E2 = OS2->end();
4807330f729Sjoerg for (; I1 != E1 && I2 != E2; ++I1, ++I2)
4817330f729Sjoerg if (!IsStructurallyEquivalent(Context, *I1, *I2))
4827330f729Sjoerg return false;
4837330f729Sjoerg return I1 == E1 && I2 == E2;
4847330f729Sjoerg }
4857330f729Sjoerg
4867330f729Sjoerg case TemplateName::AssumedTemplate: {
4877330f729Sjoerg AssumedTemplateStorage *TN1 = N1.getAsAssumedTemplateName(),
4887330f729Sjoerg *TN2 = N1.getAsAssumedTemplateName();
4897330f729Sjoerg return TN1->getDeclName() == TN2->getDeclName();
4907330f729Sjoerg }
4917330f729Sjoerg
4927330f729Sjoerg case TemplateName::DependentTemplate: {
4937330f729Sjoerg DependentTemplateName *DN1 = N1.getAsDependentTemplateName(),
4947330f729Sjoerg *DN2 = N2.getAsDependentTemplateName();
4957330f729Sjoerg if (!IsStructurallyEquivalent(Context, DN1->getQualifier(),
4967330f729Sjoerg DN2->getQualifier()))
4977330f729Sjoerg return false;
4987330f729Sjoerg if (DN1->isIdentifier() && DN2->isIdentifier())
4997330f729Sjoerg return IsStructurallyEquivalent(DN1->getIdentifier(),
5007330f729Sjoerg DN2->getIdentifier());
5017330f729Sjoerg else if (DN1->isOverloadedOperator() && DN2->isOverloadedOperator())
5027330f729Sjoerg return DN1->getOperator() == DN2->getOperator();
5037330f729Sjoerg return false;
5047330f729Sjoerg }
5057330f729Sjoerg
5067330f729Sjoerg case TemplateName::SubstTemplateTemplateParmPack: {
5077330f729Sjoerg SubstTemplateTemplateParmPackStorage
5087330f729Sjoerg *P1 = N1.getAsSubstTemplateTemplateParmPack(),
5097330f729Sjoerg *P2 = N2.getAsSubstTemplateTemplateParmPack();
5107330f729Sjoerg return IsStructurallyEquivalent(Context, P1->getArgumentPack(),
5117330f729Sjoerg P2->getArgumentPack()) &&
5127330f729Sjoerg IsStructurallyEquivalent(Context, P1->getParameterPack(),
5137330f729Sjoerg P2->getParameterPack());
5147330f729Sjoerg }
5157330f729Sjoerg
5167330f729Sjoerg case TemplateName::Template:
5177330f729Sjoerg case TemplateName::QualifiedTemplate:
5187330f729Sjoerg case TemplateName::SubstTemplateTemplateParm:
5197330f729Sjoerg // It is sufficient to check value of getAsTemplateDecl.
5207330f729Sjoerg break;
5217330f729Sjoerg
5227330f729Sjoerg }
5237330f729Sjoerg
5247330f729Sjoerg return true;
5257330f729Sjoerg }
5267330f729Sjoerg
5277330f729Sjoerg /// Determine whether two template arguments are equivalent.
IsStructurallyEquivalent(StructuralEquivalenceContext & Context,const TemplateArgument & Arg1,const TemplateArgument & Arg2)5287330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
5297330f729Sjoerg const TemplateArgument &Arg1,
5307330f729Sjoerg const TemplateArgument &Arg2) {
5317330f729Sjoerg if (Arg1.getKind() != Arg2.getKind())
5327330f729Sjoerg return false;
5337330f729Sjoerg
5347330f729Sjoerg switch (Arg1.getKind()) {
5357330f729Sjoerg case TemplateArgument::Null:
5367330f729Sjoerg return true;
5377330f729Sjoerg
5387330f729Sjoerg case TemplateArgument::Type:
5397330f729Sjoerg return IsStructurallyEquivalent(Context, Arg1.getAsType(), Arg2.getAsType());
5407330f729Sjoerg
5417330f729Sjoerg case TemplateArgument::Integral:
5427330f729Sjoerg if (!IsStructurallyEquivalent(Context, Arg1.getIntegralType(),
5437330f729Sjoerg Arg2.getIntegralType()))
5447330f729Sjoerg return false;
5457330f729Sjoerg
5467330f729Sjoerg return llvm::APSInt::isSameValue(Arg1.getAsIntegral(),
5477330f729Sjoerg Arg2.getAsIntegral());
5487330f729Sjoerg
5497330f729Sjoerg case TemplateArgument::Declaration:
5507330f729Sjoerg return IsStructurallyEquivalent(Context, Arg1.getAsDecl(), Arg2.getAsDecl());
5517330f729Sjoerg
5527330f729Sjoerg case TemplateArgument::NullPtr:
5537330f729Sjoerg return true; // FIXME: Is this correct?
5547330f729Sjoerg
5557330f729Sjoerg case TemplateArgument::Template:
5567330f729Sjoerg return IsStructurallyEquivalent(Context, Arg1.getAsTemplate(),
5577330f729Sjoerg Arg2.getAsTemplate());
5587330f729Sjoerg
5597330f729Sjoerg case TemplateArgument::TemplateExpansion:
5607330f729Sjoerg return IsStructurallyEquivalent(Context,
5617330f729Sjoerg Arg1.getAsTemplateOrTemplatePattern(),
5627330f729Sjoerg Arg2.getAsTemplateOrTemplatePattern());
5637330f729Sjoerg
5647330f729Sjoerg case TemplateArgument::Expression:
5657330f729Sjoerg return IsStructurallyEquivalent(Context, Arg1.getAsExpr(),
5667330f729Sjoerg Arg2.getAsExpr());
5677330f729Sjoerg
5687330f729Sjoerg case TemplateArgument::Pack:
5697330f729Sjoerg if (Arg1.pack_size() != Arg2.pack_size())
5707330f729Sjoerg return false;
5717330f729Sjoerg
5727330f729Sjoerg for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
5737330f729Sjoerg if (!IsStructurallyEquivalent(Context, Arg1.pack_begin()[I],
5747330f729Sjoerg Arg2.pack_begin()[I]))
5757330f729Sjoerg return false;
5767330f729Sjoerg
5777330f729Sjoerg return true;
5787330f729Sjoerg }
5797330f729Sjoerg
5807330f729Sjoerg llvm_unreachable("Invalid template argument kind");
5817330f729Sjoerg }
5827330f729Sjoerg
5837330f729Sjoerg /// Determine structural equivalence for the common part of array
5847330f729Sjoerg /// types.
IsArrayStructurallyEquivalent(StructuralEquivalenceContext & Context,const ArrayType * Array1,const ArrayType * Array2)5857330f729Sjoerg static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
5867330f729Sjoerg const ArrayType *Array1,
5877330f729Sjoerg const ArrayType *Array2) {
5887330f729Sjoerg if (!IsStructurallyEquivalent(Context, Array1->getElementType(),
5897330f729Sjoerg Array2->getElementType()))
5907330f729Sjoerg return false;
5917330f729Sjoerg if (Array1->getSizeModifier() != Array2->getSizeModifier())
5927330f729Sjoerg return false;
5937330f729Sjoerg if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
5947330f729Sjoerg return false;
5957330f729Sjoerg
5967330f729Sjoerg return true;
5977330f729Sjoerg }
5987330f729Sjoerg
5997330f729Sjoerg /// Determine structural equivalence based on the ExtInfo of functions. This
6007330f729Sjoerg /// is inspired by ASTContext::mergeFunctionTypes(), we compare calling
6017330f729Sjoerg /// conventions bits but must not compare some other bits.
IsStructurallyEquivalent(StructuralEquivalenceContext & Context,FunctionType::ExtInfo EI1,FunctionType::ExtInfo EI2)6027330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
6037330f729Sjoerg FunctionType::ExtInfo EI1,
6047330f729Sjoerg FunctionType::ExtInfo EI2) {
6057330f729Sjoerg // Compatible functions must have compatible calling conventions.
6067330f729Sjoerg if (EI1.getCC() != EI2.getCC())
6077330f729Sjoerg return false;
6087330f729Sjoerg
6097330f729Sjoerg // Regparm is part of the calling convention.
6107330f729Sjoerg if (EI1.getHasRegParm() != EI2.getHasRegParm())
6117330f729Sjoerg return false;
6127330f729Sjoerg if (EI1.getRegParm() != EI2.getRegParm())
6137330f729Sjoerg return false;
6147330f729Sjoerg
6157330f729Sjoerg if (EI1.getProducesResult() != EI2.getProducesResult())
6167330f729Sjoerg return false;
6177330f729Sjoerg if (EI1.getNoCallerSavedRegs() != EI2.getNoCallerSavedRegs())
6187330f729Sjoerg return false;
6197330f729Sjoerg if (EI1.getNoCfCheck() != EI2.getNoCfCheck())
6207330f729Sjoerg return false;
6217330f729Sjoerg
6227330f729Sjoerg return true;
6237330f729Sjoerg }
6247330f729Sjoerg
6257330f729Sjoerg /// Check the equivalence of exception specifications.
IsEquivalentExceptionSpec(StructuralEquivalenceContext & Context,const FunctionProtoType * Proto1,const FunctionProtoType * Proto2)6267330f729Sjoerg static bool IsEquivalentExceptionSpec(StructuralEquivalenceContext &Context,
6277330f729Sjoerg const FunctionProtoType *Proto1,
6287330f729Sjoerg const FunctionProtoType *Proto2) {
6297330f729Sjoerg
6307330f729Sjoerg auto Spec1 = Proto1->getExceptionSpecType();
6317330f729Sjoerg auto Spec2 = Proto2->getExceptionSpecType();
6327330f729Sjoerg
6337330f729Sjoerg if (isUnresolvedExceptionSpec(Spec1) || isUnresolvedExceptionSpec(Spec2))
6347330f729Sjoerg return true;
6357330f729Sjoerg
6367330f729Sjoerg if (Spec1 != Spec2)
6377330f729Sjoerg return false;
6387330f729Sjoerg if (Spec1 == EST_Dynamic) {
6397330f729Sjoerg if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
6407330f729Sjoerg return false;
6417330f729Sjoerg for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
6427330f729Sjoerg if (!IsStructurallyEquivalent(Context, Proto1->getExceptionType(I),
6437330f729Sjoerg Proto2->getExceptionType(I)))
6447330f729Sjoerg return false;
6457330f729Sjoerg }
6467330f729Sjoerg } else if (isComputedNoexcept(Spec1)) {
6477330f729Sjoerg if (!IsStructurallyEquivalent(Context, Proto1->getNoexceptExpr(),
6487330f729Sjoerg Proto2->getNoexceptExpr()))
6497330f729Sjoerg return false;
6507330f729Sjoerg }
6517330f729Sjoerg
6527330f729Sjoerg return true;
6537330f729Sjoerg }
6547330f729Sjoerg
6557330f729Sjoerg /// Determine structural equivalence of two types.
IsStructurallyEquivalent(StructuralEquivalenceContext & Context,QualType T1,QualType T2)6567330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
6577330f729Sjoerg QualType T1, QualType T2) {
6587330f729Sjoerg if (T1.isNull() || T2.isNull())
6597330f729Sjoerg return T1.isNull() && T2.isNull();
6607330f729Sjoerg
6617330f729Sjoerg QualType OrigT1 = T1;
6627330f729Sjoerg QualType OrigT2 = T2;
6637330f729Sjoerg
6647330f729Sjoerg if (!Context.StrictTypeSpelling) {
6657330f729Sjoerg // We aren't being strict about token-to-token equivalence of types,
6667330f729Sjoerg // so map down to the canonical type.
6677330f729Sjoerg T1 = Context.FromCtx.getCanonicalType(T1);
6687330f729Sjoerg T2 = Context.ToCtx.getCanonicalType(T2);
6697330f729Sjoerg }
6707330f729Sjoerg
6717330f729Sjoerg if (T1.getQualifiers() != T2.getQualifiers())
6727330f729Sjoerg return false;
6737330f729Sjoerg
6747330f729Sjoerg Type::TypeClass TC = T1->getTypeClass();
6757330f729Sjoerg
6767330f729Sjoerg if (T1->getTypeClass() != T2->getTypeClass()) {
6777330f729Sjoerg // Compare function types with prototypes vs. without prototypes as if
6787330f729Sjoerg // both did not have prototypes.
6797330f729Sjoerg if (T1->getTypeClass() == Type::FunctionProto &&
6807330f729Sjoerg T2->getTypeClass() == Type::FunctionNoProto)
6817330f729Sjoerg TC = Type::FunctionNoProto;
6827330f729Sjoerg else if (T1->getTypeClass() == Type::FunctionNoProto &&
6837330f729Sjoerg T2->getTypeClass() == Type::FunctionProto)
6847330f729Sjoerg TC = Type::FunctionNoProto;
6857330f729Sjoerg else
6867330f729Sjoerg return false;
6877330f729Sjoerg }
6887330f729Sjoerg
6897330f729Sjoerg switch (TC) {
6907330f729Sjoerg case Type::Builtin:
6917330f729Sjoerg // FIXME: Deal with Char_S/Char_U.
6927330f729Sjoerg if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
6937330f729Sjoerg return false;
6947330f729Sjoerg break;
6957330f729Sjoerg
6967330f729Sjoerg case Type::Complex:
6977330f729Sjoerg if (!IsStructurallyEquivalent(Context,
6987330f729Sjoerg cast<ComplexType>(T1)->getElementType(),
6997330f729Sjoerg cast<ComplexType>(T2)->getElementType()))
7007330f729Sjoerg return false;
7017330f729Sjoerg break;
7027330f729Sjoerg
7037330f729Sjoerg case Type::Adjusted:
7047330f729Sjoerg case Type::Decayed:
7057330f729Sjoerg if (!IsStructurallyEquivalent(Context,
7067330f729Sjoerg cast<AdjustedType>(T1)->getOriginalType(),
7077330f729Sjoerg cast<AdjustedType>(T2)->getOriginalType()))
7087330f729Sjoerg return false;
7097330f729Sjoerg break;
7107330f729Sjoerg
7117330f729Sjoerg case Type::Pointer:
7127330f729Sjoerg if (!IsStructurallyEquivalent(Context,
7137330f729Sjoerg cast<PointerType>(T1)->getPointeeType(),
7147330f729Sjoerg cast<PointerType>(T2)->getPointeeType()))
7157330f729Sjoerg return false;
7167330f729Sjoerg break;
7177330f729Sjoerg
7187330f729Sjoerg case Type::BlockPointer:
7197330f729Sjoerg if (!IsStructurallyEquivalent(Context,
7207330f729Sjoerg cast<BlockPointerType>(T1)->getPointeeType(),
7217330f729Sjoerg cast<BlockPointerType>(T2)->getPointeeType()))
7227330f729Sjoerg return false;
7237330f729Sjoerg break;
7247330f729Sjoerg
7257330f729Sjoerg case Type::LValueReference:
7267330f729Sjoerg case Type::RValueReference: {
7277330f729Sjoerg const auto *Ref1 = cast<ReferenceType>(T1);
7287330f729Sjoerg const auto *Ref2 = cast<ReferenceType>(T2);
7297330f729Sjoerg if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
7307330f729Sjoerg return false;
7317330f729Sjoerg if (Ref1->isInnerRef() != Ref2->isInnerRef())
7327330f729Sjoerg return false;
7337330f729Sjoerg if (!IsStructurallyEquivalent(Context, Ref1->getPointeeTypeAsWritten(),
7347330f729Sjoerg Ref2->getPointeeTypeAsWritten()))
7357330f729Sjoerg return false;
7367330f729Sjoerg break;
7377330f729Sjoerg }
7387330f729Sjoerg
7397330f729Sjoerg case Type::MemberPointer: {
7407330f729Sjoerg const auto *MemPtr1 = cast<MemberPointerType>(T1);
7417330f729Sjoerg const auto *MemPtr2 = cast<MemberPointerType>(T2);
7427330f729Sjoerg if (!IsStructurallyEquivalent(Context, MemPtr1->getPointeeType(),
7437330f729Sjoerg MemPtr2->getPointeeType()))
7447330f729Sjoerg return false;
7457330f729Sjoerg if (!IsStructurallyEquivalent(Context, QualType(MemPtr1->getClass(), 0),
7467330f729Sjoerg QualType(MemPtr2->getClass(), 0)))
7477330f729Sjoerg return false;
7487330f729Sjoerg break;
7497330f729Sjoerg }
7507330f729Sjoerg
7517330f729Sjoerg case Type::ConstantArray: {
7527330f729Sjoerg const auto *Array1 = cast<ConstantArrayType>(T1);
7537330f729Sjoerg const auto *Array2 = cast<ConstantArrayType>(T2);
7547330f729Sjoerg if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
7557330f729Sjoerg return false;
7567330f729Sjoerg
7577330f729Sjoerg if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
7587330f729Sjoerg return false;
7597330f729Sjoerg break;
7607330f729Sjoerg }
7617330f729Sjoerg
7627330f729Sjoerg case Type::IncompleteArray:
7637330f729Sjoerg if (!IsArrayStructurallyEquivalent(Context, cast<ArrayType>(T1),
7647330f729Sjoerg cast<ArrayType>(T2)))
7657330f729Sjoerg return false;
7667330f729Sjoerg break;
7677330f729Sjoerg
7687330f729Sjoerg case Type::VariableArray: {
7697330f729Sjoerg const auto *Array1 = cast<VariableArrayType>(T1);
7707330f729Sjoerg const auto *Array2 = cast<VariableArrayType>(T2);
7717330f729Sjoerg if (!IsStructurallyEquivalent(Context, Array1->getSizeExpr(),
7727330f729Sjoerg Array2->getSizeExpr()))
7737330f729Sjoerg return false;
7747330f729Sjoerg
7757330f729Sjoerg if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
7767330f729Sjoerg return false;
7777330f729Sjoerg
7787330f729Sjoerg break;
7797330f729Sjoerg }
7807330f729Sjoerg
7817330f729Sjoerg case Type::DependentSizedArray: {
7827330f729Sjoerg const auto *Array1 = cast<DependentSizedArrayType>(T1);
7837330f729Sjoerg const auto *Array2 = cast<DependentSizedArrayType>(T2);
7847330f729Sjoerg if (!IsStructurallyEquivalent(Context, Array1->getSizeExpr(),
7857330f729Sjoerg Array2->getSizeExpr()))
7867330f729Sjoerg return false;
7877330f729Sjoerg
7887330f729Sjoerg if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
7897330f729Sjoerg return false;
7907330f729Sjoerg
7917330f729Sjoerg break;
7927330f729Sjoerg }
7937330f729Sjoerg
7947330f729Sjoerg case Type::DependentAddressSpace: {
7957330f729Sjoerg const auto *DepAddressSpace1 = cast<DependentAddressSpaceType>(T1);
7967330f729Sjoerg const auto *DepAddressSpace2 = cast<DependentAddressSpaceType>(T2);
7977330f729Sjoerg if (!IsStructurallyEquivalent(Context, DepAddressSpace1->getAddrSpaceExpr(),
7987330f729Sjoerg DepAddressSpace2->getAddrSpaceExpr()))
7997330f729Sjoerg return false;
8007330f729Sjoerg if (!IsStructurallyEquivalent(Context, DepAddressSpace1->getPointeeType(),
8017330f729Sjoerg DepAddressSpace2->getPointeeType()))
8027330f729Sjoerg return false;
8037330f729Sjoerg
8047330f729Sjoerg break;
8057330f729Sjoerg }
8067330f729Sjoerg
8077330f729Sjoerg case Type::DependentSizedExtVector: {
8087330f729Sjoerg const auto *Vec1 = cast<DependentSizedExtVectorType>(T1);
8097330f729Sjoerg const auto *Vec2 = cast<DependentSizedExtVectorType>(T2);
8107330f729Sjoerg if (!IsStructurallyEquivalent(Context, Vec1->getSizeExpr(),
8117330f729Sjoerg Vec2->getSizeExpr()))
8127330f729Sjoerg return false;
8137330f729Sjoerg if (!IsStructurallyEquivalent(Context, Vec1->getElementType(),
8147330f729Sjoerg Vec2->getElementType()))
8157330f729Sjoerg return false;
8167330f729Sjoerg break;
8177330f729Sjoerg }
8187330f729Sjoerg
8197330f729Sjoerg case Type::DependentVector: {
8207330f729Sjoerg const auto *Vec1 = cast<DependentVectorType>(T1);
8217330f729Sjoerg const auto *Vec2 = cast<DependentVectorType>(T2);
8227330f729Sjoerg if (Vec1->getVectorKind() != Vec2->getVectorKind())
8237330f729Sjoerg return false;
8247330f729Sjoerg if (!IsStructurallyEquivalent(Context, Vec1->getSizeExpr(),
8257330f729Sjoerg Vec2->getSizeExpr()))
8267330f729Sjoerg return false;
8277330f729Sjoerg if (!IsStructurallyEquivalent(Context, Vec1->getElementType(),
8287330f729Sjoerg Vec2->getElementType()))
8297330f729Sjoerg return false;
8307330f729Sjoerg break;
8317330f729Sjoerg }
8327330f729Sjoerg
8337330f729Sjoerg case Type::Vector:
8347330f729Sjoerg case Type::ExtVector: {
8357330f729Sjoerg const auto *Vec1 = cast<VectorType>(T1);
8367330f729Sjoerg const auto *Vec2 = cast<VectorType>(T2);
8377330f729Sjoerg if (!IsStructurallyEquivalent(Context, Vec1->getElementType(),
8387330f729Sjoerg Vec2->getElementType()))
8397330f729Sjoerg return false;
8407330f729Sjoerg if (Vec1->getNumElements() != Vec2->getNumElements())
8417330f729Sjoerg return false;
8427330f729Sjoerg if (Vec1->getVectorKind() != Vec2->getVectorKind())
8437330f729Sjoerg return false;
8447330f729Sjoerg break;
8457330f729Sjoerg }
8467330f729Sjoerg
847*e038c9c4Sjoerg case Type::DependentSizedMatrix: {
848*e038c9c4Sjoerg const DependentSizedMatrixType *Mat1 = cast<DependentSizedMatrixType>(T1);
849*e038c9c4Sjoerg const DependentSizedMatrixType *Mat2 = cast<DependentSizedMatrixType>(T2);
850*e038c9c4Sjoerg // The element types, row and column expressions must be structurally
851*e038c9c4Sjoerg // equivalent.
852*e038c9c4Sjoerg if (!IsStructurallyEquivalent(Context, Mat1->getRowExpr(),
853*e038c9c4Sjoerg Mat2->getRowExpr()) ||
854*e038c9c4Sjoerg !IsStructurallyEquivalent(Context, Mat1->getColumnExpr(),
855*e038c9c4Sjoerg Mat2->getColumnExpr()) ||
856*e038c9c4Sjoerg !IsStructurallyEquivalent(Context, Mat1->getElementType(),
857*e038c9c4Sjoerg Mat2->getElementType()))
858*e038c9c4Sjoerg return false;
859*e038c9c4Sjoerg break;
860*e038c9c4Sjoerg }
861*e038c9c4Sjoerg
862*e038c9c4Sjoerg case Type::ConstantMatrix: {
863*e038c9c4Sjoerg const ConstantMatrixType *Mat1 = cast<ConstantMatrixType>(T1);
864*e038c9c4Sjoerg const ConstantMatrixType *Mat2 = cast<ConstantMatrixType>(T2);
865*e038c9c4Sjoerg // The element types must be structurally equivalent and the number of rows
866*e038c9c4Sjoerg // and columns must match.
867*e038c9c4Sjoerg if (!IsStructurallyEquivalent(Context, Mat1->getElementType(),
868*e038c9c4Sjoerg Mat2->getElementType()) ||
869*e038c9c4Sjoerg Mat1->getNumRows() != Mat2->getNumRows() ||
870*e038c9c4Sjoerg Mat1->getNumColumns() != Mat2->getNumColumns())
871*e038c9c4Sjoerg return false;
872*e038c9c4Sjoerg break;
873*e038c9c4Sjoerg }
874*e038c9c4Sjoerg
8757330f729Sjoerg case Type::FunctionProto: {
8767330f729Sjoerg const auto *Proto1 = cast<FunctionProtoType>(T1);
8777330f729Sjoerg const auto *Proto2 = cast<FunctionProtoType>(T2);
8787330f729Sjoerg
8797330f729Sjoerg if (Proto1->getNumParams() != Proto2->getNumParams())
8807330f729Sjoerg return false;
8817330f729Sjoerg for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) {
8827330f729Sjoerg if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I),
8837330f729Sjoerg Proto2->getParamType(I)))
8847330f729Sjoerg return false;
8857330f729Sjoerg }
8867330f729Sjoerg if (Proto1->isVariadic() != Proto2->isVariadic())
8877330f729Sjoerg return false;
8887330f729Sjoerg
8897330f729Sjoerg if (Proto1->getMethodQuals() != Proto2->getMethodQuals())
8907330f729Sjoerg return false;
8917330f729Sjoerg
8927330f729Sjoerg // Check exceptions, this information is lost in canonical type.
8937330f729Sjoerg const auto *OrigProto1 =
8947330f729Sjoerg cast<FunctionProtoType>(OrigT1.getDesugaredType(Context.FromCtx));
8957330f729Sjoerg const auto *OrigProto2 =
8967330f729Sjoerg cast<FunctionProtoType>(OrigT2.getDesugaredType(Context.ToCtx));
8977330f729Sjoerg if (!IsEquivalentExceptionSpec(Context, OrigProto1, OrigProto2))
8987330f729Sjoerg return false;
8997330f729Sjoerg
9007330f729Sjoerg // Fall through to check the bits common with FunctionNoProtoType.
9017330f729Sjoerg LLVM_FALLTHROUGH;
9027330f729Sjoerg }
9037330f729Sjoerg
9047330f729Sjoerg case Type::FunctionNoProto: {
9057330f729Sjoerg const auto *Function1 = cast<FunctionType>(T1);
9067330f729Sjoerg const auto *Function2 = cast<FunctionType>(T2);
9077330f729Sjoerg if (!IsStructurallyEquivalent(Context, Function1->getReturnType(),
9087330f729Sjoerg Function2->getReturnType()))
9097330f729Sjoerg return false;
9107330f729Sjoerg if (!IsStructurallyEquivalent(Context, Function1->getExtInfo(),
9117330f729Sjoerg Function2->getExtInfo()))
9127330f729Sjoerg return false;
9137330f729Sjoerg break;
9147330f729Sjoerg }
9157330f729Sjoerg
9167330f729Sjoerg case Type::UnresolvedUsing:
9177330f729Sjoerg if (!IsStructurallyEquivalent(Context,
9187330f729Sjoerg cast<UnresolvedUsingType>(T1)->getDecl(),
9197330f729Sjoerg cast<UnresolvedUsingType>(T2)->getDecl()))
9207330f729Sjoerg return false;
9217330f729Sjoerg break;
9227330f729Sjoerg
9237330f729Sjoerg case Type::Attributed:
9247330f729Sjoerg if (!IsStructurallyEquivalent(Context,
9257330f729Sjoerg cast<AttributedType>(T1)->getModifiedType(),
9267330f729Sjoerg cast<AttributedType>(T2)->getModifiedType()))
9277330f729Sjoerg return false;
9287330f729Sjoerg if (!IsStructurallyEquivalent(
9297330f729Sjoerg Context, cast<AttributedType>(T1)->getEquivalentType(),
9307330f729Sjoerg cast<AttributedType>(T2)->getEquivalentType()))
9317330f729Sjoerg return false;
9327330f729Sjoerg break;
9337330f729Sjoerg
9347330f729Sjoerg case Type::Paren:
9357330f729Sjoerg if (!IsStructurallyEquivalent(Context, cast<ParenType>(T1)->getInnerType(),
9367330f729Sjoerg cast<ParenType>(T2)->getInnerType()))
9377330f729Sjoerg return false;
9387330f729Sjoerg break;
9397330f729Sjoerg
9407330f729Sjoerg case Type::MacroQualified:
9417330f729Sjoerg if (!IsStructurallyEquivalent(
9427330f729Sjoerg Context, cast<MacroQualifiedType>(T1)->getUnderlyingType(),
9437330f729Sjoerg cast<MacroQualifiedType>(T2)->getUnderlyingType()))
9447330f729Sjoerg return false;
9457330f729Sjoerg break;
9467330f729Sjoerg
9477330f729Sjoerg case Type::Typedef:
9487330f729Sjoerg if (!IsStructurallyEquivalent(Context, cast<TypedefType>(T1)->getDecl(),
9497330f729Sjoerg cast<TypedefType>(T2)->getDecl()))
9507330f729Sjoerg return false;
9517330f729Sjoerg break;
9527330f729Sjoerg
9537330f729Sjoerg case Type::TypeOfExpr:
9547330f729Sjoerg if (!IsStructurallyEquivalent(
9557330f729Sjoerg Context, cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
9567330f729Sjoerg cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
9577330f729Sjoerg return false;
9587330f729Sjoerg break;
9597330f729Sjoerg
9607330f729Sjoerg case Type::TypeOf:
9617330f729Sjoerg if (!IsStructurallyEquivalent(Context,
9627330f729Sjoerg cast<TypeOfType>(T1)->getUnderlyingType(),
9637330f729Sjoerg cast<TypeOfType>(T2)->getUnderlyingType()))
9647330f729Sjoerg return false;
9657330f729Sjoerg break;
9667330f729Sjoerg
9677330f729Sjoerg case Type::UnaryTransform:
9687330f729Sjoerg if (!IsStructurallyEquivalent(
9697330f729Sjoerg Context, cast<UnaryTransformType>(T1)->getUnderlyingType(),
9707330f729Sjoerg cast<UnaryTransformType>(T2)->getUnderlyingType()))
9717330f729Sjoerg return false;
9727330f729Sjoerg break;
9737330f729Sjoerg
9747330f729Sjoerg case Type::Decltype:
9757330f729Sjoerg if (!IsStructurallyEquivalent(Context,
9767330f729Sjoerg cast<DecltypeType>(T1)->getUnderlyingExpr(),
9777330f729Sjoerg cast<DecltypeType>(T2)->getUnderlyingExpr()))
9787330f729Sjoerg return false;
9797330f729Sjoerg break;
9807330f729Sjoerg
981*e038c9c4Sjoerg case Type::Auto: {
982*e038c9c4Sjoerg auto *Auto1 = cast<AutoType>(T1);
983*e038c9c4Sjoerg auto *Auto2 = cast<AutoType>(T2);
984*e038c9c4Sjoerg if (!IsStructurallyEquivalent(Context, Auto1->getDeducedType(),
985*e038c9c4Sjoerg Auto2->getDeducedType()))
9867330f729Sjoerg return false;
987*e038c9c4Sjoerg if (Auto1->isConstrained() != Auto2->isConstrained())
988*e038c9c4Sjoerg return false;
989*e038c9c4Sjoerg if (Auto1->isConstrained()) {
990*e038c9c4Sjoerg if (Auto1->getTypeConstraintConcept() !=
991*e038c9c4Sjoerg Auto2->getTypeConstraintConcept())
992*e038c9c4Sjoerg return false;
993*e038c9c4Sjoerg ArrayRef<TemplateArgument> Auto1Args =
994*e038c9c4Sjoerg Auto1->getTypeConstraintArguments();
995*e038c9c4Sjoerg ArrayRef<TemplateArgument> Auto2Args =
996*e038c9c4Sjoerg Auto2->getTypeConstraintArguments();
997*e038c9c4Sjoerg if (Auto1Args.size() != Auto2Args.size())
998*e038c9c4Sjoerg return false;
999*e038c9c4Sjoerg for (unsigned I = 0, N = Auto1Args.size(); I != N; ++I) {
1000*e038c9c4Sjoerg if (!IsStructurallyEquivalent(Context, Auto1Args[I], Auto2Args[I]))
1001*e038c9c4Sjoerg return false;
1002*e038c9c4Sjoerg }
1003*e038c9c4Sjoerg }
10047330f729Sjoerg break;
1005*e038c9c4Sjoerg }
10067330f729Sjoerg
10077330f729Sjoerg case Type::DeducedTemplateSpecialization: {
10087330f729Sjoerg const auto *DT1 = cast<DeducedTemplateSpecializationType>(T1);
10097330f729Sjoerg const auto *DT2 = cast<DeducedTemplateSpecializationType>(T2);
10107330f729Sjoerg if (!IsStructurallyEquivalent(Context, DT1->getTemplateName(),
10117330f729Sjoerg DT2->getTemplateName()))
10127330f729Sjoerg return false;
10137330f729Sjoerg if (!IsStructurallyEquivalent(Context, DT1->getDeducedType(),
10147330f729Sjoerg DT2->getDeducedType()))
10157330f729Sjoerg return false;
10167330f729Sjoerg break;
10177330f729Sjoerg }
10187330f729Sjoerg
10197330f729Sjoerg case Type::Record:
10207330f729Sjoerg case Type::Enum:
10217330f729Sjoerg if (!IsStructurallyEquivalent(Context, cast<TagType>(T1)->getDecl(),
10227330f729Sjoerg cast<TagType>(T2)->getDecl()))
10237330f729Sjoerg return false;
10247330f729Sjoerg break;
10257330f729Sjoerg
10267330f729Sjoerg case Type::TemplateTypeParm: {
10277330f729Sjoerg const auto *Parm1 = cast<TemplateTypeParmType>(T1);
10287330f729Sjoerg const auto *Parm2 = cast<TemplateTypeParmType>(T2);
10297330f729Sjoerg if (Parm1->getDepth() != Parm2->getDepth())
10307330f729Sjoerg return false;
10317330f729Sjoerg if (Parm1->getIndex() != Parm2->getIndex())
10327330f729Sjoerg return false;
10337330f729Sjoerg if (Parm1->isParameterPack() != Parm2->isParameterPack())
10347330f729Sjoerg return false;
10357330f729Sjoerg
10367330f729Sjoerg // Names of template type parameters are never significant.
10377330f729Sjoerg break;
10387330f729Sjoerg }
10397330f729Sjoerg
10407330f729Sjoerg case Type::SubstTemplateTypeParm: {
10417330f729Sjoerg const auto *Subst1 = cast<SubstTemplateTypeParmType>(T1);
10427330f729Sjoerg const auto *Subst2 = cast<SubstTemplateTypeParmType>(T2);
10437330f729Sjoerg if (!IsStructurallyEquivalent(Context,
10447330f729Sjoerg QualType(Subst1->getReplacedParameter(), 0),
10457330f729Sjoerg QualType(Subst2->getReplacedParameter(), 0)))
10467330f729Sjoerg return false;
10477330f729Sjoerg if (!IsStructurallyEquivalent(Context, Subst1->getReplacementType(),
10487330f729Sjoerg Subst2->getReplacementType()))
10497330f729Sjoerg return false;
10507330f729Sjoerg break;
10517330f729Sjoerg }
10527330f729Sjoerg
10537330f729Sjoerg case Type::SubstTemplateTypeParmPack: {
10547330f729Sjoerg const auto *Subst1 = cast<SubstTemplateTypeParmPackType>(T1);
10557330f729Sjoerg const auto *Subst2 = cast<SubstTemplateTypeParmPackType>(T2);
10567330f729Sjoerg if (!IsStructurallyEquivalent(Context,
10577330f729Sjoerg QualType(Subst1->getReplacedParameter(), 0),
10587330f729Sjoerg QualType(Subst2->getReplacedParameter(), 0)))
10597330f729Sjoerg return false;
10607330f729Sjoerg if (!IsStructurallyEquivalent(Context, Subst1->getArgumentPack(),
10617330f729Sjoerg Subst2->getArgumentPack()))
10627330f729Sjoerg return false;
10637330f729Sjoerg break;
10647330f729Sjoerg }
10657330f729Sjoerg
10667330f729Sjoerg case Type::TemplateSpecialization: {
10677330f729Sjoerg const auto *Spec1 = cast<TemplateSpecializationType>(T1);
10687330f729Sjoerg const auto *Spec2 = cast<TemplateSpecializationType>(T2);
10697330f729Sjoerg if (!IsStructurallyEquivalent(Context, Spec1->getTemplateName(),
10707330f729Sjoerg Spec2->getTemplateName()))
10717330f729Sjoerg return false;
10727330f729Sjoerg if (Spec1->getNumArgs() != Spec2->getNumArgs())
10737330f729Sjoerg return false;
10747330f729Sjoerg for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
10757330f729Sjoerg if (!IsStructurallyEquivalent(Context, Spec1->getArg(I),
10767330f729Sjoerg Spec2->getArg(I)))
10777330f729Sjoerg return false;
10787330f729Sjoerg }
10797330f729Sjoerg break;
10807330f729Sjoerg }
10817330f729Sjoerg
10827330f729Sjoerg case Type::Elaborated: {
10837330f729Sjoerg const auto *Elab1 = cast<ElaboratedType>(T1);
10847330f729Sjoerg const auto *Elab2 = cast<ElaboratedType>(T2);
10857330f729Sjoerg // CHECKME: what if a keyword is ETK_None or ETK_typename ?
10867330f729Sjoerg if (Elab1->getKeyword() != Elab2->getKeyword())
10877330f729Sjoerg return false;
10887330f729Sjoerg if (!IsStructurallyEquivalent(Context, Elab1->getQualifier(),
10897330f729Sjoerg Elab2->getQualifier()))
10907330f729Sjoerg return false;
10917330f729Sjoerg if (!IsStructurallyEquivalent(Context, Elab1->getNamedType(),
10927330f729Sjoerg Elab2->getNamedType()))
10937330f729Sjoerg return false;
10947330f729Sjoerg break;
10957330f729Sjoerg }
10967330f729Sjoerg
10977330f729Sjoerg case Type::InjectedClassName: {
10987330f729Sjoerg const auto *Inj1 = cast<InjectedClassNameType>(T1);
10997330f729Sjoerg const auto *Inj2 = cast<InjectedClassNameType>(T2);
11007330f729Sjoerg if (!IsStructurallyEquivalent(Context,
11017330f729Sjoerg Inj1->getInjectedSpecializationType(),
11027330f729Sjoerg Inj2->getInjectedSpecializationType()))
11037330f729Sjoerg return false;
11047330f729Sjoerg break;
11057330f729Sjoerg }
11067330f729Sjoerg
11077330f729Sjoerg case Type::DependentName: {
11087330f729Sjoerg const auto *Typename1 = cast<DependentNameType>(T1);
11097330f729Sjoerg const auto *Typename2 = cast<DependentNameType>(T2);
11107330f729Sjoerg if (!IsStructurallyEquivalent(Context, Typename1->getQualifier(),
11117330f729Sjoerg Typename2->getQualifier()))
11127330f729Sjoerg return false;
11137330f729Sjoerg if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
11147330f729Sjoerg Typename2->getIdentifier()))
11157330f729Sjoerg return false;
11167330f729Sjoerg
11177330f729Sjoerg break;
11187330f729Sjoerg }
11197330f729Sjoerg
11207330f729Sjoerg case Type::DependentTemplateSpecialization: {
11217330f729Sjoerg const auto *Spec1 = cast<DependentTemplateSpecializationType>(T1);
11227330f729Sjoerg const auto *Spec2 = cast<DependentTemplateSpecializationType>(T2);
11237330f729Sjoerg if (!IsStructurallyEquivalent(Context, Spec1->getQualifier(),
11247330f729Sjoerg Spec2->getQualifier()))
11257330f729Sjoerg return false;
11267330f729Sjoerg if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
11277330f729Sjoerg Spec2->getIdentifier()))
11287330f729Sjoerg return false;
11297330f729Sjoerg if (Spec1->getNumArgs() != Spec2->getNumArgs())
11307330f729Sjoerg return false;
11317330f729Sjoerg for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
11327330f729Sjoerg if (!IsStructurallyEquivalent(Context, Spec1->getArg(I),
11337330f729Sjoerg Spec2->getArg(I)))
11347330f729Sjoerg return false;
11357330f729Sjoerg }
11367330f729Sjoerg break;
11377330f729Sjoerg }
11387330f729Sjoerg
11397330f729Sjoerg case Type::PackExpansion:
11407330f729Sjoerg if (!IsStructurallyEquivalent(Context,
11417330f729Sjoerg cast<PackExpansionType>(T1)->getPattern(),
11427330f729Sjoerg cast<PackExpansionType>(T2)->getPattern()))
11437330f729Sjoerg return false;
11447330f729Sjoerg break;
11457330f729Sjoerg
11467330f729Sjoerg case Type::ObjCInterface: {
11477330f729Sjoerg const auto *Iface1 = cast<ObjCInterfaceType>(T1);
11487330f729Sjoerg const auto *Iface2 = cast<ObjCInterfaceType>(T2);
11497330f729Sjoerg if (!IsStructurallyEquivalent(Context, Iface1->getDecl(),
11507330f729Sjoerg Iface2->getDecl()))
11517330f729Sjoerg return false;
11527330f729Sjoerg break;
11537330f729Sjoerg }
11547330f729Sjoerg
11557330f729Sjoerg case Type::ObjCTypeParam: {
11567330f729Sjoerg const auto *Obj1 = cast<ObjCTypeParamType>(T1);
11577330f729Sjoerg const auto *Obj2 = cast<ObjCTypeParamType>(T2);
11587330f729Sjoerg if (!IsStructurallyEquivalent(Context, Obj1->getDecl(), Obj2->getDecl()))
11597330f729Sjoerg return false;
11607330f729Sjoerg
11617330f729Sjoerg if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
11627330f729Sjoerg return false;
11637330f729Sjoerg for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
11647330f729Sjoerg if (!IsStructurallyEquivalent(Context, Obj1->getProtocol(I),
11657330f729Sjoerg Obj2->getProtocol(I)))
11667330f729Sjoerg return false;
11677330f729Sjoerg }
11687330f729Sjoerg break;
11697330f729Sjoerg }
11707330f729Sjoerg
11717330f729Sjoerg case Type::ObjCObject: {
11727330f729Sjoerg const auto *Obj1 = cast<ObjCObjectType>(T1);
11737330f729Sjoerg const auto *Obj2 = cast<ObjCObjectType>(T2);
11747330f729Sjoerg if (!IsStructurallyEquivalent(Context, Obj1->getBaseType(),
11757330f729Sjoerg Obj2->getBaseType()))
11767330f729Sjoerg return false;
11777330f729Sjoerg if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
11787330f729Sjoerg return false;
11797330f729Sjoerg for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
11807330f729Sjoerg if (!IsStructurallyEquivalent(Context, Obj1->getProtocol(I),
11817330f729Sjoerg Obj2->getProtocol(I)))
11827330f729Sjoerg return false;
11837330f729Sjoerg }
11847330f729Sjoerg break;
11857330f729Sjoerg }
11867330f729Sjoerg
11877330f729Sjoerg case Type::ObjCObjectPointer: {
11887330f729Sjoerg const auto *Ptr1 = cast<ObjCObjectPointerType>(T1);
11897330f729Sjoerg const auto *Ptr2 = cast<ObjCObjectPointerType>(T2);
11907330f729Sjoerg if (!IsStructurallyEquivalent(Context, Ptr1->getPointeeType(),
11917330f729Sjoerg Ptr2->getPointeeType()))
11927330f729Sjoerg return false;
11937330f729Sjoerg break;
11947330f729Sjoerg }
11957330f729Sjoerg
11967330f729Sjoerg case Type::Atomic:
11977330f729Sjoerg if (!IsStructurallyEquivalent(Context, cast<AtomicType>(T1)->getValueType(),
11987330f729Sjoerg cast<AtomicType>(T2)->getValueType()))
11997330f729Sjoerg return false;
12007330f729Sjoerg break;
12017330f729Sjoerg
12027330f729Sjoerg case Type::Pipe:
12037330f729Sjoerg if (!IsStructurallyEquivalent(Context, cast<PipeType>(T1)->getElementType(),
12047330f729Sjoerg cast<PipeType>(T2)->getElementType()))
12057330f729Sjoerg return false;
12067330f729Sjoerg break;
1207*e038c9c4Sjoerg case Type::ExtInt: {
1208*e038c9c4Sjoerg const auto *Int1 = cast<ExtIntType>(T1);
1209*e038c9c4Sjoerg const auto *Int2 = cast<ExtIntType>(T2);
1210*e038c9c4Sjoerg
1211*e038c9c4Sjoerg if (Int1->isUnsigned() != Int2->isUnsigned() ||
1212*e038c9c4Sjoerg Int1->getNumBits() != Int2->getNumBits())
1213*e038c9c4Sjoerg return false;
1214*e038c9c4Sjoerg break;
1215*e038c9c4Sjoerg }
1216*e038c9c4Sjoerg case Type::DependentExtInt: {
1217*e038c9c4Sjoerg const auto *Int1 = cast<DependentExtIntType>(T1);
1218*e038c9c4Sjoerg const auto *Int2 = cast<DependentExtIntType>(T2);
1219*e038c9c4Sjoerg
1220*e038c9c4Sjoerg if (Int1->isUnsigned() != Int2->isUnsigned() ||
1221*e038c9c4Sjoerg !IsStructurallyEquivalent(Context, Int1->getNumBitsExpr(),
1222*e038c9c4Sjoerg Int2->getNumBitsExpr()))
1223*e038c9c4Sjoerg return false;
1224*e038c9c4Sjoerg }
12257330f729Sjoerg } // end switch
12267330f729Sjoerg
12277330f729Sjoerg return true;
12287330f729Sjoerg }
12297330f729Sjoerg
12307330f729Sjoerg /// Determine structural equivalence of two fields.
IsStructurallyEquivalent(StructuralEquivalenceContext & Context,FieldDecl * Field1,FieldDecl * Field2)12317330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
12327330f729Sjoerg FieldDecl *Field1, FieldDecl *Field2) {
12337330f729Sjoerg const auto *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
12347330f729Sjoerg
12357330f729Sjoerg // For anonymous structs/unions, match up the anonymous struct/union type
12367330f729Sjoerg // declarations directly, so that we don't go off searching for anonymous
12377330f729Sjoerg // types
12387330f729Sjoerg if (Field1->isAnonymousStructOrUnion() &&
12397330f729Sjoerg Field2->isAnonymousStructOrUnion()) {
12407330f729Sjoerg RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
12417330f729Sjoerg RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
12427330f729Sjoerg return IsStructurallyEquivalent(Context, D1, D2);
12437330f729Sjoerg }
12447330f729Sjoerg
12457330f729Sjoerg // Check for equivalent field names.
12467330f729Sjoerg IdentifierInfo *Name1 = Field1->getIdentifier();
12477330f729Sjoerg IdentifierInfo *Name2 = Field2->getIdentifier();
12487330f729Sjoerg if (!::IsStructurallyEquivalent(Name1, Name2)) {
12497330f729Sjoerg if (Context.Complain) {
12507330f729Sjoerg Context.Diag2(
12517330f729Sjoerg Owner2->getLocation(),
12527330f729Sjoerg Context.getApplicableDiagnostic(diag::err_odr_tag_type_inconsistent))
12537330f729Sjoerg << Context.ToCtx.getTypeDeclType(Owner2);
12547330f729Sjoerg Context.Diag2(Field2->getLocation(), diag::note_odr_field_name)
12557330f729Sjoerg << Field2->getDeclName();
12567330f729Sjoerg Context.Diag1(Field1->getLocation(), diag::note_odr_field_name)
12577330f729Sjoerg << Field1->getDeclName();
12587330f729Sjoerg }
12597330f729Sjoerg return false;
12607330f729Sjoerg }
12617330f729Sjoerg
12627330f729Sjoerg if (!IsStructurallyEquivalent(Context, Field1->getType(),
12637330f729Sjoerg Field2->getType())) {
12647330f729Sjoerg if (Context.Complain) {
12657330f729Sjoerg Context.Diag2(
12667330f729Sjoerg Owner2->getLocation(),
12677330f729Sjoerg Context.getApplicableDiagnostic(diag::err_odr_tag_type_inconsistent))
12687330f729Sjoerg << Context.ToCtx.getTypeDeclType(Owner2);
12697330f729Sjoerg Context.Diag2(Field2->getLocation(), diag::note_odr_field)
12707330f729Sjoerg << Field2->getDeclName() << Field2->getType();
12717330f729Sjoerg Context.Diag1(Field1->getLocation(), diag::note_odr_field)
12727330f729Sjoerg << Field1->getDeclName() << Field1->getType();
12737330f729Sjoerg }
12747330f729Sjoerg return false;
12757330f729Sjoerg }
12767330f729Sjoerg
1277*e038c9c4Sjoerg if (Field1->isBitField())
1278*e038c9c4Sjoerg return IsStructurallyEquivalent(Context, Field1->getBitWidth(),
1279*e038c9c4Sjoerg Field2->getBitWidth());
12807330f729Sjoerg
12817330f729Sjoerg return true;
12827330f729Sjoerg }
12837330f729Sjoerg
12847330f729Sjoerg /// Determine structural equivalence of two methods.
IsStructurallyEquivalent(StructuralEquivalenceContext & Context,CXXMethodDecl * Method1,CXXMethodDecl * Method2)12857330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
12867330f729Sjoerg CXXMethodDecl *Method1,
12877330f729Sjoerg CXXMethodDecl *Method2) {
12887330f729Sjoerg bool PropertiesEqual =
12897330f729Sjoerg Method1->getDeclKind() == Method2->getDeclKind() &&
12907330f729Sjoerg Method1->getRefQualifier() == Method2->getRefQualifier() &&
12917330f729Sjoerg Method1->getAccess() == Method2->getAccess() &&
12927330f729Sjoerg Method1->getOverloadedOperator() == Method2->getOverloadedOperator() &&
12937330f729Sjoerg Method1->isStatic() == Method2->isStatic() &&
12947330f729Sjoerg Method1->isConst() == Method2->isConst() &&
12957330f729Sjoerg Method1->isVolatile() == Method2->isVolatile() &&
12967330f729Sjoerg Method1->isVirtual() == Method2->isVirtual() &&
12977330f729Sjoerg Method1->isPure() == Method2->isPure() &&
12987330f729Sjoerg Method1->isDefaulted() == Method2->isDefaulted() &&
12997330f729Sjoerg Method1->isDeleted() == Method2->isDeleted();
13007330f729Sjoerg if (!PropertiesEqual)
13017330f729Sjoerg return false;
13027330f729Sjoerg // FIXME: Check for 'final'.
13037330f729Sjoerg
13047330f729Sjoerg if (auto *Constructor1 = dyn_cast<CXXConstructorDecl>(Method1)) {
13057330f729Sjoerg auto *Constructor2 = cast<CXXConstructorDecl>(Method2);
13067330f729Sjoerg if (!Constructor1->getExplicitSpecifier().isEquivalent(
13077330f729Sjoerg Constructor2->getExplicitSpecifier()))
13087330f729Sjoerg return false;
13097330f729Sjoerg }
13107330f729Sjoerg
13117330f729Sjoerg if (auto *Conversion1 = dyn_cast<CXXConversionDecl>(Method1)) {
13127330f729Sjoerg auto *Conversion2 = cast<CXXConversionDecl>(Method2);
13137330f729Sjoerg if (!Conversion1->getExplicitSpecifier().isEquivalent(
13147330f729Sjoerg Conversion2->getExplicitSpecifier()))
13157330f729Sjoerg return false;
13167330f729Sjoerg if (!IsStructurallyEquivalent(Context, Conversion1->getConversionType(),
13177330f729Sjoerg Conversion2->getConversionType()))
13187330f729Sjoerg return false;
13197330f729Sjoerg }
13207330f729Sjoerg
13217330f729Sjoerg const IdentifierInfo *Name1 = Method1->getIdentifier();
13227330f729Sjoerg const IdentifierInfo *Name2 = Method2->getIdentifier();
13237330f729Sjoerg if (!::IsStructurallyEquivalent(Name1, Name2)) {
13247330f729Sjoerg return false;
13257330f729Sjoerg // TODO: Names do not match, add warning like at check for FieldDecl.
13267330f729Sjoerg }
13277330f729Sjoerg
13287330f729Sjoerg // Check the prototypes.
13297330f729Sjoerg if (!::IsStructurallyEquivalent(Context,
13307330f729Sjoerg Method1->getType(), Method2->getType()))
13317330f729Sjoerg return false;
13327330f729Sjoerg
13337330f729Sjoerg return true;
13347330f729Sjoerg }
13357330f729Sjoerg
13367330f729Sjoerg /// Determine structural equivalence of two lambda classes.
13377330f729Sjoerg static bool
IsStructurallyEquivalentLambdas(StructuralEquivalenceContext & Context,CXXRecordDecl * D1,CXXRecordDecl * D2)13387330f729Sjoerg IsStructurallyEquivalentLambdas(StructuralEquivalenceContext &Context,
13397330f729Sjoerg CXXRecordDecl *D1, CXXRecordDecl *D2) {
13407330f729Sjoerg assert(D1->isLambda() && D2->isLambda() &&
13417330f729Sjoerg "Must be called on lambda classes");
13427330f729Sjoerg if (!IsStructurallyEquivalent(Context, D1->getLambdaCallOperator(),
13437330f729Sjoerg D2->getLambdaCallOperator()))
13447330f729Sjoerg return false;
13457330f729Sjoerg
13467330f729Sjoerg return true;
13477330f729Sjoerg }
13487330f729Sjoerg
13497330f729Sjoerg /// Determine structural equivalence of two records.
IsStructurallyEquivalent(StructuralEquivalenceContext & Context,RecordDecl * D1,RecordDecl * D2)13507330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
13517330f729Sjoerg RecordDecl *D1, RecordDecl *D2) {
1352*e038c9c4Sjoerg
1353*e038c9c4Sjoerg // Check for equivalent structure names.
1354*e038c9c4Sjoerg IdentifierInfo *Name1 = D1->getIdentifier();
1355*e038c9c4Sjoerg if (!Name1 && D1->getTypedefNameForAnonDecl())
1356*e038c9c4Sjoerg Name1 = D1->getTypedefNameForAnonDecl()->getIdentifier();
1357*e038c9c4Sjoerg IdentifierInfo *Name2 = D2->getIdentifier();
1358*e038c9c4Sjoerg if (!Name2 && D2->getTypedefNameForAnonDecl())
1359*e038c9c4Sjoerg Name2 = D2->getTypedefNameForAnonDecl()->getIdentifier();
1360*e038c9c4Sjoerg if (!IsStructurallyEquivalent(Name1, Name2))
1361*e038c9c4Sjoerg return false;
1362*e038c9c4Sjoerg
13637330f729Sjoerg if (D1->isUnion() != D2->isUnion()) {
13647330f729Sjoerg if (Context.Complain) {
13657330f729Sjoerg Context.Diag2(D2->getLocation(), Context.getApplicableDiagnostic(
13667330f729Sjoerg diag::err_odr_tag_type_inconsistent))
13677330f729Sjoerg << Context.ToCtx.getTypeDeclType(D2);
13687330f729Sjoerg Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
13697330f729Sjoerg << D1->getDeclName() << (unsigned)D1->getTagKind();
13707330f729Sjoerg }
13717330f729Sjoerg return false;
13727330f729Sjoerg }
13737330f729Sjoerg
13747330f729Sjoerg if (!D1->getDeclName() && !D2->getDeclName()) {
13757330f729Sjoerg // If both anonymous structs/unions are in a record context, make sure
13767330f729Sjoerg // they occur in the same location in the context records.
13777330f729Sjoerg if (Optional<unsigned> Index1 =
13787330f729Sjoerg StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(D1)) {
13797330f729Sjoerg if (Optional<unsigned> Index2 =
13807330f729Sjoerg StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(
13817330f729Sjoerg D2)) {
13827330f729Sjoerg if (*Index1 != *Index2)
13837330f729Sjoerg return false;
13847330f729Sjoerg }
13857330f729Sjoerg }
13867330f729Sjoerg }
13877330f729Sjoerg
13887330f729Sjoerg // If both declarations are class template specializations, we know
13897330f729Sjoerg // the ODR applies, so check the template and template arguments.
13907330f729Sjoerg const auto *Spec1 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
13917330f729Sjoerg const auto *Spec2 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
13927330f729Sjoerg if (Spec1 && Spec2) {
13937330f729Sjoerg // Check that the specialized templates are the same.
13947330f729Sjoerg if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
13957330f729Sjoerg Spec2->getSpecializedTemplate()))
13967330f729Sjoerg return false;
13977330f729Sjoerg
13987330f729Sjoerg // Check that the template arguments are the same.
13997330f729Sjoerg if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
14007330f729Sjoerg return false;
14017330f729Sjoerg
14027330f729Sjoerg for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
14037330f729Sjoerg if (!IsStructurallyEquivalent(Context, Spec1->getTemplateArgs().get(I),
14047330f729Sjoerg Spec2->getTemplateArgs().get(I)))
14057330f729Sjoerg return false;
14067330f729Sjoerg }
14077330f729Sjoerg // If one is a class template specialization and the other is not, these
14087330f729Sjoerg // structures are different.
14097330f729Sjoerg else if (Spec1 || Spec2)
14107330f729Sjoerg return false;
14117330f729Sjoerg
14127330f729Sjoerg // Compare the definitions of these two records. If either or both are
14137330f729Sjoerg // incomplete (i.e. it is a forward decl), we assume that they are
14147330f729Sjoerg // equivalent.
14157330f729Sjoerg D1 = D1->getDefinition();
14167330f729Sjoerg D2 = D2->getDefinition();
14177330f729Sjoerg if (!D1 || !D2)
14187330f729Sjoerg return true;
14197330f729Sjoerg
14207330f729Sjoerg // If any of the records has external storage and we do a minimal check (or
14217330f729Sjoerg // AST import) we assume they are equivalent. (If we didn't have this
14227330f729Sjoerg // assumption then `RecordDecl::LoadFieldsFromExternalStorage` could trigger
14237330f729Sjoerg // another AST import which in turn would call the structural equivalency
14247330f729Sjoerg // check again and finally we'd have an improper result.)
14257330f729Sjoerg if (Context.EqKind == StructuralEquivalenceKind::Minimal)
14267330f729Sjoerg if (D1->hasExternalLexicalStorage() || D2->hasExternalLexicalStorage())
14277330f729Sjoerg return true;
14287330f729Sjoerg
14297330f729Sjoerg // If one definition is currently being defined, we do not compare for
14307330f729Sjoerg // equality and we assume that the decls are equal.
14317330f729Sjoerg if (D1->isBeingDefined() || D2->isBeingDefined())
14327330f729Sjoerg return true;
14337330f729Sjoerg
14347330f729Sjoerg if (auto *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
14357330f729Sjoerg if (auto *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
14367330f729Sjoerg if (D1CXX->hasExternalLexicalStorage() &&
14377330f729Sjoerg !D1CXX->isCompleteDefinition()) {
14387330f729Sjoerg D1CXX->getASTContext().getExternalSource()->CompleteType(D1CXX);
14397330f729Sjoerg }
14407330f729Sjoerg
14417330f729Sjoerg if (D1CXX->isLambda() != D2CXX->isLambda())
14427330f729Sjoerg return false;
14437330f729Sjoerg if (D1CXX->isLambda()) {
14447330f729Sjoerg if (!IsStructurallyEquivalentLambdas(Context, D1CXX, D2CXX))
14457330f729Sjoerg return false;
14467330f729Sjoerg }
14477330f729Sjoerg
14487330f729Sjoerg if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
14497330f729Sjoerg if (Context.Complain) {
14507330f729Sjoerg Context.Diag2(D2->getLocation(),
14517330f729Sjoerg Context.getApplicableDiagnostic(
14527330f729Sjoerg diag::err_odr_tag_type_inconsistent))
14537330f729Sjoerg << Context.ToCtx.getTypeDeclType(D2);
14547330f729Sjoerg Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
14557330f729Sjoerg << D2CXX->getNumBases();
14567330f729Sjoerg Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
14577330f729Sjoerg << D1CXX->getNumBases();
14587330f729Sjoerg }
14597330f729Sjoerg return false;
14607330f729Sjoerg }
14617330f729Sjoerg
14627330f729Sjoerg // Check the base classes.
14637330f729Sjoerg for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
14647330f729Sjoerg BaseEnd1 = D1CXX->bases_end(),
14657330f729Sjoerg Base2 = D2CXX->bases_begin();
14667330f729Sjoerg Base1 != BaseEnd1; ++Base1, ++Base2) {
14677330f729Sjoerg if (!IsStructurallyEquivalent(Context, Base1->getType(),
14687330f729Sjoerg Base2->getType())) {
14697330f729Sjoerg if (Context.Complain) {
14707330f729Sjoerg Context.Diag2(D2->getLocation(),
14717330f729Sjoerg Context.getApplicableDiagnostic(
14727330f729Sjoerg diag::err_odr_tag_type_inconsistent))
14737330f729Sjoerg << Context.ToCtx.getTypeDeclType(D2);
14747330f729Sjoerg Context.Diag2(Base2->getBeginLoc(), diag::note_odr_base)
14757330f729Sjoerg << Base2->getType() << Base2->getSourceRange();
14767330f729Sjoerg Context.Diag1(Base1->getBeginLoc(), diag::note_odr_base)
14777330f729Sjoerg << Base1->getType() << Base1->getSourceRange();
14787330f729Sjoerg }
14797330f729Sjoerg return false;
14807330f729Sjoerg }
14817330f729Sjoerg
14827330f729Sjoerg // Check virtual vs. non-virtual inheritance mismatch.
14837330f729Sjoerg if (Base1->isVirtual() != Base2->isVirtual()) {
14847330f729Sjoerg if (Context.Complain) {
14857330f729Sjoerg Context.Diag2(D2->getLocation(),
14867330f729Sjoerg Context.getApplicableDiagnostic(
14877330f729Sjoerg diag::err_odr_tag_type_inconsistent))
14887330f729Sjoerg << Context.ToCtx.getTypeDeclType(D2);
14897330f729Sjoerg Context.Diag2(Base2->getBeginLoc(), diag::note_odr_virtual_base)
14907330f729Sjoerg << Base2->isVirtual() << Base2->getSourceRange();
14917330f729Sjoerg Context.Diag1(Base1->getBeginLoc(), diag::note_odr_base)
14927330f729Sjoerg << Base1->isVirtual() << Base1->getSourceRange();
14937330f729Sjoerg }
14947330f729Sjoerg return false;
14957330f729Sjoerg }
14967330f729Sjoerg }
14977330f729Sjoerg
14987330f729Sjoerg // Check the friends for consistency.
14997330f729Sjoerg CXXRecordDecl::friend_iterator Friend2 = D2CXX->friend_begin(),
15007330f729Sjoerg Friend2End = D2CXX->friend_end();
15017330f729Sjoerg for (CXXRecordDecl::friend_iterator Friend1 = D1CXX->friend_begin(),
15027330f729Sjoerg Friend1End = D1CXX->friend_end();
15037330f729Sjoerg Friend1 != Friend1End; ++Friend1, ++Friend2) {
15047330f729Sjoerg if (Friend2 == Friend2End) {
15057330f729Sjoerg if (Context.Complain) {
15067330f729Sjoerg Context.Diag2(D2->getLocation(),
15077330f729Sjoerg Context.getApplicableDiagnostic(
15087330f729Sjoerg diag::err_odr_tag_type_inconsistent))
15097330f729Sjoerg << Context.ToCtx.getTypeDeclType(D2CXX);
15107330f729Sjoerg Context.Diag1((*Friend1)->getFriendLoc(), diag::note_odr_friend);
15117330f729Sjoerg Context.Diag2(D2->getLocation(), diag::note_odr_missing_friend);
15127330f729Sjoerg }
15137330f729Sjoerg return false;
15147330f729Sjoerg }
15157330f729Sjoerg
15167330f729Sjoerg if (!IsStructurallyEquivalent(Context, *Friend1, *Friend2)) {
15177330f729Sjoerg if (Context.Complain) {
15187330f729Sjoerg Context.Diag2(D2->getLocation(),
15197330f729Sjoerg Context.getApplicableDiagnostic(
15207330f729Sjoerg diag::err_odr_tag_type_inconsistent))
15217330f729Sjoerg << Context.ToCtx.getTypeDeclType(D2CXX);
15227330f729Sjoerg Context.Diag1((*Friend1)->getFriendLoc(), diag::note_odr_friend);
15237330f729Sjoerg Context.Diag2((*Friend2)->getFriendLoc(), diag::note_odr_friend);
15247330f729Sjoerg }
15257330f729Sjoerg return false;
15267330f729Sjoerg }
15277330f729Sjoerg }
15287330f729Sjoerg
15297330f729Sjoerg if (Friend2 != Friend2End) {
15307330f729Sjoerg if (Context.Complain) {
15317330f729Sjoerg Context.Diag2(D2->getLocation(),
15327330f729Sjoerg Context.getApplicableDiagnostic(
15337330f729Sjoerg diag::err_odr_tag_type_inconsistent))
15347330f729Sjoerg << Context.ToCtx.getTypeDeclType(D2);
15357330f729Sjoerg Context.Diag2((*Friend2)->getFriendLoc(), diag::note_odr_friend);
15367330f729Sjoerg Context.Diag1(D1->getLocation(), diag::note_odr_missing_friend);
15377330f729Sjoerg }
15387330f729Sjoerg return false;
15397330f729Sjoerg }
15407330f729Sjoerg } else if (D1CXX->getNumBases() > 0) {
15417330f729Sjoerg if (Context.Complain) {
15427330f729Sjoerg Context.Diag2(D2->getLocation(),
15437330f729Sjoerg Context.getApplicableDiagnostic(
15447330f729Sjoerg diag::err_odr_tag_type_inconsistent))
15457330f729Sjoerg << Context.ToCtx.getTypeDeclType(D2);
15467330f729Sjoerg const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
15477330f729Sjoerg Context.Diag1(Base1->getBeginLoc(), diag::note_odr_base)
15487330f729Sjoerg << Base1->getType() << Base1->getSourceRange();
15497330f729Sjoerg Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
15507330f729Sjoerg }
15517330f729Sjoerg return false;
15527330f729Sjoerg }
15537330f729Sjoerg }
15547330f729Sjoerg
15557330f729Sjoerg // Check the fields for consistency.
15567330f729Sjoerg RecordDecl::field_iterator Field2 = D2->field_begin(),
15577330f729Sjoerg Field2End = D2->field_end();
15587330f729Sjoerg for (RecordDecl::field_iterator Field1 = D1->field_begin(),
15597330f729Sjoerg Field1End = D1->field_end();
15607330f729Sjoerg Field1 != Field1End; ++Field1, ++Field2) {
15617330f729Sjoerg if (Field2 == Field2End) {
15627330f729Sjoerg if (Context.Complain) {
15637330f729Sjoerg Context.Diag2(D2->getLocation(),
15647330f729Sjoerg Context.getApplicableDiagnostic(
15657330f729Sjoerg diag::err_odr_tag_type_inconsistent))
15667330f729Sjoerg << Context.ToCtx.getTypeDeclType(D2);
15677330f729Sjoerg Context.Diag1(Field1->getLocation(), diag::note_odr_field)
15687330f729Sjoerg << Field1->getDeclName() << Field1->getType();
15697330f729Sjoerg Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
15707330f729Sjoerg }
15717330f729Sjoerg return false;
15727330f729Sjoerg }
15737330f729Sjoerg
15747330f729Sjoerg if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
15757330f729Sjoerg return false;
15767330f729Sjoerg }
15777330f729Sjoerg
15787330f729Sjoerg if (Field2 != Field2End) {
15797330f729Sjoerg if (Context.Complain) {
15807330f729Sjoerg Context.Diag2(D2->getLocation(), Context.getApplicableDiagnostic(
15817330f729Sjoerg diag::err_odr_tag_type_inconsistent))
15827330f729Sjoerg << Context.ToCtx.getTypeDeclType(D2);
15837330f729Sjoerg Context.Diag2(Field2->getLocation(), diag::note_odr_field)
15847330f729Sjoerg << Field2->getDeclName() << Field2->getType();
15857330f729Sjoerg Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
15867330f729Sjoerg }
15877330f729Sjoerg return false;
15887330f729Sjoerg }
15897330f729Sjoerg
15907330f729Sjoerg return true;
15917330f729Sjoerg }
15927330f729Sjoerg
15937330f729Sjoerg /// Determine structural equivalence of two enums.
IsStructurallyEquivalent(StructuralEquivalenceContext & Context,EnumDecl * D1,EnumDecl * D2)15947330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
15957330f729Sjoerg EnumDecl *D1, EnumDecl *D2) {
15967330f729Sjoerg
1597*e038c9c4Sjoerg // Check for equivalent enum names.
1598*e038c9c4Sjoerg IdentifierInfo *Name1 = D1->getIdentifier();
1599*e038c9c4Sjoerg if (!Name1 && D1->getTypedefNameForAnonDecl())
1600*e038c9c4Sjoerg Name1 = D1->getTypedefNameForAnonDecl()->getIdentifier();
1601*e038c9c4Sjoerg IdentifierInfo *Name2 = D2->getIdentifier();
1602*e038c9c4Sjoerg if (!Name2 && D2->getTypedefNameForAnonDecl())
1603*e038c9c4Sjoerg Name2 = D2->getTypedefNameForAnonDecl()->getIdentifier();
1604*e038c9c4Sjoerg if (!IsStructurallyEquivalent(Name1, Name2))
1605*e038c9c4Sjoerg return false;
1606*e038c9c4Sjoerg
16077330f729Sjoerg // Compare the definitions of these two enums. If either or both are
16087330f729Sjoerg // incomplete (i.e. forward declared), we assume that they are equivalent.
16097330f729Sjoerg D1 = D1->getDefinition();
16107330f729Sjoerg D2 = D2->getDefinition();
16117330f729Sjoerg if (!D1 || !D2)
16127330f729Sjoerg return true;
16137330f729Sjoerg
16147330f729Sjoerg EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
16157330f729Sjoerg EC2End = D2->enumerator_end();
16167330f729Sjoerg for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
16177330f729Sjoerg EC1End = D1->enumerator_end();
16187330f729Sjoerg EC1 != EC1End; ++EC1, ++EC2) {
16197330f729Sjoerg if (EC2 == EC2End) {
16207330f729Sjoerg if (Context.Complain) {
16217330f729Sjoerg Context.Diag2(D2->getLocation(),
16227330f729Sjoerg Context.getApplicableDiagnostic(
16237330f729Sjoerg diag::err_odr_tag_type_inconsistent))
16247330f729Sjoerg << Context.ToCtx.getTypeDeclType(D2);
16257330f729Sjoerg Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
16267330f729Sjoerg << EC1->getDeclName() << EC1->getInitVal().toString(10);
16277330f729Sjoerg Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
16287330f729Sjoerg }
16297330f729Sjoerg return false;
16307330f729Sjoerg }
16317330f729Sjoerg
16327330f729Sjoerg llvm::APSInt Val1 = EC1->getInitVal();
16337330f729Sjoerg llvm::APSInt Val2 = EC2->getInitVal();
16347330f729Sjoerg if (!llvm::APSInt::isSameValue(Val1, Val2) ||
16357330f729Sjoerg !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
16367330f729Sjoerg if (Context.Complain) {
16377330f729Sjoerg Context.Diag2(D2->getLocation(),
16387330f729Sjoerg Context.getApplicableDiagnostic(
16397330f729Sjoerg diag::err_odr_tag_type_inconsistent))
16407330f729Sjoerg << Context.ToCtx.getTypeDeclType(D2);
16417330f729Sjoerg Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
16427330f729Sjoerg << EC2->getDeclName() << EC2->getInitVal().toString(10);
16437330f729Sjoerg Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
16447330f729Sjoerg << EC1->getDeclName() << EC1->getInitVal().toString(10);
16457330f729Sjoerg }
16467330f729Sjoerg return false;
16477330f729Sjoerg }
16487330f729Sjoerg }
16497330f729Sjoerg
16507330f729Sjoerg if (EC2 != EC2End) {
16517330f729Sjoerg if (Context.Complain) {
16527330f729Sjoerg Context.Diag2(D2->getLocation(), Context.getApplicableDiagnostic(
16537330f729Sjoerg diag::err_odr_tag_type_inconsistent))
16547330f729Sjoerg << Context.ToCtx.getTypeDeclType(D2);
16557330f729Sjoerg Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
16567330f729Sjoerg << EC2->getDeclName() << EC2->getInitVal().toString(10);
16577330f729Sjoerg Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
16587330f729Sjoerg }
16597330f729Sjoerg return false;
16607330f729Sjoerg }
16617330f729Sjoerg
16627330f729Sjoerg return true;
16637330f729Sjoerg }
16647330f729Sjoerg
IsStructurallyEquivalent(StructuralEquivalenceContext & Context,TemplateParameterList * Params1,TemplateParameterList * Params2)16657330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
16667330f729Sjoerg TemplateParameterList *Params1,
16677330f729Sjoerg TemplateParameterList *Params2) {
16687330f729Sjoerg if (Params1->size() != Params2->size()) {
16697330f729Sjoerg if (Context.Complain) {
16707330f729Sjoerg Context.Diag2(Params2->getTemplateLoc(),
16717330f729Sjoerg Context.getApplicableDiagnostic(
16727330f729Sjoerg diag::err_odr_different_num_template_parameters))
16737330f729Sjoerg << Params1->size() << Params2->size();
16747330f729Sjoerg Context.Diag1(Params1->getTemplateLoc(),
16757330f729Sjoerg diag::note_odr_template_parameter_list);
16767330f729Sjoerg }
16777330f729Sjoerg return false;
16787330f729Sjoerg }
16797330f729Sjoerg
16807330f729Sjoerg for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
16817330f729Sjoerg if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
16827330f729Sjoerg if (Context.Complain) {
16837330f729Sjoerg Context.Diag2(Params2->getParam(I)->getLocation(),
16847330f729Sjoerg Context.getApplicableDiagnostic(
16857330f729Sjoerg diag::err_odr_different_template_parameter_kind));
16867330f729Sjoerg Context.Diag1(Params1->getParam(I)->getLocation(),
16877330f729Sjoerg diag::note_odr_template_parameter_here);
16887330f729Sjoerg }
16897330f729Sjoerg return false;
16907330f729Sjoerg }
16917330f729Sjoerg
16927330f729Sjoerg if (!IsStructurallyEquivalent(Context, Params1->getParam(I),
16937330f729Sjoerg Params2->getParam(I)))
16947330f729Sjoerg return false;
16957330f729Sjoerg }
16967330f729Sjoerg
16977330f729Sjoerg return true;
16987330f729Sjoerg }
16997330f729Sjoerg
IsStructurallyEquivalent(StructuralEquivalenceContext & Context,TemplateTypeParmDecl * D1,TemplateTypeParmDecl * D2)17007330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
17017330f729Sjoerg TemplateTypeParmDecl *D1,
17027330f729Sjoerg TemplateTypeParmDecl *D2) {
17037330f729Sjoerg if (D1->isParameterPack() != D2->isParameterPack()) {
17047330f729Sjoerg if (Context.Complain) {
17057330f729Sjoerg Context.Diag2(D2->getLocation(),
17067330f729Sjoerg Context.getApplicableDiagnostic(
17077330f729Sjoerg diag::err_odr_parameter_pack_non_pack))
17087330f729Sjoerg << D2->isParameterPack();
17097330f729Sjoerg Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
17107330f729Sjoerg << D1->isParameterPack();
17117330f729Sjoerg }
17127330f729Sjoerg return false;
17137330f729Sjoerg }
17147330f729Sjoerg
17157330f729Sjoerg return true;
17167330f729Sjoerg }
17177330f729Sjoerg
IsStructurallyEquivalent(StructuralEquivalenceContext & Context,NonTypeTemplateParmDecl * D1,NonTypeTemplateParmDecl * D2)17187330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
17197330f729Sjoerg NonTypeTemplateParmDecl *D1,
17207330f729Sjoerg NonTypeTemplateParmDecl *D2) {
17217330f729Sjoerg if (D1->isParameterPack() != D2->isParameterPack()) {
17227330f729Sjoerg if (Context.Complain) {
17237330f729Sjoerg Context.Diag2(D2->getLocation(),
17247330f729Sjoerg Context.getApplicableDiagnostic(
17257330f729Sjoerg diag::err_odr_parameter_pack_non_pack))
17267330f729Sjoerg << D2->isParameterPack();
17277330f729Sjoerg Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
17287330f729Sjoerg << D1->isParameterPack();
17297330f729Sjoerg }
17307330f729Sjoerg return false;
17317330f729Sjoerg }
17327330f729Sjoerg
17337330f729Sjoerg // Check types.
17347330f729Sjoerg if (!IsStructurallyEquivalent(Context, D1->getType(), D2->getType())) {
17357330f729Sjoerg if (Context.Complain) {
17367330f729Sjoerg Context.Diag2(D2->getLocation(),
17377330f729Sjoerg Context.getApplicableDiagnostic(
17387330f729Sjoerg diag::err_odr_non_type_parameter_type_inconsistent))
17397330f729Sjoerg << D2->getType() << D1->getType();
17407330f729Sjoerg Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
17417330f729Sjoerg << D1->getType();
17427330f729Sjoerg }
17437330f729Sjoerg return false;
17447330f729Sjoerg }
17457330f729Sjoerg
17467330f729Sjoerg return true;
17477330f729Sjoerg }
17487330f729Sjoerg
IsStructurallyEquivalent(StructuralEquivalenceContext & Context,TemplateTemplateParmDecl * D1,TemplateTemplateParmDecl * D2)17497330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
17507330f729Sjoerg TemplateTemplateParmDecl *D1,
17517330f729Sjoerg TemplateTemplateParmDecl *D2) {
17527330f729Sjoerg if (D1->isParameterPack() != D2->isParameterPack()) {
17537330f729Sjoerg if (Context.Complain) {
17547330f729Sjoerg Context.Diag2(D2->getLocation(),
17557330f729Sjoerg Context.getApplicableDiagnostic(
17567330f729Sjoerg diag::err_odr_parameter_pack_non_pack))
17577330f729Sjoerg << D2->isParameterPack();
17587330f729Sjoerg Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
17597330f729Sjoerg << D1->isParameterPack();
17607330f729Sjoerg }
17617330f729Sjoerg return false;
17627330f729Sjoerg }
17637330f729Sjoerg
17647330f729Sjoerg // Check template parameter lists.
17657330f729Sjoerg return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
17667330f729Sjoerg D2->getTemplateParameters());
17677330f729Sjoerg }
17687330f729Sjoerg
IsTemplateDeclCommonStructurallyEquivalent(StructuralEquivalenceContext & Ctx,TemplateDecl * D1,TemplateDecl * D2)17697330f729Sjoerg static bool IsTemplateDeclCommonStructurallyEquivalent(
17707330f729Sjoerg StructuralEquivalenceContext &Ctx, TemplateDecl *D1, TemplateDecl *D2) {
17717330f729Sjoerg if (!IsStructurallyEquivalent(D1->getIdentifier(), D2->getIdentifier()))
17727330f729Sjoerg return false;
17737330f729Sjoerg if (!D1->getIdentifier()) // Special name
17747330f729Sjoerg if (D1->getNameAsString() != D2->getNameAsString())
17757330f729Sjoerg return false;
17767330f729Sjoerg return IsStructurallyEquivalent(Ctx, D1->getTemplateParameters(),
17777330f729Sjoerg D2->getTemplateParameters());
17787330f729Sjoerg }
17797330f729Sjoerg
IsStructurallyEquivalent(StructuralEquivalenceContext & Context,ClassTemplateDecl * D1,ClassTemplateDecl * D2)17807330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
17817330f729Sjoerg ClassTemplateDecl *D1,
17827330f729Sjoerg ClassTemplateDecl *D2) {
17837330f729Sjoerg // Check template parameters.
17847330f729Sjoerg if (!IsTemplateDeclCommonStructurallyEquivalent(Context, D1, D2))
17857330f729Sjoerg return false;
17867330f729Sjoerg
17877330f729Sjoerg // Check the templated declaration.
17887330f729Sjoerg return IsStructurallyEquivalent(Context, D1->getTemplatedDecl(),
17897330f729Sjoerg D2->getTemplatedDecl());
17907330f729Sjoerg }
17917330f729Sjoerg
IsStructurallyEquivalent(StructuralEquivalenceContext & Context,FunctionTemplateDecl * D1,FunctionTemplateDecl * D2)17927330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
17937330f729Sjoerg FunctionTemplateDecl *D1,
17947330f729Sjoerg FunctionTemplateDecl *D2) {
17957330f729Sjoerg // Check template parameters.
17967330f729Sjoerg if (!IsTemplateDeclCommonStructurallyEquivalent(Context, D1, D2))
17977330f729Sjoerg return false;
17987330f729Sjoerg
17997330f729Sjoerg // Check the templated declaration.
18007330f729Sjoerg return IsStructurallyEquivalent(Context, D1->getTemplatedDecl()->getType(),
18017330f729Sjoerg D2->getTemplatedDecl()->getType());
18027330f729Sjoerg }
18037330f729Sjoerg
IsStructurallyEquivalent(StructuralEquivalenceContext & Context,ConceptDecl * D1,ConceptDecl * D2)18047330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
18057330f729Sjoerg ConceptDecl *D1,
18067330f729Sjoerg ConceptDecl *D2) {
18077330f729Sjoerg // Check template parameters.
18087330f729Sjoerg if (!IsTemplateDeclCommonStructurallyEquivalent(Context, D1, D2))
18097330f729Sjoerg return false;
18107330f729Sjoerg
18117330f729Sjoerg // Check the constraint expression.
18127330f729Sjoerg return IsStructurallyEquivalent(Context, D1->getConstraintExpr(),
18137330f729Sjoerg D2->getConstraintExpr());
18147330f729Sjoerg }
18157330f729Sjoerg
IsStructurallyEquivalent(StructuralEquivalenceContext & Context,FriendDecl * D1,FriendDecl * D2)18167330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
18177330f729Sjoerg FriendDecl *D1, FriendDecl *D2) {
18187330f729Sjoerg if ((D1->getFriendType() && D2->getFriendDecl()) ||
18197330f729Sjoerg (D1->getFriendDecl() && D2->getFriendType())) {
18207330f729Sjoerg return false;
18217330f729Sjoerg }
18227330f729Sjoerg if (D1->getFriendType() && D2->getFriendType())
18237330f729Sjoerg return IsStructurallyEquivalent(Context,
18247330f729Sjoerg D1->getFriendType()->getType(),
18257330f729Sjoerg D2->getFriendType()->getType());
18267330f729Sjoerg if (D1->getFriendDecl() && D2->getFriendDecl())
18277330f729Sjoerg return IsStructurallyEquivalent(Context, D1->getFriendDecl(),
18287330f729Sjoerg D2->getFriendDecl());
18297330f729Sjoerg return false;
18307330f729Sjoerg }
18317330f729Sjoerg
IsStructurallyEquivalent(StructuralEquivalenceContext & Context,TypedefNameDecl * D1,TypedefNameDecl * D2)18327330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1833*e038c9c4Sjoerg TypedefNameDecl *D1, TypedefNameDecl *D2) {
1834*e038c9c4Sjoerg if (!IsStructurallyEquivalent(D1->getIdentifier(), D2->getIdentifier()))
1835*e038c9c4Sjoerg return false;
1836*e038c9c4Sjoerg
1837*e038c9c4Sjoerg return IsStructurallyEquivalent(Context, D1->getUnderlyingType(),
1838*e038c9c4Sjoerg D2->getUnderlyingType());
1839*e038c9c4Sjoerg }
1840*e038c9c4Sjoerg
IsStructurallyEquivalent(StructuralEquivalenceContext & Context,FunctionDecl * D1,FunctionDecl * D2)1841*e038c9c4Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
18427330f729Sjoerg FunctionDecl *D1, FunctionDecl *D2) {
1843*e038c9c4Sjoerg if (!IsStructurallyEquivalent(D1->getIdentifier(), D2->getIdentifier()))
1844*e038c9c4Sjoerg return false;
1845*e038c9c4Sjoerg
1846*e038c9c4Sjoerg if (D1->isOverloadedOperator()) {
1847*e038c9c4Sjoerg if (!D2->isOverloadedOperator())
1848*e038c9c4Sjoerg return false;
1849*e038c9c4Sjoerg if (D1->getOverloadedOperator() != D2->getOverloadedOperator())
1850*e038c9c4Sjoerg return false;
1851*e038c9c4Sjoerg }
1852*e038c9c4Sjoerg
18537330f729Sjoerg // FIXME: Consider checking for function attributes as well.
18547330f729Sjoerg if (!IsStructurallyEquivalent(Context, D1->getType(), D2->getType()))
18557330f729Sjoerg return false;
18567330f729Sjoerg
18577330f729Sjoerg return true;
18587330f729Sjoerg }
18597330f729Sjoerg
18607330f729Sjoerg /// Determine structural equivalence of two declarations.
IsStructurallyEquivalent(StructuralEquivalenceContext & Context,Decl * D1,Decl * D2)18617330f729Sjoerg static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
18627330f729Sjoerg Decl *D1, Decl *D2) {
18637330f729Sjoerg // FIXME: Check for known structural equivalences via a callback of some sort.
18647330f729Sjoerg
18657330f729Sjoerg D1 = D1->getCanonicalDecl();
18667330f729Sjoerg D2 = D2->getCanonicalDecl();
18677330f729Sjoerg std::pair<Decl *, Decl *> P{D1, D2};
18687330f729Sjoerg
18697330f729Sjoerg // Check whether we already know that these two declarations are not
18707330f729Sjoerg // structurally equivalent.
18717330f729Sjoerg if (Context.NonEquivalentDecls.count(P))
18727330f729Sjoerg return false;
18737330f729Sjoerg
18747330f729Sjoerg // Check if a check for these declarations is already pending.
18757330f729Sjoerg // If yes D1 and D2 will be checked later (from DeclsToCheck),
18767330f729Sjoerg // or these are already checked (and equivalent).
18777330f729Sjoerg bool Inserted = Context.VisitedDecls.insert(P).second;
18787330f729Sjoerg if (!Inserted)
18797330f729Sjoerg return true;
18807330f729Sjoerg
18817330f729Sjoerg Context.DeclsToCheck.push(P);
18827330f729Sjoerg
18837330f729Sjoerg return true;
18847330f729Sjoerg }
18857330f729Sjoerg
Diag1(SourceLocation Loc,unsigned DiagID)18867330f729Sjoerg DiagnosticBuilder StructuralEquivalenceContext::Diag1(SourceLocation Loc,
18877330f729Sjoerg unsigned DiagID) {
18887330f729Sjoerg assert(Complain && "Not allowed to complain");
18897330f729Sjoerg if (LastDiagFromC2)
18907330f729Sjoerg FromCtx.getDiagnostics().notePriorDiagnosticFrom(ToCtx.getDiagnostics());
18917330f729Sjoerg LastDiagFromC2 = false;
18927330f729Sjoerg return FromCtx.getDiagnostics().Report(Loc, DiagID);
18937330f729Sjoerg }
18947330f729Sjoerg
Diag2(SourceLocation Loc,unsigned DiagID)18957330f729Sjoerg DiagnosticBuilder StructuralEquivalenceContext::Diag2(SourceLocation Loc,
18967330f729Sjoerg unsigned DiagID) {
18977330f729Sjoerg assert(Complain && "Not allowed to complain");
18987330f729Sjoerg if (!LastDiagFromC2)
18997330f729Sjoerg ToCtx.getDiagnostics().notePriorDiagnosticFrom(FromCtx.getDiagnostics());
19007330f729Sjoerg LastDiagFromC2 = true;
19017330f729Sjoerg return ToCtx.getDiagnostics().Report(Loc, DiagID);
19027330f729Sjoerg }
19037330f729Sjoerg
19047330f729Sjoerg Optional<unsigned>
findUntaggedStructOrUnionIndex(RecordDecl * Anon)19057330f729Sjoerg StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(RecordDecl *Anon) {
19067330f729Sjoerg ASTContext &Context = Anon->getASTContext();
19077330f729Sjoerg QualType AnonTy = Context.getRecordType(Anon);
19087330f729Sjoerg
19097330f729Sjoerg const auto *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
19107330f729Sjoerg if (!Owner)
19117330f729Sjoerg return None;
19127330f729Sjoerg
19137330f729Sjoerg unsigned Index = 0;
19147330f729Sjoerg for (const auto *D : Owner->noload_decls()) {
19157330f729Sjoerg const auto *F = dyn_cast<FieldDecl>(D);
19167330f729Sjoerg if (!F)
19177330f729Sjoerg continue;
19187330f729Sjoerg
19197330f729Sjoerg if (F->isAnonymousStructOrUnion()) {
19207330f729Sjoerg if (Context.hasSameType(F->getType(), AnonTy))
19217330f729Sjoerg break;
19227330f729Sjoerg ++Index;
19237330f729Sjoerg continue;
19247330f729Sjoerg }
19257330f729Sjoerg
19267330f729Sjoerg // If the field looks like this:
19277330f729Sjoerg // struct { ... } A;
19287330f729Sjoerg QualType FieldType = F->getType();
19297330f729Sjoerg // In case of nested structs.
19307330f729Sjoerg while (const auto *ElabType = dyn_cast<ElaboratedType>(FieldType))
19317330f729Sjoerg FieldType = ElabType->getNamedType();
19327330f729Sjoerg
19337330f729Sjoerg if (const auto *RecType = dyn_cast<RecordType>(FieldType)) {
19347330f729Sjoerg const RecordDecl *RecDecl = RecType->getDecl();
19357330f729Sjoerg if (RecDecl->getDeclContext() == Owner && !RecDecl->getIdentifier()) {
19367330f729Sjoerg if (Context.hasSameType(FieldType, AnonTy))
19377330f729Sjoerg break;
19387330f729Sjoerg ++Index;
19397330f729Sjoerg continue;
19407330f729Sjoerg }
19417330f729Sjoerg }
19427330f729Sjoerg }
19437330f729Sjoerg
19447330f729Sjoerg return Index;
19457330f729Sjoerg }
19467330f729Sjoerg
getApplicableDiagnostic(unsigned ErrorDiagnostic)19477330f729Sjoerg unsigned StructuralEquivalenceContext::getApplicableDiagnostic(
19487330f729Sjoerg unsigned ErrorDiagnostic) {
19497330f729Sjoerg if (ErrorOnTagTypeMismatch)
19507330f729Sjoerg return ErrorDiagnostic;
19517330f729Sjoerg
19527330f729Sjoerg switch (ErrorDiagnostic) {
19537330f729Sjoerg case diag::err_odr_variable_type_inconsistent:
19547330f729Sjoerg return diag::warn_odr_variable_type_inconsistent;
19557330f729Sjoerg case diag::err_odr_variable_multiple_def:
19567330f729Sjoerg return diag::warn_odr_variable_multiple_def;
19577330f729Sjoerg case diag::err_odr_function_type_inconsistent:
19587330f729Sjoerg return diag::warn_odr_function_type_inconsistent;
19597330f729Sjoerg case diag::err_odr_tag_type_inconsistent:
19607330f729Sjoerg return diag::warn_odr_tag_type_inconsistent;
19617330f729Sjoerg case diag::err_odr_field_type_inconsistent:
19627330f729Sjoerg return diag::warn_odr_field_type_inconsistent;
19637330f729Sjoerg case diag::err_odr_ivar_type_inconsistent:
19647330f729Sjoerg return diag::warn_odr_ivar_type_inconsistent;
19657330f729Sjoerg case diag::err_odr_objc_superclass_inconsistent:
19667330f729Sjoerg return diag::warn_odr_objc_superclass_inconsistent;
19677330f729Sjoerg case diag::err_odr_objc_method_result_type_inconsistent:
19687330f729Sjoerg return diag::warn_odr_objc_method_result_type_inconsistent;
19697330f729Sjoerg case diag::err_odr_objc_method_num_params_inconsistent:
19707330f729Sjoerg return diag::warn_odr_objc_method_num_params_inconsistent;
19717330f729Sjoerg case diag::err_odr_objc_method_param_type_inconsistent:
19727330f729Sjoerg return diag::warn_odr_objc_method_param_type_inconsistent;
19737330f729Sjoerg case diag::err_odr_objc_method_variadic_inconsistent:
19747330f729Sjoerg return diag::warn_odr_objc_method_variadic_inconsistent;
19757330f729Sjoerg case diag::err_odr_objc_property_type_inconsistent:
19767330f729Sjoerg return diag::warn_odr_objc_property_type_inconsistent;
19777330f729Sjoerg case diag::err_odr_objc_property_impl_kind_inconsistent:
19787330f729Sjoerg return diag::warn_odr_objc_property_impl_kind_inconsistent;
19797330f729Sjoerg case diag::err_odr_objc_synthesize_ivar_inconsistent:
19807330f729Sjoerg return diag::warn_odr_objc_synthesize_ivar_inconsistent;
19817330f729Sjoerg case diag::err_odr_different_num_template_parameters:
19827330f729Sjoerg return diag::warn_odr_different_num_template_parameters;
19837330f729Sjoerg case diag::err_odr_different_template_parameter_kind:
19847330f729Sjoerg return diag::warn_odr_different_template_parameter_kind;
19857330f729Sjoerg case diag::err_odr_parameter_pack_non_pack:
19867330f729Sjoerg return diag::warn_odr_parameter_pack_non_pack;
19877330f729Sjoerg case diag::err_odr_non_type_parameter_type_inconsistent:
19887330f729Sjoerg return diag::warn_odr_non_type_parameter_type_inconsistent;
19897330f729Sjoerg }
19907330f729Sjoerg llvm_unreachable("Diagnostic kind not handled in preceding switch");
19917330f729Sjoerg }
19927330f729Sjoerg
IsEquivalent(Decl * D1,Decl * D2)19937330f729Sjoerg bool StructuralEquivalenceContext::IsEquivalent(Decl *D1, Decl *D2) {
19947330f729Sjoerg
19957330f729Sjoerg // Ensure that the implementation functions (all static functions in this TU)
19967330f729Sjoerg // never call the public ASTStructuralEquivalence::IsEquivalent() functions,
19977330f729Sjoerg // because that will wreak havoc the internal state (DeclsToCheck and
19987330f729Sjoerg // VisitedDecls members) and can cause faulty behaviour.
19997330f729Sjoerg // In other words: Do not start a graph search from a new node with the
20007330f729Sjoerg // internal data of another search in progress.
20017330f729Sjoerg // FIXME: Better encapsulation and separation of internal and public
20027330f729Sjoerg // functionality.
20037330f729Sjoerg assert(DeclsToCheck.empty());
20047330f729Sjoerg assert(VisitedDecls.empty());
20057330f729Sjoerg
20067330f729Sjoerg if (!::IsStructurallyEquivalent(*this, D1, D2))
20077330f729Sjoerg return false;
20087330f729Sjoerg
20097330f729Sjoerg return !Finish();
20107330f729Sjoerg }
20117330f729Sjoerg
IsEquivalent(QualType T1,QualType T2)20127330f729Sjoerg bool StructuralEquivalenceContext::IsEquivalent(QualType T1, QualType T2) {
20137330f729Sjoerg assert(DeclsToCheck.empty());
20147330f729Sjoerg assert(VisitedDecls.empty());
20157330f729Sjoerg if (!::IsStructurallyEquivalent(*this, T1, T2))
20167330f729Sjoerg return false;
20177330f729Sjoerg
20187330f729Sjoerg return !Finish();
20197330f729Sjoerg }
20207330f729Sjoerg
IsEquivalent(Stmt * S1,Stmt * S2)2021*e038c9c4Sjoerg bool StructuralEquivalenceContext::IsEquivalent(Stmt *S1, Stmt *S2) {
2022*e038c9c4Sjoerg assert(DeclsToCheck.empty());
2023*e038c9c4Sjoerg assert(VisitedDecls.empty());
2024*e038c9c4Sjoerg if (!::IsStructurallyEquivalent(*this, S1, S2))
2025*e038c9c4Sjoerg return false;
2026*e038c9c4Sjoerg
2027*e038c9c4Sjoerg return !Finish();
2028*e038c9c4Sjoerg }
2029*e038c9c4Sjoerg
CheckCommonEquivalence(Decl * D1,Decl * D2)20307330f729Sjoerg bool StructuralEquivalenceContext::CheckCommonEquivalence(Decl *D1, Decl *D2) {
20317330f729Sjoerg // Check for equivalent described template.
20327330f729Sjoerg TemplateDecl *Template1 = D1->getDescribedTemplate();
20337330f729Sjoerg TemplateDecl *Template2 = D2->getDescribedTemplate();
20347330f729Sjoerg if ((Template1 != nullptr) != (Template2 != nullptr))
20357330f729Sjoerg return false;
20367330f729Sjoerg if (Template1 && !IsStructurallyEquivalent(*this, Template1, Template2))
20377330f729Sjoerg return false;
20387330f729Sjoerg
20397330f729Sjoerg // FIXME: Move check for identifier names into this function.
20407330f729Sjoerg
20417330f729Sjoerg return true;
20427330f729Sjoerg }
20437330f729Sjoerg
CheckKindSpecificEquivalence(Decl * D1,Decl * D2)20447330f729Sjoerg bool StructuralEquivalenceContext::CheckKindSpecificEquivalence(
20457330f729Sjoerg Decl *D1, Decl *D2) {
20467330f729Sjoerg
2047*e038c9c4Sjoerg // Kind mismatch.
2048*e038c9c4Sjoerg if (D1->getKind() != D2->getKind())
2049*e038c9c4Sjoerg return false;
2050*e038c9c4Sjoerg
2051*e038c9c4Sjoerg // Cast the Decls to their actual subclass so that the right overload of
2052*e038c9c4Sjoerg // IsStructurallyEquivalent is called.
2053*e038c9c4Sjoerg switch (D1->getKind()) {
2054*e038c9c4Sjoerg #define ABSTRACT_DECL(DECL)
2055*e038c9c4Sjoerg #define DECL(DERIVED, BASE) \
2056*e038c9c4Sjoerg case Decl::Kind::DERIVED: \
2057*e038c9c4Sjoerg return ::IsStructurallyEquivalent(*this, static_cast<DERIVED##Decl *>(D1), \
2058*e038c9c4Sjoerg static_cast<DERIVED##Decl *>(D2));
2059*e038c9c4Sjoerg #include "clang/AST/DeclNodes.inc"
2060*e038c9c4Sjoerg }
20617330f729Sjoerg return true;
20627330f729Sjoerg }
20637330f729Sjoerg
Finish()20647330f729Sjoerg bool StructuralEquivalenceContext::Finish() {
20657330f729Sjoerg while (!DeclsToCheck.empty()) {
20667330f729Sjoerg // Check the next declaration.
20677330f729Sjoerg std::pair<Decl *, Decl *> P = DeclsToCheck.front();
20687330f729Sjoerg DeclsToCheck.pop();
20697330f729Sjoerg
20707330f729Sjoerg Decl *D1 = P.first;
20717330f729Sjoerg Decl *D2 = P.second;
20727330f729Sjoerg
20737330f729Sjoerg bool Equivalent =
20747330f729Sjoerg CheckCommonEquivalence(D1, D2) && CheckKindSpecificEquivalence(D1, D2);
20757330f729Sjoerg
20767330f729Sjoerg if (!Equivalent) {
20777330f729Sjoerg // Note that these two declarations are not equivalent (and we already
20787330f729Sjoerg // know about it).
20797330f729Sjoerg NonEquivalentDecls.insert(P);
20807330f729Sjoerg
20817330f729Sjoerg return true;
20827330f729Sjoerg }
20837330f729Sjoerg }
20847330f729Sjoerg
20857330f729Sjoerg return false;
20867330f729Sjoerg }
2087