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