blob: 567945c16cdb97f54d4e750ebf51dfc17dcf5497 [file] [log] [blame]
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001//===- ASTStructuralEquivalence.cpp ---------------------------------------===//
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Lopes95ff11b2017-04-28 00:31:30 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implement StructuralEquivalenceContext class and helper functions
10// for layout matching.
11//
Gabor Marton950fb572018-07-17 12:39:27 +000012// 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 Lopes95ff11b2017-04-28 00:31:30 +000065//===----------------------------------------------------------------------===//
66
67#include "clang/AST/ASTStructuralEquivalence.h"
68#include "clang/AST/ASTContext.h"
69#include "clang/AST/ASTDiagnostic.h"
Eugene Zelenko2a1ba942018-04-09 22:14:10 +000070#include "clang/AST/Decl.h"
71#include "clang/AST/DeclBase.h"
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +000072#include "clang/AST/DeclCXX.h"
Peter Szecsib180eeb2018-04-25 17:28:03 +000073#include "clang/AST/DeclFriend.h"
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +000074#include "clang/AST/DeclObjC.h"
Eugene Zelenko2a1ba942018-04-09 22:14:10 +000075#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 Lopes95ff11b2017-04-28 00:31:30 +000093
94using namespace clang;
95
96static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
97 QualType T1, QualType T2);
98static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
99 Decl *D1, Decl *D2);
100static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
101 const TemplateArgument &Arg1,
102 const TemplateArgument &Arg2);
103
104/// Determine structural equivalence of two expressions.
105static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
Erich Keanef702b022018-07-13 19:46:04 +0000106 const Expr *E1, const Expr *E2) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000107 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.
115static 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.
124static 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
162static 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 Zelenko2a1ba942018-04-09 22:14:10 +0000213
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000214 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.
228static 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 Marton950fb572018-07-17 12:39:27 +0000239 return IsStructurallyEquivalent(Context, Arg1.getAsType(), Arg2.getAsType());
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000240
241 case TemplateArgument::Integral:
Gabor Marton950fb572018-07-17 12:39:27 +0000242 if (!IsStructurallyEquivalent(Context, Arg1.getIntegralType(),
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000243 Arg2.getIntegralType()))
244 return false;
245
246 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(),
247 Arg2.getAsIntegral());
248
249 case TemplateArgument::Declaration:
Gabor Marton950fb572018-07-17 12:39:27 +0000250 return IsStructurallyEquivalent(Context, Arg1.getAsDecl(), Arg2.getAsDecl());
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000251
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.
285static 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
Gabor Marton41f20462019-01-24 14:47:44 +0000299/// Determine structural equivalence based on the ExtInfo of functions. This
300/// is inspired by ASTContext::mergeFunctionTypes(), we compare calling
301/// conventions bits but must not compare some other bits.
302static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
303 FunctionType::ExtInfo EI1,
304 FunctionType::ExtInfo EI2) {
305 // Compatible functions must have compatible calling conventions.
306 if (EI1.getCC() != EI2.getCC())
307 return false;
308
309 // Regparm is part of the calling convention.
310 if (EI1.getHasRegParm() != EI2.getHasRegParm())
311 return false;
312 if (EI1.getRegParm() != EI2.getRegParm())
313 return false;
314
315 if (EI1.getProducesResult() != EI2.getProducesResult())
316 return false;
317 if (EI1.getNoCallerSavedRegs() != EI2.getNoCallerSavedRegs())
318 return false;
319 if (EI1.getNoCfCheck() != EI2.getNoCfCheck())
320 return false;
321
322 return true;
323}
324
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000325/// Determine structural equivalence of two types.
326static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
327 QualType T1, QualType T2) {
328 if (T1.isNull() || T2.isNull())
329 return T1.isNull() && T2.isNull();
330
Balazs Keric7797c42018-07-11 09:37:24 +0000331 QualType OrigT1 = T1;
332 QualType OrigT2 = T2;
333
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000334 if (!Context.StrictTypeSpelling) {
335 // We aren't being strict about token-to-token equivalence of types,
336 // so map down to the canonical type.
337 T1 = Context.FromCtx.getCanonicalType(T1);
338 T2 = Context.ToCtx.getCanonicalType(T2);
339 }
340
341 if (T1.getQualifiers() != T2.getQualifiers())
342 return false;
343
344 Type::TypeClass TC = T1->getTypeClass();
345
346 if (T1->getTypeClass() != T2->getTypeClass()) {
347 // Compare function types with prototypes vs. without prototypes as if
348 // both did not have prototypes.
349 if (T1->getTypeClass() == Type::FunctionProto &&
350 T2->getTypeClass() == Type::FunctionNoProto)
351 TC = Type::FunctionNoProto;
352 else if (T1->getTypeClass() == Type::FunctionNoProto &&
353 T2->getTypeClass() == Type::FunctionProto)
354 TC = Type::FunctionNoProto;
355 else
356 return false;
357 }
358
359 switch (TC) {
360 case Type::Builtin:
361 // FIXME: Deal with Char_S/Char_U.
362 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
363 return false;
364 break;
365
366 case Type::Complex:
367 if (!IsStructurallyEquivalent(Context,
368 cast<ComplexType>(T1)->getElementType(),
369 cast<ComplexType>(T2)->getElementType()))
370 return false;
371 break;
372
373 case Type::Adjusted:
374 case Type::Decayed:
375 if (!IsStructurallyEquivalent(Context,
376 cast<AdjustedType>(T1)->getOriginalType(),
377 cast<AdjustedType>(T2)->getOriginalType()))
378 return false;
379 break;
380
381 case Type::Pointer:
382 if (!IsStructurallyEquivalent(Context,
383 cast<PointerType>(T1)->getPointeeType(),
384 cast<PointerType>(T2)->getPointeeType()))
385 return false;
386 break;
387
388 case Type::BlockPointer:
389 if (!IsStructurallyEquivalent(Context,
390 cast<BlockPointerType>(T1)->getPointeeType(),
391 cast<BlockPointerType>(T2)->getPointeeType()))
392 return false;
393 break;
394
395 case Type::LValueReference:
396 case Type::RValueReference: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000397 const auto *Ref1 = cast<ReferenceType>(T1);
398 const auto *Ref2 = cast<ReferenceType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000399 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
400 return false;
401 if (Ref1->isInnerRef() != Ref2->isInnerRef())
402 return false;
403 if (!IsStructurallyEquivalent(Context, Ref1->getPointeeTypeAsWritten(),
404 Ref2->getPointeeTypeAsWritten()))
405 return false;
406 break;
407 }
408
409 case Type::MemberPointer: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000410 const auto *MemPtr1 = cast<MemberPointerType>(T1);
411 const auto *MemPtr2 = cast<MemberPointerType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000412 if (!IsStructurallyEquivalent(Context, MemPtr1->getPointeeType(),
413 MemPtr2->getPointeeType()))
414 return false;
415 if (!IsStructurallyEquivalent(Context, QualType(MemPtr1->getClass(), 0),
416 QualType(MemPtr2->getClass(), 0)))
417 return false;
418 break;
419 }
420
421 case Type::ConstantArray: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000422 const auto *Array1 = cast<ConstantArrayType>(T1);
423 const auto *Array2 = cast<ConstantArrayType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000424 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
425 return false;
426
427 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
428 return false;
429 break;
430 }
431
432 case Type::IncompleteArray:
433 if (!IsArrayStructurallyEquivalent(Context, cast<ArrayType>(T1),
434 cast<ArrayType>(T2)))
435 return false;
436 break;
437
438 case Type::VariableArray: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000439 const auto *Array1 = cast<VariableArrayType>(T1);
440 const auto *Array2 = cast<VariableArrayType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000441 if (!IsStructurallyEquivalent(Context, Array1->getSizeExpr(),
442 Array2->getSizeExpr()))
443 return false;
444
445 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
446 return false;
447
448 break;
449 }
450
451 case Type::DependentSizedArray: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000452 const auto *Array1 = cast<DependentSizedArrayType>(T1);
453 const auto *Array2 = cast<DependentSizedArrayType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000454 if (!IsStructurallyEquivalent(Context, Array1->getSizeExpr(),
455 Array2->getSizeExpr()))
456 return false;
457
458 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
459 return false;
460
461 break;
462 }
463
Andrew Gozillon572bbb02017-10-02 06:25:51 +0000464 case Type::DependentAddressSpace: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000465 const auto *DepAddressSpace1 = cast<DependentAddressSpaceType>(T1);
466 const auto *DepAddressSpace2 = cast<DependentAddressSpaceType>(T2);
Andrew Gozillon572bbb02017-10-02 06:25:51 +0000467 if (!IsStructurallyEquivalent(Context, DepAddressSpace1->getAddrSpaceExpr(),
468 DepAddressSpace2->getAddrSpaceExpr()))
469 return false;
470 if (!IsStructurallyEquivalent(Context, DepAddressSpace1->getPointeeType(),
471 DepAddressSpace2->getPointeeType()))
472 return false;
473
474 break;
475 }
476
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000477 case Type::DependentSizedExtVector: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000478 const auto *Vec1 = cast<DependentSizedExtVectorType>(T1);
479 const auto *Vec2 = cast<DependentSizedExtVectorType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000480 if (!IsStructurallyEquivalent(Context, Vec1->getSizeExpr(),
481 Vec2->getSizeExpr()))
482 return false;
483 if (!IsStructurallyEquivalent(Context, Vec1->getElementType(),
484 Vec2->getElementType()))
485 return false;
486 break;
487 }
488
Erich Keanef702b022018-07-13 19:46:04 +0000489 case Type::DependentVector: {
490 const auto *Vec1 = cast<DependentVectorType>(T1);
491 const auto *Vec2 = cast<DependentVectorType>(T2);
492 if (Vec1->getVectorKind() != Vec2->getVectorKind())
493 return false;
494 if (!IsStructurallyEquivalent(Context, Vec1->getSizeExpr(),
495 Vec2->getSizeExpr()))
496 return false;
497 if (!IsStructurallyEquivalent(Context, Vec1->getElementType(),
498 Vec2->getElementType()))
499 return false;
500 break;
501 }
502
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000503 case Type::Vector:
504 case Type::ExtVector: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000505 const auto *Vec1 = cast<VectorType>(T1);
506 const auto *Vec2 = cast<VectorType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000507 if (!IsStructurallyEquivalent(Context, Vec1->getElementType(),
508 Vec2->getElementType()))
509 return false;
510 if (Vec1->getNumElements() != Vec2->getNumElements())
511 return false;
512 if (Vec1->getVectorKind() != Vec2->getVectorKind())
513 return false;
514 break;
515 }
516
517 case Type::FunctionProto: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000518 const auto *Proto1 = cast<FunctionProtoType>(T1);
519 const auto *Proto2 = cast<FunctionProtoType>(T2);
Balazs Keric7797c42018-07-11 09:37:24 +0000520
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000521 if (Proto1->getNumParams() != Proto2->getNumParams())
522 return false;
523 for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) {
524 if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I),
525 Proto2->getParamType(I)))
526 return false;
527 }
528 if (Proto1->isVariadic() != Proto2->isVariadic())
529 return false;
Balazs Keric7797c42018-07-11 09:37:24 +0000530
Anastasia Stulovac61eaa52019-01-28 11:37:49 +0000531 if (Proto1->getMethodQuals() != Proto2->getMethodQuals())
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000532 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000533
Balazs Keric7797c42018-07-11 09:37:24 +0000534 // Check exceptions, this information is lost in canonical type.
535 const auto *OrigProto1 =
536 cast<FunctionProtoType>(OrigT1.getDesugaredType(Context.FromCtx));
537 const auto *OrigProto2 =
538 cast<FunctionProtoType>(OrigT2.getDesugaredType(Context.ToCtx));
539 auto Spec1 = OrigProto1->getExceptionSpecType();
540 auto Spec2 = OrigProto2->getExceptionSpecType();
Fangrui Song6907ce22018-07-30 19:24:48 +0000541
Balazs Keric7797c42018-07-11 09:37:24 +0000542 if (Spec1 != Spec2)
543 return false;
544 if (Spec1 == EST_Dynamic) {
545 if (OrigProto1->getNumExceptions() != OrigProto2->getNumExceptions())
546 return false;
547 for (unsigned I = 0, N = OrigProto1->getNumExceptions(); I != N; ++I) {
548 if (!IsStructurallyEquivalent(Context, OrigProto1->getExceptionType(I),
549 OrigProto2->getExceptionType(I)))
550 return false;
551 }
552 } else if (isComputedNoexcept(Spec1)) {
553 if (!IsStructurallyEquivalent(Context, OrigProto1->getNoexceptExpr(),
554 OrigProto2->getNoexceptExpr()))
555 return false;
556 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000557
558 // Fall through to check the bits common with FunctionNoProtoType.
Galina Kistanovaf87496d2017-06-03 06:31:42 +0000559 LLVM_FALLTHROUGH;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000560 }
561
562 case Type::FunctionNoProto: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000563 const auto *Function1 = cast<FunctionType>(T1);
564 const auto *Function2 = cast<FunctionType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000565 if (!IsStructurallyEquivalent(Context, Function1->getReturnType(),
566 Function2->getReturnType()))
567 return false;
Gabor Marton41f20462019-01-24 14:47:44 +0000568 if (!IsStructurallyEquivalent(Context, Function1->getExtInfo(),
569 Function2->getExtInfo()))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000570 return false;
571 break;
572 }
573
574 case Type::UnresolvedUsing:
575 if (!IsStructurallyEquivalent(Context,
576 cast<UnresolvedUsingType>(T1)->getDecl(),
577 cast<UnresolvedUsingType>(T2)->getDecl()))
578 return false;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000579 break;
580
581 case Type::Attributed:
582 if (!IsStructurallyEquivalent(Context,
583 cast<AttributedType>(T1)->getModifiedType(),
584 cast<AttributedType>(T2)->getModifiedType()))
585 return false;
586 if (!IsStructurallyEquivalent(
587 Context, cast<AttributedType>(T1)->getEquivalentType(),
588 cast<AttributedType>(T2)->getEquivalentType()))
589 return false;
590 break;
591
592 case Type::Paren:
593 if (!IsStructurallyEquivalent(Context, cast<ParenType>(T1)->getInnerType(),
594 cast<ParenType>(T2)->getInnerType()))
595 return false;
596 break;
597
Leonard Chanc72aaf62019-05-07 03:20:17 +0000598 case Type::MacroQualified:
599 if (!IsStructurallyEquivalent(
600 Context, cast<MacroQualifiedType>(T1)->getUnderlyingType(),
601 cast<MacroQualifiedType>(T2)->getUnderlyingType()))
602 return false;
603 break;
604
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000605 case Type::Typedef:
606 if (!IsStructurallyEquivalent(Context, cast<TypedefType>(T1)->getDecl(),
607 cast<TypedefType>(T2)->getDecl()))
608 return false;
609 break;
610
611 case Type::TypeOfExpr:
612 if (!IsStructurallyEquivalent(
613 Context, cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
614 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
615 return false;
616 break;
617
618 case Type::TypeOf:
619 if (!IsStructurallyEquivalent(Context,
620 cast<TypeOfType>(T1)->getUnderlyingType(),
621 cast<TypeOfType>(T2)->getUnderlyingType()))
622 return false;
623 break;
624
625 case Type::UnaryTransform:
626 if (!IsStructurallyEquivalent(
627 Context, cast<UnaryTransformType>(T1)->getUnderlyingType(),
Eric Fiselier2a0ea012018-04-04 06:31:21 +0000628 cast<UnaryTransformType>(T2)->getUnderlyingType()))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000629 return false;
630 break;
631
632 case Type::Decltype:
633 if (!IsStructurallyEquivalent(Context,
634 cast<DecltypeType>(T1)->getUnderlyingExpr(),
635 cast<DecltypeType>(T2)->getUnderlyingExpr()))
636 return false;
637 break;
638
639 case Type::Auto:
640 if (!IsStructurallyEquivalent(Context, cast<AutoType>(T1)->getDeducedType(),
641 cast<AutoType>(T2)->getDeducedType()))
642 return false;
643 break;
644
645 case Type::DeducedTemplateSpecialization: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000646 const auto *DT1 = cast<DeducedTemplateSpecializationType>(T1);
647 const auto *DT2 = cast<DeducedTemplateSpecializationType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000648 if (!IsStructurallyEquivalent(Context, DT1->getTemplateName(),
649 DT2->getTemplateName()))
650 return false;
651 if (!IsStructurallyEquivalent(Context, DT1->getDeducedType(),
652 DT2->getDeducedType()))
653 return false;
654 break;
655 }
656
657 case Type::Record:
658 case Type::Enum:
659 if (!IsStructurallyEquivalent(Context, cast<TagType>(T1)->getDecl(),
660 cast<TagType>(T2)->getDecl()))
661 return false;
662 break;
663
664 case Type::TemplateTypeParm: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000665 const auto *Parm1 = cast<TemplateTypeParmType>(T1);
666 const auto *Parm2 = cast<TemplateTypeParmType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000667 if (Parm1->getDepth() != Parm2->getDepth())
668 return false;
669 if (Parm1->getIndex() != Parm2->getIndex())
670 return false;
671 if (Parm1->isParameterPack() != Parm2->isParameterPack())
672 return false;
673
674 // Names of template type parameters are never significant.
675 break;
676 }
677
678 case Type::SubstTemplateTypeParm: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000679 const auto *Subst1 = cast<SubstTemplateTypeParmType>(T1);
680 const auto *Subst2 = cast<SubstTemplateTypeParmType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000681 if (!IsStructurallyEquivalent(Context,
682 QualType(Subst1->getReplacedParameter(), 0),
683 QualType(Subst2->getReplacedParameter(), 0)))
684 return false;
685 if (!IsStructurallyEquivalent(Context, Subst1->getReplacementType(),
686 Subst2->getReplacementType()))
687 return false;
688 break;
689 }
690
691 case Type::SubstTemplateTypeParmPack: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000692 const auto *Subst1 = cast<SubstTemplateTypeParmPackType>(T1);
693 const auto *Subst2 = cast<SubstTemplateTypeParmPackType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000694 if (!IsStructurallyEquivalent(Context,
695 QualType(Subst1->getReplacedParameter(), 0),
696 QualType(Subst2->getReplacedParameter(), 0)))
697 return false;
698 if (!IsStructurallyEquivalent(Context, Subst1->getArgumentPack(),
699 Subst2->getArgumentPack()))
700 return false;
701 break;
702 }
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000703
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000704 case Type::TemplateSpecialization: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000705 const auto *Spec1 = cast<TemplateSpecializationType>(T1);
706 const auto *Spec2 = cast<TemplateSpecializationType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000707 if (!IsStructurallyEquivalent(Context, Spec1->getTemplateName(),
708 Spec2->getTemplateName()))
709 return false;
710 if (Spec1->getNumArgs() != Spec2->getNumArgs())
711 return false;
712 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
713 if (!IsStructurallyEquivalent(Context, Spec1->getArg(I),
714 Spec2->getArg(I)))
715 return false;
716 }
717 break;
718 }
719
720 case Type::Elaborated: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000721 const auto *Elab1 = cast<ElaboratedType>(T1);
722 const auto *Elab2 = cast<ElaboratedType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000723 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
724 if (Elab1->getKeyword() != Elab2->getKeyword())
725 return false;
726 if (!IsStructurallyEquivalent(Context, Elab1->getQualifier(),
727 Elab2->getQualifier()))
728 return false;
729 if (!IsStructurallyEquivalent(Context, Elab1->getNamedType(),
730 Elab2->getNamedType()))
731 return false;
732 break;
733 }
734
735 case Type::InjectedClassName: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000736 const auto *Inj1 = cast<InjectedClassNameType>(T1);
737 const auto *Inj2 = cast<InjectedClassNameType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000738 if (!IsStructurallyEquivalent(Context,
739 Inj1->getInjectedSpecializationType(),
740 Inj2->getInjectedSpecializationType()))
741 return false;
742 break;
743 }
744
745 case Type::DependentName: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000746 const auto *Typename1 = cast<DependentNameType>(T1);
747 const auto *Typename2 = cast<DependentNameType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000748 if (!IsStructurallyEquivalent(Context, Typename1->getQualifier(),
749 Typename2->getQualifier()))
750 return false;
751 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
752 Typename2->getIdentifier()))
753 return false;
754
755 break;
756 }
757
758 case Type::DependentTemplateSpecialization: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000759 const auto *Spec1 = cast<DependentTemplateSpecializationType>(T1);
760 const auto *Spec2 = cast<DependentTemplateSpecializationType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000761 if (!IsStructurallyEquivalent(Context, Spec1->getQualifier(),
762 Spec2->getQualifier()))
763 return false;
764 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
765 Spec2->getIdentifier()))
766 return false;
767 if (Spec1->getNumArgs() != Spec2->getNumArgs())
768 return false;
769 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
770 if (!IsStructurallyEquivalent(Context, Spec1->getArg(I),
771 Spec2->getArg(I)))
772 return false;
773 }
774 break;
775 }
776
777 case Type::PackExpansion:
778 if (!IsStructurallyEquivalent(Context,
779 cast<PackExpansionType>(T1)->getPattern(),
780 cast<PackExpansionType>(T2)->getPattern()))
781 return false;
782 break;
783
784 case Type::ObjCInterface: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000785 const auto *Iface1 = cast<ObjCInterfaceType>(T1);
786 const auto *Iface2 = cast<ObjCInterfaceType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000787 if (!IsStructurallyEquivalent(Context, Iface1->getDecl(),
788 Iface2->getDecl()))
789 return false;
790 break;
791 }
792
793 case Type::ObjCTypeParam: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000794 const auto *Obj1 = cast<ObjCTypeParamType>(T1);
795 const auto *Obj2 = cast<ObjCTypeParamType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000796 if (!IsStructurallyEquivalent(Context, Obj1->getDecl(), Obj2->getDecl()))
797 return false;
798
799 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
800 return false;
801 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
802 if (!IsStructurallyEquivalent(Context, Obj1->getProtocol(I),
803 Obj2->getProtocol(I)))
804 return false;
805 }
806 break;
807 }
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000808
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000809 case Type::ObjCObject: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000810 const auto *Obj1 = cast<ObjCObjectType>(T1);
811 const auto *Obj2 = cast<ObjCObjectType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000812 if (!IsStructurallyEquivalent(Context, Obj1->getBaseType(),
813 Obj2->getBaseType()))
814 return false;
815 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
816 return false;
817 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
818 if (!IsStructurallyEquivalent(Context, Obj1->getProtocol(I),
819 Obj2->getProtocol(I)))
820 return false;
821 }
822 break;
823 }
824
825 case Type::ObjCObjectPointer: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000826 const auto *Ptr1 = cast<ObjCObjectPointerType>(T1);
827 const auto *Ptr2 = cast<ObjCObjectPointerType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000828 if (!IsStructurallyEquivalent(Context, Ptr1->getPointeeType(),
829 Ptr2->getPointeeType()))
830 return false;
831 break;
832 }
833
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000834 case Type::Atomic:
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000835 if (!IsStructurallyEquivalent(Context, cast<AtomicType>(T1)->getValueType(),
836 cast<AtomicType>(T2)->getValueType()))
837 return false;
838 break;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000839
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000840 case Type::Pipe:
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000841 if (!IsStructurallyEquivalent(Context, cast<PipeType>(T1)->getElementType(),
842 cast<PipeType>(T2)->getElementType()))
843 return false;
844 break;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000845 } // end switch
846
847 return true;
848}
849
850/// Determine structural equivalence of two fields.
851static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
852 FieldDecl *Field1, FieldDecl *Field2) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000853 const auto *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000854
855 // For anonymous structs/unions, match up the anonymous struct/union type
856 // declarations directly, so that we don't go off searching for anonymous
857 // types
858 if (Field1->isAnonymousStructOrUnion() &&
859 Field2->isAnonymousStructOrUnion()) {
860 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
861 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
862 return IsStructurallyEquivalent(Context, D1, D2);
863 }
864
865 // Check for equivalent field names.
866 IdentifierInfo *Name1 = Field1->getIdentifier();
867 IdentifierInfo *Name2 = Field2->getIdentifier();
Bruno Cardoso Lopesdf0ee342017-07-01 00:06:47 +0000868 if (!::IsStructurallyEquivalent(Name1, Name2)) {
869 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +0000870 Context.Diag2(
871 Owner2->getLocation(),
872 Context.getApplicableDiagnostic(diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopesdf0ee342017-07-01 00:06:47 +0000873 << Context.ToCtx.getTypeDeclType(Owner2);
874 Context.Diag2(Field2->getLocation(), diag::note_odr_field_name)
875 << Field2->getDeclName();
876 Context.Diag1(Field1->getLocation(), diag::note_odr_field_name)
877 << Field1->getDeclName();
878 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000879 return false;
Bruno Cardoso Lopesdf0ee342017-07-01 00:06:47 +0000880 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000881
882 if (!IsStructurallyEquivalent(Context, Field1->getType(),
883 Field2->getType())) {
884 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +0000885 Context.Diag2(
886 Owner2->getLocation(),
887 Context.getApplicableDiagnostic(diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000888 << Context.ToCtx.getTypeDeclType(Owner2);
889 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
890 << Field2->getDeclName() << Field2->getType();
891 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
892 << Field1->getDeclName() << Field1->getType();
893 }
894 return false;
895 }
896
897 if (Field1->isBitField() != Field2->isBitField()) {
898 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +0000899 Context.Diag2(
900 Owner2->getLocation(),
901 Context.getApplicableDiagnostic(diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000902 << Context.ToCtx.getTypeDeclType(Owner2);
903 if (Field1->isBitField()) {
904 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
905 << Field1->getDeclName() << Field1->getType()
906 << Field1->getBitWidthValue(Context.FromCtx);
907 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
908 << Field2->getDeclName();
909 } else {
910 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
911 << Field2->getDeclName() << Field2->getType()
912 << Field2->getBitWidthValue(Context.ToCtx);
913 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
914 << Field1->getDeclName();
915 }
916 }
917 return false;
918 }
919
920 if (Field1->isBitField()) {
921 // Make sure that the bit-fields are the same length.
922 unsigned Bits1 = Field1->getBitWidthValue(Context.FromCtx);
923 unsigned Bits2 = Field2->getBitWidthValue(Context.ToCtx);
924
925 if (Bits1 != Bits2) {
926 if (Context.Complain) {
927 Context.Diag2(Owner2->getLocation(),
Gabor Marton60768cd2019-04-01 14:46:53 +0000928 Context.getApplicableDiagnostic(
929 diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000930 << Context.ToCtx.getTypeDeclType(Owner2);
931 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
932 << Field2->getDeclName() << Field2->getType() << Bits2;
933 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
934 << Field1->getDeclName() << Field1->getType() << Bits1;
935 }
936 return false;
937 }
938 }
939
940 return true;
941}
942
Raphael Isemannb23ccec2018-12-10 12:37:46 +0000943/// Determine structural equivalence of two methods.
Balazs Keric7797c42018-07-11 09:37:24 +0000944static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
945 CXXMethodDecl *Method1,
946 CXXMethodDecl *Method2) {
947 bool PropertiesEqual =
948 Method1->getDeclKind() == Method2->getDeclKind() &&
949 Method1->getRefQualifier() == Method2->getRefQualifier() &&
950 Method1->getAccess() == Method2->getAccess() &&
951 Method1->getOverloadedOperator() == Method2->getOverloadedOperator() &&
952 Method1->isStatic() == Method2->isStatic() &&
953 Method1->isConst() == Method2->isConst() &&
954 Method1->isVolatile() == Method2->isVolatile() &&
955 Method1->isVirtual() == Method2->isVirtual() &&
956 Method1->isPure() == Method2->isPure() &&
957 Method1->isDefaulted() == Method2->isDefaulted() &&
958 Method1->isDeleted() == Method2->isDeleted();
959 if (!PropertiesEqual)
960 return false;
961 // FIXME: Check for 'final'.
962
963 if (auto *Constructor1 = dyn_cast<CXXConstructorDecl>(Method1)) {
964 auto *Constructor2 = cast<CXXConstructorDecl>(Method2);
Hans Wennborgd2b9fc82019-05-06 09:51:10 +0000965 if (Constructor1->isExplicit() != Constructor2->isExplicit())
Balazs Keric7797c42018-07-11 09:37:24 +0000966 return false;
967 }
968
969 if (auto *Conversion1 = dyn_cast<CXXConversionDecl>(Method1)) {
970 auto *Conversion2 = cast<CXXConversionDecl>(Method2);
Hans Wennborgd2b9fc82019-05-06 09:51:10 +0000971 if (Conversion1->isExplicit() != Conversion2->isExplicit())
Balazs Keric7797c42018-07-11 09:37:24 +0000972 return false;
973 if (!IsStructurallyEquivalent(Context, Conversion1->getConversionType(),
974 Conversion2->getConversionType()))
975 return false;
976 }
977
978 const IdentifierInfo *Name1 = Method1->getIdentifier();
979 const IdentifierInfo *Name2 = Method2->getIdentifier();
980 if (!::IsStructurallyEquivalent(Name1, Name2)) {
981 return false;
982 // TODO: Names do not match, add warning like at check for FieldDecl.
983 }
984
985 // Check the prototypes.
986 if (!::IsStructurallyEquivalent(Context,
987 Method1->getType(), Method2->getType()))
988 return false;
989
990 return true;
991}
992
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000993/// Determine structural equivalence of two records.
994static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
995 RecordDecl *D1, RecordDecl *D2) {
996 if (D1->isUnion() != D2->isUnion()) {
997 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +0000998 Context.Diag2(D2->getLocation(), Context.getApplicableDiagnostic(
999 diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001000 << Context.ToCtx.getTypeDeclType(D2);
1001 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
1002 << D1->getDeclName() << (unsigned)D1->getTagKind();
1003 }
1004 return false;
1005 }
1006
Gabor Martonf086fa82018-07-17 12:06:36 +00001007 if (!D1->getDeclName() && !D2->getDeclName()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001008 // If both anonymous structs/unions are in a record context, make sure
1009 // they occur in the same location in the context records.
1010 if (Optional<unsigned> Index1 =
1011 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(D1)) {
1012 if (Optional<unsigned> Index2 =
1013 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(
1014 D2)) {
1015 if (*Index1 != *Index2)
1016 return false;
1017 }
1018 }
1019 }
1020
1021 // If both declarations are class template specializations, we know
1022 // the ODR applies, so check the template and template arguments.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001023 const auto *Spec1 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
1024 const auto *Spec2 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001025 if (Spec1 && Spec2) {
1026 // Check that the specialized templates are the same.
1027 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
1028 Spec2->getSpecializedTemplate()))
1029 return false;
1030
1031 // Check that the template arguments are the same.
1032 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
1033 return false;
1034
1035 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
1036 if (!IsStructurallyEquivalent(Context, Spec1->getTemplateArgs().get(I),
1037 Spec2->getTemplateArgs().get(I)))
1038 return false;
1039 }
1040 // If one is a class template specialization and the other is not, these
1041 // structures are different.
1042 else if (Spec1 || Spec2)
1043 return false;
1044
1045 // Compare the definitions of these two records. If either or both are
Gabor Marton17d39672018-11-26 15:54:08 +00001046 // incomplete (i.e. it is a forward decl), we assume that they are
1047 // equivalent.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001048 D1 = D1->getDefinition();
1049 D2 = D2->getDefinition();
1050 if (!D1 || !D2)
1051 return true;
1052
Gabor Marton26f72a92018-07-12 09:42:05 +00001053 // If any of the records has external storage and we do a minimal check (or
Balazs Keria0a81b12018-08-08 15:04:27 +00001054 // AST import) we assume they are equivalent. (If we didn't have this
Gabor Marton26f72a92018-07-12 09:42:05 +00001055 // assumption then `RecordDecl::LoadFieldsFromExternalStorage` could trigger
1056 // another AST import which in turn would call the structural equivalency
1057 // check again and finally we'd have an improper result.)
1058 if (Context.EqKind == StructuralEquivalenceKind::Minimal)
1059 if (D1->hasExternalLexicalStorage() || D2->hasExternalLexicalStorage())
1060 return true;
1061
Gabor Marton17d39672018-11-26 15:54:08 +00001062 // If one definition is currently being defined, we do not compare for
1063 // equality and we assume that the decls are equal.
1064 if (D1->isBeingDefined() || D2->isBeingDefined())
1065 return true;
1066
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001067 if (auto *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
1068 if (auto *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
Sean Callanan9092d472017-05-13 00:46:33 +00001069 if (D1CXX->hasExternalLexicalStorage() &&
1070 !D1CXX->isCompleteDefinition()) {
1071 D1CXX->getASTContext().getExternalSource()->CompleteType(D1CXX);
1072 }
1073
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001074 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
1075 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +00001076 Context.Diag2(D2->getLocation(),
1077 Context.getApplicableDiagnostic(
1078 diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001079 << Context.ToCtx.getTypeDeclType(D2);
1080 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
1081 << D2CXX->getNumBases();
1082 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
1083 << D1CXX->getNumBases();
1084 }
1085 return false;
1086 }
1087
1088 // Check the base classes.
1089 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
1090 BaseEnd1 = D1CXX->bases_end(),
1091 Base2 = D2CXX->bases_begin();
1092 Base1 != BaseEnd1; ++Base1, ++Base2) {
1093 if (!IsStructurallyEquivalent(Context, Base1->getType(),
1094 Base2->getType())) {
1095 if (Context.Complain) {
1096 Context.Diag2(D2->getLocation(),
Gabor Marton60768cd2019-04-01 14:46:53 +00001097 Context.getApplicableDiagnostic(
1098 diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001099 << Context.ToCtx.getTypeDeclType(D2);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001100 Context.Diag2(Base2->getBeginLoc(), diag::note_odr_base)
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001101 << Base2->getType() << Base2->getSourceRange();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001102 Context.Diag1(Base1->getBeginLoc(), diag::note_odr_base)
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001103 << Base1->getType() << Base1->getSourceRange();
1104 }
1105 return false;
1106 }
1107
1108 // Check virtual vs. non-virtual inheritance mismatch.
1109 if (Base1->isVirtual() != Base2->isVirtual()) {
1110 if (Context.Complain) {
1111 Context.Diag2(D2->getLocation(),
Gabor Marton60768cd2019-04-01 14:46:53 +00001112 Context.getApplicableDiagnostic(
1113 diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001114 << Context.ToCtx.getTypeDeclType(D2);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001115 Context.Diag2(Base2->getBeginLoc(), diag::note_odr_virtual_base)
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001116 << Base2->isVirtual() << Base2->getSourceRange();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001117 Context.Diag1(Base1->getBeginLoc(), diag::note_odr_base)
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001118 << Base1->isVirtual() << Base1->getSourceRange();
1119 }
1120 return false;
1121 }
1122 }
Peter Szecsib180eeb2018-04-25 17:28:03 +00001123
1124 // Check the friends for consistency.
1125 CXXRecordDecl::friend_iterator Friend2 = D2CXX->friend_begin(),
Gabor Marton60768cd2019-04-01 14:46:53 +00001126 Friend2End = D2CXX->friend_end();
Peter Szecsib180eeb2018-04-25 17:28:03 +00001127 for (CXXRecordDecl::friend_iterator Friend1 = D1CXX->friend_begin(),
Gabor Marton60768cd2019-04-01 14:46:53 +00001128 Friend1End = D1CXX->friend_end();
Peter Szecsib180eeb2018-04-25 17:28:03 +00001129 Friend1 != Friend1End; ++Friend1, ++Friend2) {
1130 if (Friend2 == Friend2End) {
1131 if (Context.Complain) {
1132 Context.Diag2(D2->getLocation(),
Gabor Marton60768cd2019-04-01 14:46:53 +00001133 Context.getApplicableDiagnostic(
1134 diag::err_odr_tag_type_inconsistent))
1135 << Context.ToCtx.getTypeDeclType(D2CXX);
Peter Szecsib180eeb2018-04-25 17:28:03 +00001136 Context.Diag1((*Friend1)->getFriendLoc(), diag::note_odr_friend);
1137 Context.Diag2(D2->getLocation(), diag::note_odr_missing_friend);
1138 }
1139 return false;
1140 }
1141
1142 if (!IsStructurallyEquivalent(Context, *Friend1, *Friend2)) {
1143 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +00001144 Context.Diag2(D2->getLocation(),
1145 Context.getApplicableDiagnostic(
1146 diag::err_odr_tag_type_inconsistent))
1147 << Context.ToCtx.getTypeDeclType(D2CXX);
Peter Szecsib180eeb2018-04-25 17:28:03 +00001148 Context.Diag1((*Friend1)->getFriendLoc(), diag::note_odr_friend);
1149 Context.Diag2((*Friend2)->getFriendLoc(), diag::note_odr_friend);
1150 }
1151 return false;
1152 }
1153 }
1154
1155 if (Friend2 != Friend2End) {
1156 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +00001157 Context.Diag2(D2->getLocation(),
1158 Context.getApplicableDiagnostic(
1159 diag::err_odr_tag_type_inconsistent))
1160 << Context.ToCtx.getTypeDeclType(D2);
Peter Szecsib180eeb2018-04-25 17:28:03 +00001161 Context.Diag2((*Friend2)->getFriendLoc(), diag::note_odr_friend);
1162 Context.Diag1(D1->getLocation(), diag::note_odr_missing_friend);
1163 }
1164 return false;
1165 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001166 } else if (D1CXX->getNumBases() > 0) {
1167 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +00001168 Context.Diag2(D2->getLocation(),
1169 Context.getApplicableDiagnostic(
1170 diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001171 << Context.ToCtx.getTypeDeclType(D2);
1172 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001173 Context.Diag1(Base1->getBeginLoc(), diag::note_odr_base)
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001174 << Base1->getType() << Base1->getSourceRange();
1175 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1176 }
1177 return false;
1178 }
1179 }
1180
1181 // Check the fields for consistency.
1182 RecordDecl::field_iterator Field2 = D2->field_begin(),
1183 Field2End = D2->field_end();
1184 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
1185 Field1End = D1->field_end();
1186 Field1 != Field1End; ++Field1, ++Field2) {
1187 if (Field2 == Field2End) {
1188 if (Context.Complain) {
Bruno Cardoso Lopesdf0ee342017-07-01 00:06:47 +00001189 Context.Diag2(D2->getLocation(),
Gabor Marton60768cd2019-04-01 14:46:53 +00001190 Context.getApplicableDiagnostic(
1191 diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001192 << Context.ToCtx.getTypeDeclType(D2);
1193 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1194 << Field1->getDeclName() << Field1->getType();
1195 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1196 }
1197 return false;
1198 }
1199
1200 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
1201 return false;
1202 }
1203
1204 if (Field2 != Field2End) {
1205 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +00001206 Context.Diag2(D2->getLocation(), Context.getApplicableDiagnostic(
1207 diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001208 << Context.ToCtx.getTypeDeclType(D2);
1209 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1210 << Field2->getDeclName() << Field2->getType();
1211 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1212 }
1213 return false;
1214 }
1215
1216 return true;
1217}
1218
1219/// Determine structural equivalence of two enums.
1220static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1221 EnumDecl *D1, EnumDecl *D2) {
Gabor Marton6b01e1c2018-08-09 12:36:25 +00001222
1223 // Compare the definitions of these two enums. If either or both are
1224 // incomplete (i.e. forward declared), we assume that they are equivalent.
1225 D1 = D1->getDefinition();
1226 D2 = D2->getDefinition();
1227 if (!D1 || !D2)
1228 return true;
1229
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001230 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1231 EC2End = D2->enumerator_end();
1232 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1233 EC1End = D1->enumerator_end();
1234 EC1 != EC1End; ++EC1, ++EC2) {
1235 if (EC2 == EC2End) {
1236 if (Context.Complain) {
Bruno Cardoso Lopesdf0ee342017-07-01 00:06:47 +00001237 Context.Diag2(D2->getLocation(),
Gabor Marton60768cd2019-04-01 14:46:53 +00001238 Context.getApplicableDiagnostic(
1239 diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001240 << Context.ToCtx.getTypeDeclType(D2);
1241 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1242 << EC1->getDeclName() << EC1->getInitVal().toString(10);
1243 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1244 }
1245 return false;
1246 }
1247
1248 llvm::APSInt Val1 = EC1->getInitVal();
1249 llvm::APSInt Val2 = EC2->getInitVal();
1250 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
1251 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1252 if (Context.Complain) {
Bruno Cardoso Lopesdf0ee342017-07-01 00:06:47 +00001253 Context.Diag2(D2->getLocation(),
Gabor Marton60768cd2019-04-01 14:46:53 +00001254 Context.getApplicableDiagnostic(
1255 diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001256 << Context.ToCtx.getTypeDeclType(D2);
1257 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1258 << EC2->getDeclName() << EC2->getInitVal().toString(10);
1259 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1260 << EC1->getDeclName() << EC1->getInitVal().toString(10);
1261 }
1262 return false;
1263 }
1264 }
1265
1266 if (EC2 != EC2End) {
1267 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +00001268 Context.Diag2(D2->getLocation(), Context.getApplicableDiagnostic(
1269 diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001270 << Context.ToCtx.getTypeDeclType(D2);
1271 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1272 << EC2->getDeclName() << EC2->getInitVal().toString(10);
1273 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1274 }
1275 return false;
1276 }
1277
1278 return true;
1279}
1280
1281static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1282 TemplateParameterList *Params1,
1283 TemplateParameterList *Params2) {
1284 if (Params1->size() != Params2->size()) {
1285 if (Context.Complain) {
1286 Context.Diag2(Params2->getTemplateLoc(),
Gabor Marton60768cd2019-04-01 14:46:53 +00001287 Context.getApplicableDiagnostic(
1288 diag::err_odr_different_num_template_parameters))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001289 << Params1->size() << Params2->size();
1290 Context.Diag1(Params1->getTemplateLoc(),
1291 diag::note_odr_template_parameter_list);
1292 }
1293 return false;
1294 }
1295
1296 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1297 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1298 if (Context.Complain) {
1299 Context.Diag2(Params2->getParam(I)->getLocation(),
Gabor Marton60768cd2019-04-01 14:46:53 +00001300 Context.getApplicableDiagnostic(
1301 diag::err_odr_different_template_parameter_kind));
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001302 Context.Diag1(Params1->getParam(I)->getLocation(),
1303 diag::note_odr_template_parameter_here);
1304 }
1305 return false;
1306 }
1307
Gabor Marton950fb572018-07-17 12:39:27 +00001308 if (!IsStructurallyEquivalent(Context, Params1->getParam(I),
1309 Params2->getParam(I)))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001310 return false;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001311 }
1312
1313 return true;
1314}
1315
1316static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1317 TemplateTypeParmDecl *D1,
1318 TemplateTypeParmDecl *D2) {
1319 if (D1->isParameterPack() != D2->isParameterPack()) {
1320 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +00001321 Context.Diag2(D2->getLocation(),
1322 Context.getApplicableDiagnostic(
1323 diag::err_odr_parameter_pack_non_pack))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001324 << D2->isParameterPack();
1325 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1326 << D1->isParameterPack();
1327 }
1328 return false;
1329 }
1330
1331 return true;
1332}
1333
1334static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1335 NonTypeTemplateParmDecl *D1,
1336 NonTypeTemplateParmDecl *D2) {
1337 if (D1->isParameterPack() != D2->isParameterPack()) {
1338 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +00001339 Context.Diag2(D2->getLocation(),
1340 Context.getApplicableDiagnostic(
1341 diag::err_odr_parameter_pack_non_pack))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001342 << D2->isParameterPack();
1343 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1344 << D1->isParameterPack();
1345 }
1346 return false;
1347 }
1348
1349 // Check types.
Gabor Marton950fb572018-07-17 12:39:27 +00001350 if (!IsStructurallyEquivalent(Context, D1->getType(), D2->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001351 if (Context.Complain) {
1352 Context.Diag2(D2->getLocation(),
Gabor Marton60768cd2019-04-01 14:46:53 +00001353 Context.getApplicableDiagnostic(
1354 diag::err_odr_non_type_parameter_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001355 << D2->getType() << D1->getType();
1356 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1357 << D1->getType();
1358 }
1359 return false;
1360 }
1361
1362 return true;
1363}
1364
1365static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1366 TemplateTemplateParmDecl *D1,
1367 TemplateTemplateParmDecl *D2) {
1368 if (D1->isParameterPack() != D2->isParameterPack()) {
1369 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +00001370 Context.Diag2(D2->getLocation(),
1371 Context.getApplicableDiagnostic(
1372 diag::err_odr_parameter_pack_non_pack))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001373 << D2->isParameterPack();
1374 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1375 << D1->isParameterPack();
1376 }
1377 return false;
1378 }
1379
1380 // Check template parameter lists.
1381 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1382 D2->getTemplateParameters());
1383}
1384
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001385static bool IsTemplateDeclCommonStructurallyEquivalent(
1386 StructuralEquivalenceContext &Ctx, TemplateDecl *D1, TemplateDecl *D2) {
1387 if (!IsStructurallyEquivalent(D1->getIdentifier(), D2->getIdentifier()))
1388 return false;
1389 if (!D1->getIdentifier()) // Special name
1390 if (D1->getNameAsString() != D2->getNameAsString())
1391 return false;
1392 return IsStructurallyEquivalent(Ctx, D1->getTemplateParameters(),
1393 D2->getTemplateParameters());
1394}
1395
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001396static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1397 ClassTemplateDecl *D1,
1398 ClassTemplateDecl *D2) {
1399 // Check template parameters.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001400 if (!IsTemplateDeclCommonStructurallyEquivalent(Context, D1, D2))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001401 return false;
1402
1403 // Check the templated declaration.
Gabor Marton950fb572018-07-17 12:39:27 +00001404 return IsStructurallyEquivalent(Context, D1->getTemplatedDecl(),
1405 D2->getTemplatedDecl());
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001406}
1407
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001408static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1409 FunctionTemplateDecl *D1,
1410 FunctionTemplateDecl *D2) {
1411 // Check template parameters.
1412 if (!IsTemplateDeclCommonStructurallyEquivalent(Context, D1, D2))
1413 return false;
1414
1415 // Check the templated declaration.
Gabor Marton950fb572018-07-17 12:39:27 +00001416 return IsStructurallyEquivalent(Context, D1->getTemplatedDecl()->getType(),
1417 D2->getTemplatedDecl()->getType());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001418}
1419
Peter Szecsib180eeb2018-04-25 17:28:03 +00001420static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1421 FriendDecl *D1, FriendDecl *D2) {
1422 if ((D1->getFriendType() && D2->getFriendDecl()) ||
1423 (D1->getFriendDecl() && D2->getFriendType())) {
1424 return false;
1425 }
1426 if (D1->getFriendType() && D2->getFriendType())
1427 return IsStructurallyEquivalent(Context,
1428 D1->getFriendType()->getType(),
1429 D2->getFriendType()->getType());
1430 if (D1->getFriendDecl() && D2->getFriendDecl())
1431 return IsStructurallyEquivalent(Context, D1->getFriendDecl(),
1432 D2->getFriendDecl());
1433 return false;
1434}
1435
1436static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1437 FunctionDecl *D1, FunctionDecl *D2) {
1438 // FIXME: Consider checking for function attributes as well.
1439 if (!IsStructurallyEquivalent(Context, D1->getType(), D2->getType()))
1440 return false;
1441
1442 return true;
1443}
1444
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001445/// Determine structural equivalence of two declarations.
1446static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1447 Decl *D1, Decl *D2) {
1448 // FIXME: Check for known structural equivalences via a callback of some sort.
1449
1450 // Check whether we already know that these two declarations are not
1451 // structurally equivalent.
1452 if (Context.NonEquivalentDecls.count(
1453 std::make_pair(D1->getCanonicalDecl(), D2->getCanonicalDecl())))
1454 return false;
1455
1456 // Determine whether we've already produced a tentative equivalence for D1.
1457 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1458 if (EquivToD1)
1459 return EquivToD1 == D2->getCanonicalDecl();
1460
1461 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1462 EquivToD1 = D2->getCanonicalDecl();
1463 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1464 return true;
1465}
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001466
1467DiagnosticBuilder StructuralEquivalenceContext::Diag1(SourceLocation Loc,
1468 unsigned DiagID) {
1469 assert(Complain && "Not allowed to complain");
1470 if (LastDiagFromC2)
1471 FromCtx.getDiagnostics().notePriorDiagnosticFrom(ToCtx.getDiagnostics());
1472 LastDiagFromC2 = false;
1473 return FromCtx.getDiagnostics().Report(Loc, DiagID);
1474}
1475
1476DiagnosticBuilder StructuralEquivalenceContext::Diag2(SourceLocation Loc,
1477 unsigned DiagID) {
1478 assert(Complain && "Not allowed to complain");
1479 if (!LastDiagFromC2)
1480 ToCtx.getDiagnostics().notePriorDiagnosticFrom(FromCtx.getDiagnostics());
1481 LastDiagFromC2 = true;
1482 return ToCtx.getDiagnostics().Report(Loc, DiagID);
1483}
1484
1485Optional<unsigned>
1486StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(RecordDecl *Anon) {
1487 ASTContext &Context = Anon->getASTContext();
1488 QualType AnonTy = Context.getRecordType(Anon);
1489
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001490 const auto *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001491 if (!Owner)
1492 return None;
1493
1494 unsigned Index = 0;
1495 for (const auto *D : Owner->noload_decls()) {
1496 const auto *F = dyn_cast<FieldDecl>(D);
1497 if (!F)
1498 continue;
1499
1500 if (F->isAnonymousStructOrUnion()) {
1501 if (Context.hasSameType(F->getType(), AnonTy))
1502 break;
1503 ++Index;
1504 continue;
1505 }
1506
1507 // If the field looks like this:
1508 // struct { ... } A;
1509 QualType FieldType = F->getType();
Aleksei Sidorin499de6c2018-04-05 15:31:49 +00001510 // In case of nested structs.
1511 while (const auto *ElabType = dyn_cast<ElaboratedType>(FieldType))
1512 FieldType = ElabType->getNamedType();
1513
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001514 if (const auto *RecType = dyn_cast<RecordType>(FieldType)) {
1515 const RecordDecl *RecDecl = RecType->getDecl();
1516 if (RecDecl->getDeclContext() == Owner && !RecDecl->getIdentifier()) {
1517 if (Context.hasSameType(FieldType, AnonTy))
1518 break;
1519 ++Index;
1520 continue;
1521 }
1522 }
1523 }
1524
1525 return Index;
1526}
1527
Gabor Marton60768cd2019-04-01 14:46:53 +00001528unsigned StructuralEquivalenceContext::getApplicableDiagnostic(
1529 unsigned ErrorDiagnostic) {
1530 if (ErrorOnTagTypeMismatch)
1531 return ErrorDiagnostic;
1532
1533 switch (ErrorDiagnostic) {
1534 case diag::err_odr_variable_type_inconsistent:
1535 return diag::warn_odr_variable_type_inconsistent;
1536 case diag::err_odr_variable_multiple_def:
1537 return diag::warn_odr_variable_multiple_def;
1538 case diag::err_odr_function_type_inconsistent:
1539 return diag::warn_odr_function_type_inconsistent;
1540 case diag::err_odr_tag_type_inconsistent:
1541 return diag::warn_odr_tag_type_inconsistent;
1542 case diag::err_odr_field_type_inconsistent:
1543 return diag::warn_odr_field_type_inconsistent;
1544 case diag::err_odr_ivar_type_inconsistent:
1545 return diag::warn_odr_ivar_type_inconsistent;
1546 case diag::err_odr_objc_superclass_inconsistent:
1547 return diag::warn_odr_objc_superclass_inconsistent;
1548 case diag::err_odr_objc_method_result_type_inconsistent:
1549 return diag::warn_odr_objc_method_result_type_inconsistent;
1550 case diag::err_odr_objc_method_num_params_inconsistent:
1551 return diag::warn_odr_objc_method_num_params_inconsistent;
1552 case diag::err_odr_objc_method_param_type_inconsistent:
1553 return diag::warn_odr_objc_method_param_type_inconsistent;
1554 case diag::err_odr_objc_method_variadic_inconsistent:
1555 return diag::warn_odr_objc_method_variadic_inconsistent;
1556 case diag::err_odr_objc_property_type_inconsistent:
1557 return diag::warn_odr_objc_property_type_inconsistent;
1558 case diag::err_odr_objc_property_impl_kind_inconsistent:
1559 return diag::warn_odr_objc_property_impl_kind_inconsistent;
1560 case diag::err_odr_objc_synthesize_ivar_inconsistent:
1561 return diag::warn_odr_objc_synthesize_ivar_inconsistent;
1562 case diag::err_odr_different_num_template_parameters:
1563 return diag::warn_odr_different_num_template_parameters;
1564 case diag::err_odr_different_template_parameter_kind:
1565 return diag::warn_odr_different_template_parameter_kind;
1566 case diag::err_odr_parameter_pack_non_pack:
1567 return diag::warn_odr_parameter_pack_non_pack;
1568 case diag::err_odr_non_type_parameter_type_inconsistent:
1569 return diag::warn_odr_non_type_parameter_type_inconsistent;
1570 }
Gabor Martondae5ff22019-04-01 15:48:29 +00001571 llvm_unreachable("Diagnostic kind not handled in preceding switch");
Gabor Marton60768cd2019-04-01 14:46:53 +00001572}
1573
Gabor Marton950fb572018-07-17 12:39:27 +00001574bool StructuralEquivalenceContext::IsEquivalent(Decl *D1, Decl *D2) {
1575
1576 // Ensure that the implementation functions (all static functions in this TU)
1577 // never call the public ASTStructuralEquivalence::IsEquivalent() functions,
1578 // because that will wreak havoc the internal state (DeclsToCheck and
1579 // TentativeEquivalences members) and can cause faulty behaviour. For
1580 // instance, some leaf declarations can be stated and cached as inequivalent
1581 // as a side effect of one inequivalent element in the DeclsToCheck list.
1582 assert(DeclsToCheck.empty());
1583 assert(TentativeEquivalences.empty());
1584
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001585 if (!::IsStructurallyEquivalent(*this, D1, D2))
1586 return false;
1587
1588 return !Finish();
1589}
1590
Gabor Marton950fb572018-07-17 12:39:27 +00001591bool StructuralEquivalenceContext::IsEquivalent(QualType T1, QualType T2) {
1592 assert(DeclsToCheck.empty());
1593 assert(TentativeEquivalences.empty());
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001594 if (!::IsStructurallyEquivalent(*this, T1, T2))
1595 return false;
1596
1597 return !Finish();
1598}
1599
Balazs Keria0a81b12018-08-08 15:04:27 +00001600bool StructuralEquivalenceContext::CheckCommonEquivalence(Decl *D1, Decl *D2) {
1601 // Check for equivalent described template.
1602 TemplateDecl *Template1 = D1->getDescribedTemplate();
1603 TemplateDecl *Template2 = D2->getDescribedTemplate();
1604 if ((Template1 != nullptr) != (Template2 != nullptr))
1605 return false;
1606 if (Template1 && !IsStructurallyEquivalent(*this, Template1, Template2))
1607 return false;
1608
1609 // FIXME: Move check for identifier names into this function.
1610
1611 return true;
1612}
1613
1614bool StructuralEquivalenceContext::CheckKindSpecificEquivalence(
1615 Decl *D1, Decl *D2) {
1616 // FIXME: Switch on all declaration kinds. For now, we're just going to
1617 // check the obvious ones.
1618 if (auto *Record1 = dyn_cast<RecordDecl>(D1)) {
1619 if (auto *Record2 = dyn_cast<RecordDecl>(D2)) {
1620 // Check for equivalent structure names.
1621 IdentifierInfo *Name1 = Record1->getIdentifier();
1622 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1623 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
1624 IdentifierInfo *Name2 = Record2->getIdentifier();
1625 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1626 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
1627 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1628 !::IsStructurallyEquivalent(*this, Record1, Record2))
1629 return false;
1630 } else {
1631 // Record/non-record mismatch.
1632 return false;
1633 }
1634 } else if (auto *Enum1 = dyn_cast<EnumDecl>(D1)) {
1635 if (auto *Enum2 = dyn_cast<EnumDecl>(D2)) {
1636 // Check for equivalent enum names.
1637 IdentifierInfo *Name1 = Enum1->getIdentifier();
1638 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1639 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
1640 IdentifierInfo *Name2 = Enum2->getIdentifier();
1641 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1642 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
1643 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1644 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1645 return false;
1646 } else {
1647 // Enum/non-enum mismatch
1648 return false;
1649 }
1650 } else if (const auto *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1651 if (const auto *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
1652 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
1653 Typedef2->getIdentifier()) ||
1654 !::IsStructurallyEquivalent(*this, Typedef1->getUnderlyingType(),
1655 Typedef2->getUnderlyingType()))
1656 return false;
1657 } else {
1658 // Typedef/non-typedef mismatch.
1659 return false;
1660 }
1661 } else if (auto *ClassTemplate1 = dyn_cast<ClassTemplateDecl>(D1)) {
1662 if (auto *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1663 if (!::IsStructurallyEquivalent(*this, ClassTemplate1,
1664 ClassTemplate2))
1665 return false;
1666 } else {
1667 // Class template/non-class-template mismatch.
1668 return false;
1669 }
1670 } else if (auto *FunctionTemplate1 = dyn_cast<FunctionTemplateDecl>(D1)) {
1671 if (auto *FunctionTemplate2 = dyn_cast<FunctionTemplateDecl>(D2)) {
1672 if (!::IsStructurallyEquivalent(*this, FunctionTemplate1,
1673 FunctionTemplate2))
1674 return false;
1675 } else {
1676 // Class template/non-class-template mismatch.
1677 return false;
1678 }
1679 } else if (auto *TTP1 = dyn_cast<TemplateTypeParmDecl>(D1)) {
1680 if (auto *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1681 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1682 return false;
1683 } else {
1684 // Kind mismatch.
1685 return false;
1686 }
1687 } else if (auto *NTTP1 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1688 if (auto *NTTP2 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1689 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1690 return false;
1691 } else {
1692 // Kind mismatch.
1693 return false;
1694 }
1695 } else if (auto *TTP1 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1696 if (auto *TTP2 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1697 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1698 return false;
1699 } else {
1700 // Kind mismatch.
1701 return false;
1702 }
1703 } else if (auto *MD1 = dyn_cast<CXXMethodDecl>(D1)) {
1704 if (auto *MD2 = dyn_cast<CXXMethodDecl>(D2)) {
1705 if (!::IsStructurallyEquivalent(*this, MD1, MD2))
1706 return false;
1707 } else {
1708 // Kind mismatch.
1709 return false;
1710 }
1711 } else if (FunctionDecl *FD1 = dyn_cast<FunctionDecl>(D1)) {
1712 if (FunctionDecl *FD2 = dyn_cast<FunctionDecl>(D2)) {
Gabor Martonfc638d62019-02-08 08:55:32 +00001713 if (FD1->isOverloadedOperator()) {
1714 if (!FD2->isOverloadedOperator())
1715 return false;
1716 if (FD1->getOverloadedOperator() != FD2->getOverloadedOperator())
1717 return false;
1718 }
Balazs Keria0a81b12018-08-08 15:04:27 +00001719 if (!::IsStructurallyEquivalent(FD1->getIdentifier(),
1720 FD2->getIdentifier()))
1721 return false;
1722 if (!::IsStructurallyEquivalent(*this, FD1, FD2))
1723 return false;
1724 } else {
1725 // Kind mismatch.
1726 return false;
1727 }
1728 } else if (FriendDecl *FrD1 = dyn_cast<FriendDecl>(D1)) {
1729 if (FriendDecl *FrD2 = dyn_cast<FriendDecl>(D2)) {
1730 if (!::IsStructurallyEquivalent(*this, FrD1, FrD2))
1731 return false;
1732 } else {
1733 // Kind mismatch.
1734 return false;
1735 }
1736 }
1737
1738 return true;
1739}
1740
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001741bool StructuralEquivalenceContext::Finish() {
1742 while (!DeclsToCheck.empty()) {
1743 // Check the next declaration.
1744 Decl *D1 = DeclsToCheck.front();
1745 DeclsToCheck.pop_front();
1746
1747 Decl *D2 = TentativeEquivalences[D1];
1748 assert(D2 && "Unrecorded tentative equivalence?");
1749
Balazs Keria0a81b12018-08-08 15:04:27 +00001750 bool Equivalent =
1751 CheckCommonEquivalence(D1, D2) && CheckKindSpecificEquivalence(D1, D2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001752
1753 if (!Equivalent) {
1754 // Note that these two declarations are not equivalent (and we already
1755 // know about it).
1756 NonEquivalentDecls.insert(
1757 std::make_pair(D1->getCanonicalDecl(), D2->getCanonicalDecl()));
1758 return true;
1759 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001760 }
1761
1762 return false;
1763}