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