[clang] [CIR] Upstream support for accessing structure members (PR #136383)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 21 12:10:59 PDT 2025
================
@@ -309,6 +309,37 @@ RecordType::computeStructAlignment(const mlir::DataLayout &dataLayout) const {
return recordAlignment;
}
+uint64_t RecordType::getElementOffset(const ::mlir::DataLayout &dataLayout,
+ unsigned idx) const {
+ assert(idx < getMembers().size() && "access not valid");
+
+ // All union elements are at offset zero.
+ if (isUnion() || idx == 0)
+ return 0;
+
+ assert(isComplete() && "Cannot get layout of incomplete records");
+ assert(idx < getNumElements());
+ llvm::ArrayRef<mlir::Type> members = getMembers();
+
+ unsigned offset = 0;
+
+ for (unsigned i = 0, e = idx; i != e; ++i) {
----------------
andykaylor wrote:
> Is there good reason to not use range-for here? Something like:
>
> `for (mlir::Type ty : std::make_range(members.begin(), std::next(members.begin(), idx)))` (though there is perhaps some ADT stuff to do a better job of this)
Given that it wasn't ending at the end, it seemed just as well to iterate like this. Is there an advantage to the range-for in this case, given the extra calls to set it up?
https://github.com/llvm/llvm-project/pull/136383
More information about the cfe-commits
mailing list