Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions clang/include/clang/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4524,6 +4524,11 @@ class RecordDecl : public TagDecl {
return field_begin() == field_end();
}

/// Returns the number of fields (non-static data members) in this record.
unsigned getNumFields() const {
return std::distance(field_begin(), field_end());
}

/// noload_fields - Iterate over the fields stored in this record
/// that are currently loaded; don't attempt to retrieve anything
/// from an external source.
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/ComparisonCategories.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ bool ComparisonCategoryInfo::ValueInfo::hasValidIntValue() const {
// Before we attempt to get the value of the first field, ensure that we
// actually have one (and only one) field.
const auto *Record = VD->getType()->getAsCXXRecordDecl();
if (std::distance(Record->field_begin(), Record->field_end()) != 1 ||
if (Record->getNumFields() != 1 ||
!Record->field_begin()->getType()->isIntegralOrEnumerationType())
return false;

Expand Down
19 changes: 8 additions & 11 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3971,8 +3971,7 @@ static bool constructAggregate(EvalInfo &Info, const FPOptions FPO,
if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
NumBases = CXXRD->getNumBases();

*Res = APValue(APValue::UninitStruct(), NumBases,
std::distance(RD->field_begin(), RD->field_end()));
*Res = APValue(APValue::UninitStruct(), NumBases, RD->getNumFields());

SmallVector<std::tuple<APValue *, QualType, unsigned>> ReverseList;
// we need to traverse backwards
Expand Down Expand Up @@ -5529,8 +5528,8 @@ static bool handleDefaultInitValue(QualType T, APValue &Result) {
Result = APValue((const FieldDecl *)nullptr);
return true;
}
Result = APValue(APValue::UninitStruct(), RD->getNumBases(),
std::distance(RD->field_begin(), RD->field_end()));
Result =
APValue(APValue::UninitStruct(), RD->getNumBases(), RD->getNumFields());

unsigned Index = 0;
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Expand Down Expand Up @@ -7184,7 +7183,7 @@ static bool HandleConstructorCall(const Expr *E, const LValue &This,
if (!Result.hasValue()) {
if (!RD->isUnion())
Result = APValue(APValue::UninitStruct(), RD->getNumBases(),
std::distance(RD->field_begin(), RD->field_end()));
RD->getNumFields());
else
// A union starts with no active member.
Result = APValue((const FieldDecl*)nullptr);
Expand Down Expand Up @@ -8135,8 +8134,7 @@ class BufferToAPValueConverter {
if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
NumBases = CXXRD->getNumBases();

APValue ResultVal(APValue::UninitStruct(), NumBases,
std::distance(RD->field_begin(), RD->field_end()));
APValue ResultVal(APValue::UninitStruct(), NumBases, RD->getNumFields());

// Visit the base classes.
if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Expand Down Expand Up @@ -11146,7 +11144,7 @@ static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E,
assert(!RD->isUnion() && "Expected non-union class type");
const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD);
Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0,
std::distance(RD->field_begin(), RD->field_end()));
RD->getNumFields());

if (RD->isInvalidDecl()) return false;
const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
Expand Down Expand Up @@ -11342,7 +11340,7 @@ bool RecordExprEvaluator::VisitCXXParenListOrInitListExpr(

if (!Result.hasValue())
Result = APValue(APValue::UninitStruct(), CXXRD ? CXXRD->getNumBases() : 0,
std::distance(RD->field_begin(), RD->field_end()));
RD->getNumFields());
unsigned ElementNo = 0;
bool Success = true;

Expand Down Expand Up @@ -11549,8 +11547,7 @@ bool RecordExprEvaluator::VisitLambdaExpr(const LambdaExpr *E) {
if (ClosureClass->isInvalidDecl())
return false;

const size_t NumFields =
std::distance(ClosureClass->field_begin(), ClosureClass->field_end());
const size_t NumFields = ClosureClass->getNumFields();

assert(NumFields == (size_t)std::distance(E->capture_init_begin(),
E->capture_init_end()) &&
Expand Down
6 changes: 2 additions & 4 deletions clang/lib/CodeGen/CGHLSLRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -816,8 +816,7 @@ CGHLSLRuntime::handleStructSemanticLoad(
const llvm::StructType *ST = cast<StructType>(Type);
const clang::RecordDecl *RD = Decl->getType()->getAsRecordDecl();

assert(std::distance(RD->field_begin(), RD->field_end()) ==
ST->getNumElements());
assert(RD->getNumFields() == ST->getNumElements());

llvm::Value *Aggregate = llvm::PoisonValue::get(Type);
auto FieldDecl = RD->field_begin();
Expand Down Expand Up @@ -849,8 +848,7 @@ CGHLSLRuntime::handleStructSemanticStore(
RD = Decl->getType()->getAsRecordDecl();
assert(RD);

assert(std::distance(RD->field_begin(), RD->field_end()) ==
ST->getNumElements());
assert(RD->getNumFields() == ST->getNumElements());

auto FieldDecl = RD->field_begin();
for (unsigned I = 0; I < ST->getNumElements(); ++I) {
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/Sema/CodeCompleteConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,7 @@ unsigned CodeCompleteConsumer::OverloadCandidate::getNumParams() const {
return Template->getTemplateParameters()->size();

if (Kind == CK_Aggregate) {
unsigned Count =
std::distance(AggregateType->field_begin(), AggregateType->field_end());
unsigned Count = AggregateType->getNumFields();
if (const auto *CRD = dyn_cast<CXXRecordDecl>(AggregateType))
Count += CRD->getNumBases();
return Count;
Expand Down