blob: 6dbf5ded3cc7235ec0aa1bc70d62f054f0878d45 [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
598 case Type::Typedef:
599 if (!IsStructurallyEquivalent(Context, cast<TypedefType>(T1)->getDecl(),
600 cast<TypedefType>(T2)->getDecl()))
601 return false;
602 break;
603
604 case Type::TypeOfExpr:
605 if (!IsStructurallyEquivalent(
606 Context, cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
607 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
608 return false;
609 break;
610
611 case Type::TypeOf:
612 if (!IsStructurallyEquivalent(Context,
613 cast<TypeOfType>(T1)->getUnderlyingType(),
614 cast<TypeOfType>(T2)->getUnderlyingType()))
615 return false;
616 break;
617
618 case Type::UnaryTransform:
619 if (!IsStructurallyEquivalent(
620 Context, cast<UnaryTransformType>(T1)->getUnderlyingType(),
Eric Fiselier2a0ea012018-04-04 06:31:21 +0000621 cast<UnaryTransformType>(T2)->getUnderlyingType()))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000622 return false;
623 break;
624
625 case Type::Decltype:
626 if (!IsStructurallyEquivalent(Context,
627 cast<DecltypeType>(T1)->getUnderlyingExpr(),
628 cast<DecltypeType>(T2)->getUnderlyingExpr()))
629 return false;
630 break;
631
632 case Type::Auto:
633 if (!IsStructurallyEquivalent(Context, cast<AutoType>(T1)->getDeducedType(),
634 cast<AutoType>(T2)->getDeducedType()))
635 return false;
636 break;
637
638 case Type::DeducedTemplateSpecialization: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000639 const auto *DT1 = cast<DeducedTemplateSpecializationType>(T1);
640 const auto *DT2 = cast<DeducedTemplateSpecializationType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000641 if (!IsStructurallyEquivalent(Context, DT1->getTemplateName(),
642 DT2->getTemplateName()))
643 return false;
644 if (!IsStructurallyEquivalent(Context, DT1->getDeducedType(),
645 DT2->getDeducedType()))
646 return false;
647 break;
648 }
649
650 case Type::Record:
651 case Type::Enum:
652 if (!IsStructurallyEquivalent(Context, cast<TagType>(T1)->getDecl(),
653 cast<TagType>(T2)->getDecl()))
654 return false;
655 break;
656
657 case Type::TemplateTypeParm: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000658 const auto *Parm1 = cast<TemplateTypeParmType>(T1);
659 const auto *Parm2 = cast<TemplateTypeParmType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000660 if (Parm1->getDepth() != Parm2->getDepth())
661 return false;
662 if (Parm1->getIndex() != Parm2->getIndex())
663 return false;
664 if (Parm1->isParameterPack() != Parm2->isParameterPack())
665 return false;
666
667 // Names of template type parameters are never significant.
668 break;
669 }
670
671 case Type::SubstTemplateTypeParm: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000672 const auto *Subst1 = cast<SubstTemplateTypeParmType>(T1);
673 const auto *Subst2 = cast<SubstTemplateTypeParmType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000674 if (!IsStructurallyEquivalent(Context,
675 QualType(Subst1->getReplacedParameter(), 0),
676 QualType(Subst2->getReplacedParameter(), 0)))
677 return false;
678 if (!IsStructurallyEquivalent(Context, Subst1->getReplacementType(),
679 Subst2->getReplacementType()))
680 return false;
681 break;
682 }
683
684 case Type::SubstTemplateTypeParmPack: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000685 const auto *Subst1 = cast<SubstTemplateTypeParmPackType>(T1);
686 const auto *Subst2 = cast<SubstTemplateTypeParmPackType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000687 if (!IsStructurallyEquivalent(Context,
688 QualType(Subst1->getReplacedParameter(), 0),
689 QualType(Subst2->getReplacedParameter(), 0)))
690 return false;
691 if (!IsStructurallyEquivalent(Context, Subst1->getArgumentPack(),
692 Subst2->getArgumentPack()))
693 return false;
694 break;
695 }
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000696
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000697 case Type::TemplateSpecialization: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000698 const auto *Spec1 = cast<TemplateSpecializationType>(T1);
699 const auto *Spec2 = cast<TemplateSpecializationType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000700 if (!IsStructurallyEquivalent(Context, Spec1->getTemplateName(),
701 Spec2->getTemplateName()))
702 return false;
703 if (Spec1->getNumArgs() != Spec2->getNumArgs())
704 return false;
705 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
706 if (!IsStructurallyEquivalent(Context, Spec1->getArg(I),
707 Spec2->getArg(I)))
708 return false;
709 }
710 break;
711 }
712
713 case Type::Elaborated: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000714 const auto *Elab1 = cast<ElaboratedType>(T1);
715 const auto *Elab2 = cast<ElaboratedType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000716 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
717 if (Elab1->getKeyword() != Elab2->getKeyword())
718 return false;
719 if (!IsStructurallyEquivalent(Context, Elab1->getQualifier(),
720 Elab2->getQualifier()))
721 return false;
722 if (!IsStructurallyEquivalent(Context, Elab1->getNamedType(),
723 Elab2->getNamedType()))
724 return false;
725 break;
726 }
727
728 case Type::InjectedClassName: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000729 const auto *Inj1 = cast<InjectedClassNameType>(T1);
730 const auto *Inj2 = cast<InjectedClassNameType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000731 if (!IsStructurallyEquivalent(Context,
732 Inj1->getInjectedSpecializationType(),
733 Inj2->getInjectedSpecializationType()))
734 return false;
735 break;
736 }
737
738 case Type::DependentName: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000739 const auto *Typename1 = cast<DependentNameType>(T1);
740 const auto *Typename2 = cast<DependentNameType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000741 if (!IsStructurallyEquivalent(Context, Typename1->getQualifier(),
742 Typename2->getQualifier()))
743 return false;
744 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
745 Typename2->getIdentifier()))
746 return false;
747
748 break;
749 }
750
751 case Type::DependentTemplateSpecialization: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000752 const auto *Spec1 = cast<DependentTemplateSpecializationType>(T1);
753 const auto *Spec2 = cast<DependentTemplateSpecializationType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000754 if (!IsStructurallyEquivalent(Context, Spec1->getQualifier(),
755 Spec2->getQualifier()))
756 return false;
757 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
758 Spec2->getIdentifier()))
759 return false;
760 if (Spec1->getNumArgs() != Spec2->getNumArgs())
761 return false;
762 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
763 if (!IsStructurallyEquivalent(Context, Spec1->getArg(I),
764 Spec2->getArg(I)))
765 return false;
766 }
767 break;
768 }
769
770 case Type::PackExpansion:
771 if (!IsStructurallyEquivalent(Context,
772 cast<PackExpansionType>(T1)->getPattern(),
773 cast<PackExpansionType>(T2)->getPattern()))
774 return false;
775 break;
776
777 case Type::ObjCInterface: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000778 const auto *Iface1 = cast<ObjCInterfaceType>(T1);
779 const auto *Iface2 = cast<ObjCInterfaceType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000780 if (!IsStructurallyEquivalent(Context, Iface1->getDecl(),
781 Iface2->getDecl()))
782 return false;
783 break;
784 }
785
786 case Type::ObjCTypeParam: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000787 const auto *Obj1 = cast<ObjCTypeParamType>(T1);
788 const auto *Obj2 = cast<ObjCTypeParamType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000789 if (!IsStructurallyEquivalent(Context, Obj1->getDecl(), Obj2->getDecl()))
790 return false;
791
792 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
793 return false;
794 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
795 if (!IsStructurallyEquivalent(Context, Obj1->getProtocol(I),
796 Obj2->getProtocol(I)))
797 return false;
798 }
799 break;
800 }
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000801
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000802 case Type::ObjCObject: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000803 const auto *Obj1 = cast<ObjCObjectType>(T1);
804 const auto *Obj2 = cast<ObjCObjectType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000805 if (!IsStructurallyEquivalent(Context, Obj1->getBaseType(),
806 Obj2->getBaseType()))
807 return false;
808 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
809 return false;
810 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
811 if (!IsStructurallyEquivalent(Context, Obj1->getProtocol(I),
812 Obj2->getProtocol(I)))
813 return false;
814 }
815 break;
816 }
817
818 case Type::ObjCObjectPointer: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000819 const auto *Ptr1 = cast<ObjCObjectPointerType>(T1);
820 const auto *Ptr2 = cast<ObjCObjectPointerType>(T2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000821 if (!IsStructurallyEquivalent(Context, Ptr1->getPointeeType(),
822 Ptr2->getPointeeType()))
823 return false;
824 break;
825 }
826
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000827 case Type::Atomic:
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000828 if (!IsStructurallyEquivalent(Context, cast<AtomicType>(T1)->getValueType(),
829 cast<AtomicType>(T2)->getValueType()))
830 return false;
831 break;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000832
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000833 case Type::Pipe:
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000834 if (!IsStructurallyEquivalent(Context, cast<PipeType>(T1)->getElementType(),
835 cast<PipeType>(T2)->getElementType()))
836 return false;
837 break;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000838 } // end switch
839
840 return true;
841}
842
843/// Determine structural equivalence of two fields.
844static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
845 FieldDecl *Field1, FieldDecl *Field2) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000846 const auto *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000847
848 // For anonymous structs/unions, match up the anonymous struct/union type
849 // declarations directly, so that we don't go off searching for anonymous
850 // types
851 if (Field1->isAnonymousStructOrUnion() &&
852 Field2->isAnonymousStructOrUnion()) {
853 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
854 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
855 return IsStructurallyEquivalent(Context, D1, D2);
856 }
857
858 // Check for equivalent field names.
859 IdentifierInfo *Name1 = Field1->getIdentifier();
860 IdentifierInfo *Name2 = Field2->getIdentifier();
Bruno Cardoso Lopesdf0ee342017-07-01 00:06:47 +0000861 if (!::IsStructurallyEquivalent(Name1, Name2)) {
862 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +0000863 Context.Diag2(
864 Owner2->getLocation(),
865 Context.getApplicableDiagnostic(diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopesdf0ee342017-07-01 00:06:47 +0000866 << Context.ToCtx.getTypeDeclType(Owner2);
867 Context.Diag2(Field2->getLocation(), diag::note_odr_field_name)
868 << Field2->getDeclName();
869 Context.Diag1(Field1->getLocation(), diag::note_odr_field_name)
870 << Field1->getDeclName();
871 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000872 return false;
Bruno Cardoso Lopesdf0ee342017-07-01 00:06:47 +0000873 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000874
875 if (!IsStructurallyEquivalent(Context, Field1->getType(),
876 Field2->getType())) {
877 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +0000878 Context.Diag2(
879 Owner2->getLocation(),
880 Context.getApplicableDiagnostic(diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000881 << Context.ToCtx.getTypeDeclType(Owner2);
882 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
883 << Field2->getDeclName() << Field2->getType();
884 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
885 << Field1->getDeclName() << Field1->getType();
886 }
887 return false;
888 }
889
890 if (Field1->isBitField() != Field2->isBitField()) {
891 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +0000892 Context.Diag2(
893 Owner2->getLocation(),
894 Context.getApplicableDiagnostic(diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000895 << Context.ToCtx.getTypeDeclType(Owner2);
896 if (Field1->isBitField()) {
897 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
898 << Field1->getDeclName() << Field1->getType()
899 << Field1->getBitWidthValue(Context.FromCtx);
900 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
901 << Field2->getDeclName();
902 } else {
903 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
904 << Field2->getDeclName() << Field2->getType()
905 << Field2->getBitWidthValue(Context.ToCtx);
906 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
907 << Field1->getDeclName();
908 }
909 }
910 return false;
911 }
912
913 if (Field1->isBitField()) {
914 // Make sure that the bit-fields are the same length.
915 unsigned Bits1 = Field1->getBitWidthValue(Context.FromCtx);
916 unsigned Bits2 = Field2->getBitWidthValue(Context.ToCtx);
917
918 if (Bits1 != Bits2) {
919 if (Context.Complain) {
920 Context.Diag2(Owner2->getLocation(),
Gabor Marton60768cd2019-04-01 14:46:53 +0000921 Context.getApplicableDiagnostic(
922 diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000923 << Context.ToCtx.getTypeDeclType(Owner2);
924 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
925 << Field2->getDeclName() << Field2->getType() << Bits2;
926 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
927 << Field1->getDeclName() << Field1->getType() << Bits1;
928 }
929 return false;
930 }
931 }
932
933 return true;
934}
935
Raphael Isemannb23ccec2018-12-10 12:37:46 +0000936/// Determine structural equivalence of two methods.
Balazs Keric7797c42018-07-11 09:37:24 +0000937static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
938 CXXMethodDecl *Method1,
939 CXXMethodDecl *Method2) {
940 bool PropertiesEqual =
941 Method1->getDeclKind() == Method2->getDeclKind() &&
942 Method1->getRefQualifier() == Method2->getRefQualifier() &&
943 Method1->getAccess() == Method2->getAccess() &&
944 Method1->getOverloadedOperator() == Method2->getOverloadedOperator() &&
945 Method1->isStatic() == Method2->isStatic() &&
946 Method1->isConst() == Method2->isConst() &&
947 Method1->isVolatile() == Method2->isVolatile() &&
948 Method1->isVirtual() == Method2->isVirtual() &&
949 Method1->isPure() == Method2->isPure() &&
950 Method1->isDefaulted() == Method2->isDefaulted() &&
951 Method1->isDeleted() == Method2->isDeleted();
952 if (!PropertiesEqual)
953 return false;
954 // FIXME: Check for 'final'.
955
956 if (auto *Constructor1 = dyn_cast<CXXConstructorDecl>(Method1)) {
957 auto *Constructor2 = cast<CXXConstructorDecl>(Method2);
958 if (Constructor1->isExplicit() != Constructor2->isExplicit())
959 return false;
960 }
961
962 if (auto *Conversion1 = dyn_cast<CXXConversionDecl>(Method1)) {
963 auto *Conversion2 = cast<CXXConversionDecl>(Method2);
964 if (Conversion1->isExplicit() != Conversion2->isExplicit())
965 return false;
966 if (!IsStructurallyEquivalent(Context, Conversion1->getConversionType(),
967 Conversion2->getConversionType()))
968 return false;
969 }
970
971 const IdentifierInfo *Name1 = Method1->getIdentifier();
972 const IdentifierInfo *Name2 = Method2->getIdentifier();
973 if (!::IsStructurallyEquivalent(Name1, Name2)) {
974 return false;
975 // TODO: Names do not match, add warning like at check for FieldDecl.
976 }
977
978 // Check the prototypes.
979 if (!::IsStructurallyEquivalent(Context,
980 Method1->getType(), Method2->getType()))
981 return false;
982
983 return true;
984}
985
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000986/// Determine structural equivalence of two records.
987static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
988 RecordDecl *D1, RecordDecl *D2) {
989 if (D1->isUnion() != D2->isUnion()) {
990 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +0000991 Context.Diag2(D2->getLocation(), Context.getApplicableDiagnostic(
992 diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000993 << Context.ToCtx.getTypeDeclType(D2);
994 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
995 << D1->getDeclName() << (unsigned)D1->getTagKind();
996 }
997 return false;
998 }
999
Gabor Martonf086fa82018-07-17 12:06:36 +00001000 if (!D1->getDeclName() && !D2->getDeclName()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001001 // If both anonymous structs/unions are in a record context, make sure
1002 // they occur in the same location in the context records.
1003 if (Optional<unsigned> Index1 =
1004 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(D1)) {
1005 if (Optional<unsigned> Index2 =
1006 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(
1007 D2)) {
1008 if (*Index1 != *Index2)
1009 return false;
1010 }
1011 }
1012 }
1013
1014 // If both declarations are class template specializations, we know
1015 // the ODR applies, so check the template and template arguments.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001016 const auto *Spec1 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
1017 const auto *Spec2 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001018 if (Spec1 && Spec2) {
1019 // Check that the specialized templates are the same.
1020 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
1021 Spec2->getSpecializedTemplate()))
1022 return false;
1023
1024 // Check that the template arguments are the same.
1025 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
1026 return false;
1027
1028 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
1029 if (!IsStructurallyEquivalent(Context, Spec1->getTemplateArgs().get(I),
1030 Spec2->getTemplateArgs().get(I)))
1031 return false;
1032 }
1033 // If one is a class template specialization and the other is not, these
1034 // structures are different.
1035 else if (Spec1 || Spec2)
1036 return false;
1037
1038 // Compare the definitions of these two records. If either or both are
Gabor Marton17d39672018-11-26 15:54:08 +00001039 // incomplete (i.e. it is a forward decl), we assume that they are
1040 // equivalent.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001041 D1 = D1->getDefinition();
1042 D2 = D2->getDefinition();
1043 if (!D1 || !D2)
1044 return true;
1045
Gabor Marton26f72a92018-07-12 09:42:05 +00001046 // If any of the records has external storage and we do a minimal check (or
Balazs Keria0a81b12018-08-08 15:04:27 +00001047 // AST import) we assume they are equivalent. (If we didn't have this
Gabor Marton26f72a92018-07-12 09:42:05 +00001048 // assumption then `RecordDecl::LoadFieldsFromExternalStorage` could trigger
1049 // another AST import which in turn would call the structural equivalency
1050 // check again and finally we'd have an improper result.)
1051 if (Context.EqKind == StructuralEquivalenceKind::Minimal)
1052 if (D1->hasExternalLexicalStorage() || D2->hasExternalLexicalStorage())
1053 return true;
1054
Gabor Marton17d39672018-11-26 15:54:08 +00001055 // If one definition is currently being defined, we do not compare for
1056 // equality and we assume that the decls are equal.
1057 if (D1->isBeingDefined() || D2->isBeingDefined())
1058 return true;
1059
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001060 if (auto *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
1061 if (auto *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
Sean Callanan9092d472017-05-13 00:46:33 +00001062 if (D1CXX->hasExternalLexicalStorage() &&
1063 !D1CXX->isCompleteDefinition()) {
1064 D1CXX->getASTContext().getExternalSource()->CompleteType(D1CXX);
1065 }
1066
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001067 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
1068 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +00001069 Context.Diag2(D2->getLocation(),
1070 Context.getApplicableDiagnostic(
1071 diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001072 << Context.ToCtx.getTypeDeclType(D2);
1073 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
1074 << D2CXX->getNumBases();
1075 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
1076 << D1CXX->getNumBases();
1077 }
1078 return false;
1079 }
1080
1081 // Check the base classes.
1082 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
1083 BaseEnd1 = D1CXX->bases_end(),
1084 Base2 = D2CXX->bases_begin();
1085 Base1 != BaseEnd1; ++Base1, ++Base2) {
1086 if (!IsStructurallyEquivalent(Context, Base1->getType(),
1087 Base2->getType())) {
1088 if (Context.Complain) {
1089 Context.Diag2(D2->getLocation(),
Gabor Marton60768cd2019-04-01 14:46:53 +00001090 Context.getApplicableDiagnostic(
1091 diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001092 << Context.ToCtx.getTypeDeclType(D2);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001093 Context.Diag2(Base2->getBeginLoc(), diag::note_odr_base)
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001094 << Base2->getType() << Base2->getSourceRange();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001095 Context.Diag1(Base1->getBeginLoc(), diag::note_odr_base)
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001096 << Base1->getType() << Base1->getSourceRange();
1097 }
1098 return false;
1099 }
1100
1101 // Check virtual vs. non-virtual inheritance mismatch.
1102 if (Base1->isVirtual() != Base2->isVirtual()) {
1103 if (Context.Complain) {
1104 Context.Diag2(D2->getLocation(),
Gabor Marton60768cd2019-04-01 14:46:53 +00001105 Context.getApplicableDiagnostic(
1106 diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001107 << Context.ToCtx.getTypeDeclType(D2);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001108 Context.Diag2(Base2->getBeginLoc(), diag::note_odr_virtual_base)
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001109 << Base2->isVirtual() << Base2->getSourceRange();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001110 Context.Diag1(Base1->getBeginLoc(), diag::note_odr_base)
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001111 << Base1->isVirtual() << Base1->getSourceRange();
1112 }
1113 return false;
1114 }
1115 }
Peter Szecsib180eeb2018-04-25 17:28:03 +00001116
1117 // Check the friends for consistency.
1118 CXXRecordDecl::friend_iterator Friend2 = D2CXX->friend_begin(),
Gabor Marton60768cd2019-04-01 14:46:53 +00001119 Friend2End = D2CXX->friend_end();
Peter Szecsib180eeb2018-04-25 17:28:03 +00001120 for (CXXRecordDecl::friend_iterator Friend1 = D1CXX->friend_begin(),
Gabor Marton60768cd2019-04-01 14:46:53 +00001121 Friend1End = D1CXX->friend_end();
Peter Szecsib180eeb2018-04-25 17:28:03 +00001122 Friend1 != Friend1End; ++Friend1, ++Friend2) {
1123 if (Friend2 == Friend2End) {
1124 if (Context.Complain) {
1125 Context.Diag2(D2->getLocation(),
Gabor Marton60768cd2019-04-01 14:46:53 +00001126 Context.getApplicableDiagnostic(
1127 diag::err_odr_tag_type_inconsistent))
1128 << Context.ToCtx.getTypeDeclType(D2CXX);
Peter Szecsib180eeb2018-04-25 17:28:03 +00001129 Context.Diag1((*Friend1)->getFriendLoc(), diag::note_odr_friend);
1130 Context.Diag2(D2->getLocation(), diag::note_odr_missing_friend);
1131 }
1132 return false;
1133 }
1134
1135 if (!IsStructurallyEquivalent(Context, *Friend1, *Friend2)) {
1136 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +00001137 Context.Diag2(D2->getLocation(),
1138 Context.getApplicableDiagnostic(
1139 diag::err_odr_tag_type_inconsistent))
1140 << Context.ToCtx.getTypeDeclType(D2CXX);
Peter Szecsib180eeb2018-04-25 17:28:03 +00001141 Context.Diag1((*Friend1)->getFriendLoc(), diag::note_odr_friend);
1142 Context.Diag2((*Friend2)->getFriendLoc(), diag::note_odr_friend);
1143 }
1144 return false;
1145 }
1146 }
1147
1148 if (Friend2 != Friend2End) {
1149 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +00001150 Context.Diag2(D2->getLocation(),
1151 Context.getApplicableDiagnostic(
1152 diag::err_odr_tag_type_inconsistent))
1153 << Context.ToCtx.getTypeDeclType(D2);
Peter Szecsib180eeb2018-04-25 17:28:03 +00001154 Context.Diag2((*Friend2)->getFriendLoc(), diag::note_odr_friend);
1155 Context.Diag1(D1->getLocation(), diag::note_odr_missing_friend);
1156 }
1157 return false;
1158 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001159 } else if (D1CXX->getNumBases() > 0) {
1160 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +00001161 Context.Diag2(D2->getLocation(),
1162 Context.getApplicableDiagnostic(
1163 diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001164 << Context.ToCtx.getTypeDeclType(D2);
1165 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001166 Context.Diag1(Base1->getBeginLoc(), diag::note_odr_base)
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001167 << Base1->getType() << Base1->getSourceRange();
1168 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1169 }
1170 return false;
1171 }
1172 }
1173
1174 // Check the fields for consistency.
1175 RecordDecl::field_iterator Field2 = D2->field_begin(),
1176 Field2End = D2->field_end();
1177 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
1178 Field1End = D1->field_end();
1179 Field1 != Field1End; ++Field1, ++Field2) {
1180 if (Field2 == Field2End) {
1181 if (Context.Complain) {
Bruno Cardoso Lopesdf0ee342017-07-01 00:06:47 +00001182 Context.Diag2(D2->getLocation(),
Gabor Marton60768cd2019-04-01 14:46:53 +00001183 Context.getApplicableDiagnostic(
1184 diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001185 << Context.ToCtx.getTypeDeclType(D2);
1186 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1187 << Field1->getDeclName() << Field1->getType();
1188 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1189 }
1190 return false;
1191 }
1192
1193 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
1194 return false;
1195 }
1196
1197 if (Field2 != Field2End) {
1198 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +00001199 Context.Diag2(D2->getLocation(), Context.getApplicableDiagnostic(
1200 diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001201 << Context.ToCtx.getTypeDeclType(D2);
1202 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1203 << Field2->getDeclName() << Field2->getType();
1204 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1205 }
1206 return false;
1207 }
1208
1209 return true;
1210}
1211
1212/// Determine structural equivalence of two enums.
1213static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1214 EnumDecl *D1, EnumDecl *D2) {
Gabor Marton6b01e1c2018-08-09 12:36:25 +00001215
1216 // Compare the definitions of these two enums. If either or both are
1217 // incomplete (i.e. forward declared), we assume that they are equivalent.
1218 D1 = D1->getDefinition();
1219 D2 = D2->getDefinition();
1220 if (!D1 || !D2)
1221 return true;
1222
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001223 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1224 EC2End = D2->enumerator_end();
1225 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1226 EC1End = D1->enumerator_end();
1227 EC1 != EC1End; ++EC1, ++EC2) {
1228 if (EC2 == EC2End) {
1229 if (Context.Complain) {
Bruno Cardoso Lopesdf0ee342017-07-01 00:06:47 +00001230 Context.Diag2(D2->getLocation(),
Gabor Marton60768cd2019-04-01 14:46:53 +00001231 Context.getApplicableDiagnostic(
1232 diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001233 << Context.ToCtx.getTypeDeclType(D2);
1234 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1235 << EC1->getDeclName() << EC1->getInitVal().toString(10);
1236 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1237 }
1238 return false;
1239 }
1240
1241 llvm::APSInt Val1 = EC1->getInitVal();
1242 llvm::APSInt Val2 = EC2->getInitVal();
1243 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
1244 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1245 if (Context.Complain) {
Bruno Cardoso Lopesdf0ee342017-07-01 00:06:47 +00001246 Context.Diag2(D2->getLocation(),
Gabor Marton60768cd2019-04-01 14:46:53 +00001247 Context.getApplicableDiagnostic(
1248 diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001249 << Context.ToCtx.getTypeDeclType(D2);
1250 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1251 << EC2->getDeclName() << EC2->getInitVal().toString(10);
1252 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1253 << EC1->getDeclName() << EC1->getInitVal().toString(10);
1254 }
1255 return false;
1256 }
1257 }
1258
1259 if (EC2 != EC2End) {
1260 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +00001261 Context.Diag2(D2->getLocation(), Context.getApplicableDiagnostic(
1262 diag::err_odr_tag_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001263 << Context.ToCtx.getTypeDeclType(D2);
1264 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1265 << EC2->getDeclName() << EC2->getInitVal().toString(10);
1266 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1267 }
1268 return false;
1269 }
1270
1271 return true;
1272}
1273
1274static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1275 TemplateParameterList *Params1,
1276 TemplateParameterList *Params2) {
1277 if (Params1->size() != Params2->size()) {
1278 if (Context.Complain) {
1279 Context.Diag2(Params2->getTemplateLoc(),
Gabor Marton60768cd2019-04-01 14:46:53 +00001280 Context.getApplicableDiagnostic(
1281 diag::err_odr_different_num_template_parameters))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001282 << Params1->size() << Params2->size();
1283 Context.Diag1(Params1->getTemplateLoc(),
1284 diag::note_odr_template_parameter_list);
1285 }
1286 return false;
1287 }
1288
1289 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1290 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1291 if (Context.Complain) {
1292 Context.Diag2(Params2->getParam(I)->getLocation(),
Gabor Marton60768cd2019-04-01 14:46:53 +00001293 Context.getApplicableDiagnostic(
1294 diag::err_odr_different_template_parameter_kind));
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001295 Context.Diag1(Params1->getParam(I)->getLocation(),
1296 diag::note_odr_template_parameter_here);
1297 }
1298 return false;
1299 }
1300
Gabor Marton950fb572018-07-17 12:39:27 +00001301 if (!IsStructurallyEquivalent(Context, Params1->getParam(I),
1302 Params2->getParam(I)))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001303 return false;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001304 }
1305
1306 return true;
1307}
1308
1309static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1310 TemplateTypeParmDecl *D1,
1311 TemplateTypeParmDecl *D2) {
1312 if (D1->isParameterPack() != D2->isParameterPack()) {
1313 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +00001314 Context.Diag2(D2->getLocation(),
1315 Context.getApplicableDiagnostic(
1316 diag::err_odr_parameter_pack_non_pack))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001317 << D2->isParameterPack();
1318 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1319 << D1->isParameterPack();
1320 }
1321 return false;
1322 }
1323
1324 return true;
1325}
1326
1327static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1328 NonTypeTemplateParmDecl *D1,
1329 NonTypeTemplateParmDecl *D2) {
1330 if (D1->isParameterPack() != D2->isParameterPack()) {
1331 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +00001332 Context.Diag2(D2->getLocation(),
1333 Context.getApplicableDiagnostic(
1334 diag::err_odr_parameter_pack_non_pack))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001335 << D2->isParameterPack();
1336 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1337 << D1->isParameterPack();
1338 }
1339 return false;
1340 }
1341
1342 // Check types.
Gabor Marton950fb572018-07-17 12:39:27 +00001343 if (!IsStructurallyEquivalent(Context, D1->getType(), D2->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001344 if (Context.Complain) {
1345 Context.Diag2(D2->getLocation(),
Gabor Marton60768cd2019-04-01 14:46:53 +00001346 Context.getApplicableDiagnostic(
1347 diag::err_odr_non_type_parameter_type_inconsistent))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001348 << D2->getType() << D1->getType();
1349 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1350 << D1->getType();
1351 }
1352 return false;
1353 }
1354
1355 return true;
1356}
1357
1358static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1359 TemplateTemplateParmDecl *D1,
1360 TemplateTemplateParmDecl *D2) {
1361 if (D1->isParameterPack() != D2->isParameterPack()) {
1362 if (Context.Complain) {
Gabor Marton60768cd2019-04-01 14:46:53 +00001363 Context.Diag2(D2->getLocation(),
1364 Context.getApplicableDiagnostic(
1365 diag::err_odr_parameter_pack_non_pack))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001366 << D2->isParameterPack();
1367 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1368 << D1->isParameterPack();
1369 }
1370 return false;
1371 }
1372
1373 // Check template parameter lists.
1374 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1375 D2->getTemplateParameters());
1376}
1377
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001378static bool IsTemplateDeclCommonStructurallyEquivalent(
1379 StructuralEquivalenceContext &Ctx, TemplateDecl *D1, TemplateDecl *D2) {
1380 if (!IsStructurallyEquivalent(D1->getIdentifier(), D2->getIdentifier()))
1381 return false;
1382 if (!D1->getIdentifier()) // Special name
1383 if (D1->getNameAsString() != D2->getNameAsString())
1384 return false;
1385 return IsStructurallyEquivalent(Ctx, D1->getTemplateParameters(),
1386 D2->getTemplateParameters());
1387}
1388
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001389static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1390 ClassTemplateDecl *D1,
1391 ClassTemplateDecl *D2) {
1392 // Check template parameters.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001393 if (!IsTemplateDeclCommonStructurallyEquivalent(Context, D1, D2))
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001394 return false;
1395
1396 // Check the templated declaration.
Gabor Marton950fb572018-07-17 12:39:27 +00001397 return IsStructurallyEquivalent(Context, D1->getTemplatedDecl(),
1398 D2->getTemplatedDecl());
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001399}
1400
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001401static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1402 FunctionTemplateDecl *D1,
1403 FunctionTemplateDecl *D2) {
1404 // Check template parameters.
1405 if (!IsTemplateDeclCommonStructurallyEquivalent(Context, D1, D2))
1406 return false;
1407
1408 // Check the templated declaration.
Gabor Marton950fb572018-07-17 12:39:27 +00001409 return IsStructurallyEquivalent(Context, D1->getTemplatedDecl()->getType(),
1410 D2->getTemplatedDecl()->getType());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001411}
1412
Peter Szecsib180eeb2018-04-25 17:28:03 +00001413static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1414 FriendDecl *D1, FriendDecl *D2) {
1415 if ((D1->getFriendType() && D2->getFriendDecl()) ||
1416 (D1->getFriendDecl() && D2->getFriendType())) {
1417 return false;
1418 }
1419 if (D1->getFriendType() && D2->getFriendType())
1420 return IsStructurallyEquivalent(Context,
1421 D1->getFriendType()->getType(),
1422 D2->getFriendType()->getType());
1423 if (D1->getFriendDecl() && D2->getFriendDecl())
1424 return IsStructurallyEquivalent(Context, D1->getFriendDecl(),
1425 D2->getFriendDecl());
1426 return false;
1427}
1428
1429static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1430 FunctionDecl *D1, FunctionDecl *D2) {
1431 // FIXME: Consider checking for function attributes as well.
1432 if (!IsStructurallyEquivalent(Context, D1->getType(), D2->getType()))
1433 return false;
1434
1435 return true;
1436}
1437
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001438/// Determine structural equivalence of two declarations.
1439static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1440 Decl *D1, Decl *D2) {
1441 // FIXME: Check for known structural equivalences via a callback of some sort.
1442
1443 // Check whether we already know that these two declarations are not
1444 // structurally equivalent.
1445 if (Context.NonEquivalentDecls.count(
1446 std::make_pair(D1->getCanonicalDecl(), D2->getCanonicalDecl())))
1447 return false;
1448
1449 // Determine whether we've already produced a tentative equivalence for D1.
1450 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1451 if (EquivToD1)
1452 return EquivToD1 == D2->getCanonicalDecl();
1453
1454 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1455 EquivToD1 = D2->getCanonicalDecl();
1456 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1457 return true;
1458}
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001459
1460DiagnosticBuilder StructuralEquivalenceContext::Diag1(SourceLocation Loc,
1461 unsigned DiagID) {
1462 assert(Complain && "Not allowed to complain");
1463 if (LastDiagFromC2)
1464 FromCtx.getDiagnostics().notePriorDiagnosticFrom(ToCtx.getDiagnostics());
1465 LastDiagFromC2 = false;
1466 return FromCtx.getDiagnostics().Report(Loc, DiagID);
1467}
1468
1469DiagnosticBuilder StructuralEquivalenceContext::Diag2(SourceLocation Loc,
1470 unsigned DiagID) {
1471 assert(Complain && "Not allowed to complain");
1472 if (!LastDiagFromC2)
1473 ToCtx.getDiagnostics().notePriorDiagnosticFrom(FromCtx.getDiagnostics());
1474 LastDiagFromC2 = true;
1475 return ToCtx.getDiagnostics().Report(Loc, DiagID);
1476}
1477
1478Optional<unsigned>
1479StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(RecordDecl *Anon) {
1480 ASTContext &Context = Anon->getASTContext();
1481 QualType AnonTy = Context.getRecordType(Anon);
1482
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001483 const auto *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001484 if (!Owner)
1485 return None;
1486
1487 unsigned Index = 0;
1488 for (const auto *D : Owner->noload_decls()) {
1489 const auto *F = dyn_cast<FieldDecl>(D);
1490 if (!F)
1491 continue;
1492
1493 if (F->isAnonymousStructOrUnion()) {
1494 if (Context.hasSameType(F->getType(), AnonTy))
1495 break;
1496 ++Index;
1497 continue;
1498 }
1499
1500 // If the field looks like this:
1501 // struct { ... } A;
1502 QualType FieldType = F->getType();
Aleksei Sidorin499de6c2018-04-05 15:31:49 +00001503 // In case of nested structs.
1504 while (const auto *ElabType = dyn_cast<ElaboratedType>(FieldType))
1505 FieldType = ElabType->getNamedType();
1506
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001507 if (const auto *RecType = dyn_cast<RecordType>(FieldType)) {
1508 const RecordDecl *RecDecl = RecType->getDecl();
1509 if (RecDecl->getDeclContext() == Owner && !RecDecl->getIdentifier()) {
1510 if (Context.hasSameType(FieldType, AnonTy))
1511 break;
1512 ++Index;
1513 continue;
1514 }
1515 }
1516 }
1517
1518 return Index;
1519}
1520
Gabor Marton60768cd2019-04-01 14:46:53 +00001521unsigned StructuralEquivalenceContext::getApplicableDiagnostic(
1522 unsigned ErrorDiagnostic) {
1523 if (ErrorOnTagTypeMismatch)
1524 return ErrorDiagnostic;
1525
1526 switch (ErrorDiagnostic) {
1527 case diag::err_odr_variable_type_inconsistent:
1528 return diag::warn_odr_variable_type_inconsistent;
1529 case diag::err_odr_variable_multiple_def:
1530 return diag::warn_odr_variable_multiple_def;
1531 case diag::err_odr_function_type_inconsistent:
1532 return diag::warn_odr_function_type_inconsistent;
1533 case diag::err_odr_tag_type_inconsistent:
1534 return diag::warn_odr_tag_type_inconsistent;
1535 case diag::err_odr_field_type_inconsistent:
1536 return diag::warn_odr_field_type_inconsistent;
1537 case diag::err_odr_ivar_type_inconsistent:
1538 return diag::warn_odr_ivar_type_inconsistent;
1539 case diag::err_odr_objc_superclass_inconsistent:
1540 return diag::warn_odr_objc_superclass_inconsistent;
1541 case diag::err_odr_objc_method_result_type_inconsistent:
1542 return diag::warn_odr_objc_method_result_type_inconsistent;
1543 case diag::err_odr_objc_method_num_params_inconsistent:
1544 return diag::warn_odr_objc_method_num_params_inconsistent;
1545 case diag::err_odr_objc_method_param_type_inconsistent:
1546 return diag::warn_odr_objc_method_param_type_inconsistent;
1547 case diag::err_odr_objc_method_variadic_inconsistent:
1548 return diag::warn_odr_objc_method_variadic_inconsistent;
1549 case diag::err_odr_objc_property_type_inconsistent:
1550 return diag::warn_odr_objc_property_type_inconsistent;
1551 case diag::err_odr_objc_property_impl_kind_inconsistent:
1552 return diag::warn_odr_objc_property_impl_kind_inconsistent;
1553 case diag::err_odr_objc_synthesize_ivar_inconsistent:
1554 return diag::warn_odr_objc_synthesize_ivar_inconsistent;
1555 case diag::err_odr_different_num_template_parameters:
1556 return diag::warn_odr_different_num_template_parameters;
1557 case diag::err_odr_different_template_parameter_kind:
1558 return diag::warn_odr_different_template_parameter_kind;
1559 case diag::err_odr_parameter_pack_non_pack:
1560 return diag::warn_odr_parameter_pack_non_pack;
1561 case diag::err_odr_non_type_parameter_type_inconsistent:
1562 return diag::warn_odr_non_type_parameter_type_inconsistent;
1563 }
1564}
1565
Gabor Marton950fb572018-07-17 12:39:27 +00001566bool StructuralEquivalenceContext::IsEquivalent(Decl *D1, Decl *D2) {
1567
1568 // Ensure that the implementation functions (all static functions in this TU)
1569 // never call the public ASTStructuralEquivalence::IsEquivalent() functions,
1570 // because that will wreak havoc the internal state (DeclsToCheck and
1571 // TentativeEquivalences members) and can cause faulty behaviour. For
1572 // instance, some leaf declarations can be stated and cached as inequivalent
1573 // as a side effect of one inequivalent element in the DeclsToCheck list.
1574 assert(DeclsToCheck.empty());
1575 assert(TentativeEquivalences.empty());
1576
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001577 if (!::IsStructurallyEquivalent(*this, D1, D2))
1578 return false;
1579
1580 return !Finish();
1581}
1582
Gabor Marton950fb572018-07-17 12:39:27 +00001583bool StructuralEquivalenceContext::IsEquivalent(QualType T1, QualType T2) {
1584 assert(DeclsToCheck.empty());
1585 assert(TentativeEquivalences.empty());
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001586 if (!::IsStructurallyEquivalent(*this, T1, T2))
1587 return false;
1588
1589 return !Finish();
1590}
1591
Balazs Keria0a81b12018-08-08 15:04:27 +00001592bool StructuralEquivalenceContext::CheckCommonEquivalence(Decl *D1, Decl *D2) {
1593 // Check for equivalent described template.
1594 TemplateDecl *Template1 = D1->getDescribedTemplate();
1595 TemplateDecl *Template2 = D2->getDescribedTemplate();
1596 if ((Template1 != nullptr) != (Template2 != nullptr))
1597 return false;
1598 if (Template1 && !IsStructurallyEquivalent(*this, Template1, Template2))
1599 return false;
1600
1601 // FIXME: Move check for identifier names into this function.
1602
1603 return true;
1604}
1605
1606bool StructuralEquivalenceContext::CheckKindSpecificEquivalence(
1607 Decl *D1, Decl *D2) {
1608 // FIXME: Switch on all declaration kinds. For now, we're just going to
1609 // check the obvious ones.
1610 if (auto *Record1 = dyn_cast<RecordDecl>(D1)) {
1611 if (auto *Record2 = dyn_cast<RecordDecl>(D2)) {
1612 // Check for equivalent structure names.
1613 IdentifierInfo *Name1 = Record1->getIdentifier();
1614 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1615 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
1616 IdentifierInfo *Name2 = Record2->getIdentifier();
1617 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1618 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
1619 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1620 !::IsStructurallyEquivalent(*this, Record1, Record2))
1621 return false;
1622 } else {
1623 // Record/non-record mismatch.
1624 return false;
1625 }
1626 } else if (auto *Enum1 = dyn_cast<EnumDecl>(D1)) {
1627 if (auto *Enum2 = dyn_cast<EnumDecl>(D2)) {
1628 // Check for equivalent enum names.
1629 IdentifierInfo *Name1 = Enum1->getIdentifier();
1630 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1631 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
1632 IdentifierInfo *Name2 = Enum2->getIdentifier();
1633 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1634 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
1635 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1636 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1637 return false;
1638 } else {
1639 // Enum/non-enum mismatch
1640 return false;
1641 }
1642 } else if (const auto *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1643 if (const auto *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
1644 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
1645 Typedef2->getIdentifier()) ||
1646 !::IsStructurallyEquivalent(*this, Typedef1->getUnderlyingType(),
1647 Typedef2->getUnderlyingType()))
1648 return false;
1649 } else {
1650 // Typedef/non-typedef mismatch.
1651 return false;
1652 }
1653 } else if (auto *ClassTemplate1 = dyn_cast<ClassTemplateDecl>(D1)) {
1654 if (auto *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1655 if (!::IsStructurallyEquivalent(*this, ClassTemplate1,
1656 ClassTemplate2))
1657 return false;
1658 } else {
1659 // Class template/non-class-template mismatch.
1660 return false;
1661 }
1662 } else if (auto *FunctionTemplate1 = dyn_cast<FunctionTemplateDecl>(D1)) {
1663 if (auto *FunctionTemplate2 = dyn_cast<FunctionTemplateDecl>(D2)) {
1664 if (!::IsStructurallyEquivalent(*this, FunctionTemplate1,
1665 FunctionTemplate2))
1666 return false;
1667 } else {
1668 // Class template/non-class-template mismatch.
1669 return false;
1670 }
1671 } else if (auto *TTP1 = dyn_cast<TemplateTypeParmDecl>(D1)) {
1672 if (auto *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1673 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1674 return false;
1675 } else {
1676 // Kind mismatch.
1677 return false;
1678 }
1679 } else if (auto *NTTP1 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1680 if (auto *NTTP2 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1681 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1682 return false;
1683 } else {
1684 // Kind mismatch.
1685 return false;
1686 }
1687 } else if (auto *TTP1 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1688 if (auto *TTP2 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1689 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1690 return false;
1691 } else {
1692 // Kind mismatch.
1693 return false;
1694 }
1695 } else if (auto *MD1 = dyn_cast<CXXMethodDecl>(D1)) {
1696 if (auto *MD2 = dyn_cast<CXXMethodDecl>(D2)) {
1697 if (!::IsStructurallyEquivalent(*this, MD1, MD2))
1698 return false;
1699 } else {
1700 // Kind mismatch.
1701 return false;
1702 }
1703 } else if (FunctionDecl *FD1 = dyn_cast<FunctionDecl>(D1)) {
1704 if (FunctionDecl *FD2 = dyn_cast<FunctionDecl>(D2)) {
Gabor Martonfc638d62019-02-08 08:55:32 +00001705 if (FD1->isOverloadedOperator()) {
1706 if (!FD2->isOverloadedOperator())
1707 return false;
1708 if (FD1->getOverloadedOperator() != FD2->getOverloadedOperator())
1709 return false;
1710 }
Balazs Keria0a81b12018-08-08 15:04:27 +00001711 if (!::IsStructurallyEquivalent(FD1->getIdentifier(),
1712 FD2->getIdentifier()))
1713 return false;
1714 if (!::IsStructurallyEquivalent(*this, FD1, FD2))
1715 return false;
1716 } else {
1717 // Kind mismatch.
1718 return false;
1719 }
1720 } else if (FriendDecl *FrD1 = dyn_cast<FriendDecl>(D1)) {
1721 if (FriendDecl *FrD2 = dyn_cast<FriendDecl>(D2)) {
1722 if (!::IsStructurallyEquivalent(*this, FrD1, FrD2))
1723 return false;
1724 } else {
1725 // Kind mismatch.
1726 return false;
1727 }
1728 }
1729
1730 return true;
1731}
1732
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001733bool StructuralEquivalenceContext::Finish() {
1734 while (!DeclsToCheck.empty()) {
1735 // Check the next declaration.
1736 Decl *D1 = DeclsToCheck.front();
1737 DeclsToCheck.pop_front();
1738
1739 Decl *D2 = TentativeEquivalences[D1];
1740 assert(D2 && "Unrecorded tentative equivalence?");
1741
Balazs Keria0a81b12018-08-08 15:04:27 +00001742 bool Equivalent =
1743 CheckCommonEquivalence(D1, D2) && CheckKindSpecificEquivalence(D1, D2);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001744
1745 if (!Equivalent) {
1746 // Note that these two declarations are not equivalent (and we already
1747 // know about it).
1748 NonEquivalentDecls.insert(
1749 std::make_pair(D1->getCanonicalDecl(), D2->getCanonicalDecl()));
1750 return true;
1751 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001752 }
1753
1754 return false;
1755}