Skip to content

Commit a9cc64e

Browse files
committed
Separate the MS inheritance model enum from the attribute, NFC
This avoids the need to include Attr.h in DeclCXX.h for a four-value enum. Removing the include will be done separately, since it is large and risky change.
1 parent 4506afe commit a9cc64e

File tree

12 files changed

+149
-137
lines changed

12 files changed

+149
-137
lines changed

clang/include/clang/AST/CXXInheritance.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,30 @@ class CXXFinalOverriderMap
372372
class CXXIndirectPrimaryBaseSet
373373
: public llvm::SmallSet<const CXXRecordDecl*, 32> {};
374374

375+
inline bool
376+
inheritanceModelHasVBPtrOffsetField(MSInheritanceModel Inheritance) {
377+
return Inheritance == MSInheritanceModel::Unspecified;
378+
}
379+
380+
// Only member pointers to functions need a this adjustment, since it can be
381+
// combined with the field offset for data pointers.
382+
inline bool inheritanceModelHasNVOffsetField(bool IsMemberFunction,
383+
MSInheritanceModel Inheritance) {
384+
return IsMemberFunction && Inheritance >= MSInheritanceModel::Multiple;
385+
}
386+
387+
inline bool
388+
inheritanceModelHasVBTableOffsetField(MSInheritanceModel Inheritance) {
389+
return Inheritance >= MSInheritanceModel::Virtual;
390+
}
391+
392+
inline bool inheritanceModelHasOnlyOneField(bool IsMemberFunction,
393+
MSInheritanceModel Inheritance) {
394+
if (IsMemberFunction)
395+
return Inheritance <= MSInheritanceModel::Single;
396+
return Inheritance <= MSInheritanceModel::Multiple;
397+
}
398+
375399
} // namespace clang
376400

377401
#endif // LLVM_CLANG_AST_CXXINHERITANCE_H

clang/include/clang/AST/DeclCXX.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,22 +1736,18 @@ class CXXRecordDecl : public RecordDecl {
17361736
}
17371737

17381738
/// Returns the inheritance model used for this record.
1739-
MSInheritanceAttr::Spelling getMSInheritanceModel() const;
1739+
MSInheritanceModel getMSInheritanceModel() const;
17401740

17411741
/// Calculate what the inheritance model would be for this class.
1742-
MSInheritanceAttr::Spelling calculateInheritanceModel() const;
1742+
MSInheritanceModel calculateInheritanceModel() const;
17431743

17441744
/// In the Microsoft C++ ABI, use zero for the field offset of a null data
17451745
/// member pointer if we can guarantee that zero is not a valid field offset,
17461746
/// or if the member pointer has multiple fields. Polymorphic classes have a
17471747
/// vfptr at offset zero, so we can use zero for null. If there are multiple
17481748
/// fields, we can use zero even if it is a valid field offset because
17491749
/// null-ness testing will check the other fields.
1750-
bool nullFieldOffsetIsZero() const {
1751-
return !MSInheritanceAttr::hasOnlyOneField(/*IsMemberFunction=*/false,
1752-
getMSInheritanceModel()) ||
1753-
(hasDefinition() && isPolymorphic());
1754-
}
1750+
bool nullFieldOffsetIsZero() const;
17551751

17561752
/// Controls when vtordisps will be emitted if this record is used as a
17571753
/// virtual base.

clang/include/clang/Basic/Attr.td

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2990,25 +2990,9 @@ def MSInheritance : InheritableAttr {
29902990
Keyword<"__virtual_inheritance">,
29912991
Keyword<"__unspecified_inheritance">];
29922992
let AdditionalMembers = [{
2993-
static bool hasVBPtrOffsetField(Spelling Inheritance) {
2994-
return Inheritance == Keyword_unspecified_inheritance;
2995-
}
2996-
2997-
// Only member pointers to functions need a this adjustment, since it can be
2998-
// combined with the field offset for data pointers.
2999-
static bool hasNVOffsetField(bool IsMemberFunction, Spelling Inheritance) {
3000-
return IsMemberFunction && Inheritance >= Keyword_multiple_inheritance;
3001-
}
3002-
3003-
static bool hasVBTableOffsetField(Spelling Inheritance) {
3004-
return Inheritance >= Keyword_virtual_inheritance;
3005-
}
3006-
3007-
static bool hasOnlyOneField(bool IsMemberFunction,
3008-
Spelling Inheritance) {
3009-
if (IsMemberFunction)
3010-
return Inheritance <= Keyword_single_inheritance;
3011-
return Inheritance <= Keyword_multiple_inheritance;
2993+
MSInheritanceModel getInheritanceModel() const {
2994+
// The spelling enum should agree with MSInheritanceModel.
2995+
return MSInheritanceModel(getSemanticSpelling());
30122996
}
30132997
}];
30142998
let Documentation = [MSInheritanceDocs];

clang/include/clang/Basic/Specifiers.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,15 @@ namespace clang {
354354
SwiftContext
355355
};
356356

357+
/// Assigned inheritance model for a class in the MS C++ ABI. Must match order
358+
/// of spellings in MSInheritanceAttr.
359+
enum class MSInheritanceModel {
360+
Single = 0,
361+
Multiple = 1,
362+
Virtual = 2,
363+
Unspecified = 3,
364+
};
365+
357366
llvm::StringRef getParameterABISpelling(ParameterABI kind);
358367
} // end namespace clang
359368

clang/include/clang/Sema/Sema.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2800,9 +2800,10 @@ class Sema final {
28002800
StringRef Uuid);
28012801
DLLImportAttr *mergeDLLImportAttr(Decl *D, const AttributeCommonInfo &CI);
28022802
DLLExportAttr *mergeDLLExportAttr(Decl *D, const AttributeCommonInfo &CI);
2803-
MSInheritanceAttr *
2804-
mergeMSInheritanceAttr(Decl *D, const AttributeCommonInfo &CI, bool BestCase,
2805-
MSInheritanceAttr::Spelling SemanticSpelling);
2803+
MSInheritanceAttr *mergeMSInheritanceAttr(Decl *D,
2804+
const AttributeCommonInfo &CI,
2805+
bool BestCase,
2806+
MSInheritanceModel Model);
28062807
FormatAttr *mergeFormatAttr(Decl *D, const AttributeCommonInfo &CI,
28072808
IdentifierInfo *Format, int FormatIdx,
28082809
int FirstArg);
@@ -3740,7 +3741,7 @@ class Sema final {
37403741
bool checkTargetAttr(SourceLocation LiteralLoc, StringRef Str);
37413742
bool checkMSInheritanceAttrOnDefinition(
37423743
CXXRecordDecl *RD, SourceRange Range, bool BestCase,
3743-
MSInheritanceAttr::Spelling SemanticSpelling);
3744+
MSInheritanceModel SemanticSpelling);
37443745

37453746
void CheckAlignasUnderalignment(Decl *D);
37463747

clang/lib/AST/MicrosoftCXXABI.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "CXXABI.h"
1515
#include "clang/AST/ASTContext.h"
1616
#include "clang/AST/Attr.h"
17+
#include "clang/AST/CXXInheritance.h"
1718
#include "clang/AST/DeclCXX.h"
1819
#include "clang/AST/MangleNumberingContext.h"
1920
#include "clang/AST/RecordLayout.h"
@@ -154,21 +155,26 @@ static bool usesMultipleInheritanceModel(const CXXRecordDecl *RD) {
154155
return false;
155156
}
156157

157-
MSInheritanceAttr::Spelling CXXRecordDecl::calculateInheritanceModel() const {
158+
MSInheritanceModel CXXRecordDecl::calculateInheritanceModel() const {
158159
if (!hasDefinition() || isParsingBaseSpecifiers())
159-
return MSInheritanceAttr::Keyword_unspecified_inheritance;
160+
return MSInheritanceModel::Unspecified;
160161
if (getNumVBases() > 0)
161-
return MSInheritanceAttr::Keyword_virtual_inheritance;
162+
return MSInheritanceModel::Virtual;
162163
if (usesMultipleInheritanceModel(this))
163-
return MSInheritanceAttr::Keyword_multiple_inheritance;
164-
return MSInheritanceAttr::Keyword_single_inheritance;
164+
return MSInheritanceModel::Multiple;
165+
return MSInheritanceModel::Single;
165166
}
166167

167-
MSInheritanceAttr::Spelling
168-
CXXRecordDecl::getMSInheritanceModel() const {
168+
MSInheritanceModel CXXRecordDecl::getMSInheritanceModel() const {
169169
MSInheritanceAttr *IA = getAttr<MSInheritanceAttr>();
170170
assert(IA && "Expected MSInheritanceAttr on the CXXRecordDecl!");
171-
return IA->getSemanticSpelling();
171+
return IA->getInheritanceModel();
172+
}
173+
174+
bool CXXRecordDecl::nullFieldOffsetIsZero() const {
175+
return !inheritanceModelHasOnlyOneField(/*IsMemberFunction=*/false,
176+
getMSInheritanceModel()) ||
177+
(hasDefinition() && isPolymorphic());
172178
}
173179

174180
MSVtorDispMode CXXRecordDecl::getMSVtorDispMode() const {
@@ -209,19 +215,19 @@ MSVtorDispMode CXXRecordDecl::getMSVtorDispMode() const {
209215
static std::pair<unsigned, unsigned>
210216
getMSMemberPointerSlots(const MemberPointerType *MPT) {
211217
const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
212-
MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
218+
MSInheritanceModel Inheritance = RD->getMSInheritanceModel();
213219
unsigned Ptrs = 0;
214220
unsigned Ints = 0;
215221
if (MPT->isMemberFunctionPointer())
216222
Ptrs = 1;
217223
else
218224
Ints = 1;
219-
if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(),
225+
if (inheritanceModelHasNVOffsetField(MPT->isMemberFunctionPointer(),
220226
Inheritance))
221227
Ints++;
222-
if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance))
228+
if (inheritanceModelHasVBPtrOffsetField(Inheritance))
223229
Ints++;
224-
if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance))
230+
if (inheritanceModelHasVBTableOffsetField(Inheritance))
225231
Ints++;
226232
return std::make_pair(Ptrs, Ints);
227233
}

clang/lib/AST/MicrosoftMangle.cpp

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ void MicrosoftCXXNameMangler::mangleMemberDataPointer(const CXXRecordDecl *RD,
592592

593593
int64_t FieldOffset;
594594
int64_t VBTableOffset;
595-
MSInheritanceAttr::Spelling IM = RD->getMSInheritanceModel();
595+
MSInheritanceModel IM = RD->getMSInheritanceModel();
596596
if (VD) {
597597
FieldOffset = getASTContext().getFieldOffset(VD);
598598
assert(FieldOffset % getASTContext().getCharWidth() == 0 &&
@@ -601,7 +601,7 @@ void MicrosoftCXXNameMangler::mangleMemberDataPointer(const CXXRecordDecl *RD,
601601

602602
VBTableOffset = 0;
603603

604-
if (IM == MSInheritanceAttr::Keyword_virtual_inheritance)
604+
if (IM == MSInheritanceModel::Virtual)
605605
FieldOffset -= getASTContext().getOffsetOfBaseWithVBPtr(RD).getQuantity();
606606
} else {
607607
FieldOffset = RD->nullFieldOffsetIsZero() ? 0 : -1;
@@ -611,12 +611,10 @@ void MicrosoftCXXNameMangler::mangleMemberDataPointer(const CXXRecordDecl *RD,
611611

612612
char Code = '\0';
613613
switch (IM) {
614-
case MSInheritanceAttr::Keyword_single_inheritance: Code = '0'; break;
615-
case MSInheritanceAttr::Keyword_multiple_inheritance: Code = '0'; break;
616-
case MSInheritanceAttr::Keyword_virtual_inheritance: Code = 'F'; break;
617-
case MSInheritanceAttr::Keyword_unspecified_inheritance: Code = 'G'; break;
618-
case MSInheritanceAttr::SpellingNotCalculated:
619-
llvm_unreachable("not reachable");
614+
case MSInheritanceModel::Single: Code = '0'; break;
615+
case MSInheritanceModel::Multiple: Code = '0'; break;
616+
case MSInheritanceModel::Virtual: Code = 'F'; break;
617+
case MSInheritanceModel::Unspecified: Code = 'G'; break;
620618
}
621619

622620
Out << '$' << Code;
@@ -626,9 +624,9 @@ void MicrosoftCXXNameMangler::mangleMemberDataPointer(const CXXRecordDecl *RD,
626624
// The C++ standard doesn't allow base-to-derived member pointer conversions
627625
// in template parameter contexts, so the vbptr offset of data member pointers
628626
// is always zero.
629-
if (MSInheritanceAttr::hasVBPtrOffsetField(IM))
627+
if (inheritanceModelHasVBPtrOffsetField(IM))
630628
mangleNumber(0);
631-
if (MSInheritanceAttr::hasVBTableOffsetField(IM))
629+
if (inheritanceModelHasVBTableOffsetField(IM))
632630
mangleNumber(VBTableOffset);
633631
}
634632

@@ -640,16 +638,14 @@ MicrosoftCXXNameMangler::mangleMemberFunctionPointer(const CXXRecordDecl *RD,
640638
// ::= $I? <name> <number> <number>
641639
// ::= $J? <name> <number> <number> <number>
642640

643-
MSInheritanceAttr::Spelling IM = RD->getMSInheritanceModel();
641+
MSInheritanceModel IM = RD->getMSInheritanceModel();
644642

645643
char Code = '\0';
646644
switch (IM) {
647-
case MSInheritanceAttr::Keyword_single_inheritance: Code = '1'; break;
648-
case MSInheritanceAttr::Keyword_multiple_inheritance: Code = 'H'; break;
649-
case MSInheritanceAttr::Keyword_virtual_inheritance: Code = 'I'; break;
650-
case MSInheritanceAttr::Keyword_unspecified_inheritance: Code = 'J'; break;
651-
case MSInheritanceAttr::SpellingNotCalculated:
652-
llvm_unreachable("not reachable");
645+
case MSInheritanceModel::Single: Code = '1'; break;
646+
case MSInheritanceModel::Multiple: Code = 'H'; break;
647+
case MSInheritanceModel::Virtual: Code = 'I'; break;
648+
case MSInheritanceModel::Unspecified: Code = 'J'; break;
653649
}
654650

655651
// If non-virtual, mangle the name. If virtual, mangle as a virtual memptr
@@ -676,25 +672,24 @@ MicrosoftCXXNameMangler::mangleMemberFunctionPointer(const CXXRecordDecl *RD,
676672
mangleFunctionEncoding(MD, /*ShouldMangle=*/true);
677673
}
678674

679-
if (VBTableOffset == 0 &&
680-
IM == MSInheritanceAttr::Keyword_virtual_inheritance)
675+
if (VBTableOffset == 0 && IM == MSInheritanceModel::Virtual)
681676
NVOffset -= getASTContext().getOffsetOfBaseWithVBPtr(RD).getQuantity();
682677
} else {
683678
// Null single inheritance member functions are encoded as a simple nullptr.
684-
if (IM == MSInheritanceAttr::Keyword_single_inheritance) {
679+
if (IM == MSInheritanceModel::Single) {
685680
Out << "$0A@";
686681
return;
687682
}
688-
if (IM == MSInheritanceAttr::Keyword_unspecified_inheritance)
683+
if (IM == MSInheritanceModel::Unspecified)
689684
VBTableOffset = -1;
690685
Out << '$' << Code;
691686
}
692687

693-
if (MSInheritanceAttr::hasNVOffsetField(/*IsMemberFunction=*/true, IM))
688+
if (inheritanceModelHasNVOffsetField(/*IsMemberFunction=*/true, IM))
694689
mangleNumber(static_cast<uint32_t>(NVOffset));
695-
if (MSInheritanceAttr::hasVBPtrOffsetField(IM))
690+
if (inheritanceModelHasVBPtrOffsetField(IM))
696691
mangleNumber(VBPtrOffset);
697-
if (MSInheritanceAttr::hasVBTableOffsetField(IM))
692+
if (inheritanceModelHasVBTableOffsetField(IM))
698693
mangleNumber(VBTableOffset);
699694
}
700695

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2733,19 +2733,17 @@ llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty,
27332733
// Set the MS inheritance model. There is no flag for the unspecified model.
27342734
if (CGM.getTarget().getCXXABI().isMicrosoft()) {
27352735
switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) {
2736-
case MSInheritanceAttr::Keyword_single_inheritance:
2736+
case MSInheritanceModel::Single:
27372737
Flags |= llvm::DINode::FlagSingleInheritance;
27382738
break;
2739-
case MSInheritanceAttr::Keyword_multiple_inheritance:
2739+
case MSInheritanceModel::Multiple:
27402740
Flags |= llvm::DINode::FlagMultipleInheritance;
27412741
break;
2742-
case MSInheritanceAttr::Keyword_virtual_inheritance:
2742+
case MSInheritanceModel::Virtual:
27432743
Flags |= llvm::DINode::FlagVirtualInheritance;
27442744
break;
2745-
case MSInheritanceAttr::Keyword_unspecified_inheritance:
2745+
case MSInheritanceModel::Unspecified:
27462746
break;
2747-
case MSInheritanceAttr::SpellingNotCalculated:
2748-
llvm_unreachable("Spelling not yet calculated");
27492747
}
27502748
}
27512749
}

0 commit comments

Comments
 (0)