================
@@ -2075,6 +2070,61 @@ void CGDebugInfo::CollectRecordLambdaFields(
}
}
+/// Try to create an llvm::Constant for a constexpr array of integer elements.
+/// Handles arrays of char, short, int, long with element width up to 64 bits.
+/// Returns nullptr if the array cannot be represented.
+static llvm::Constant *tryEmitConstexprArrayAsConstant(CodeGenModule &CGM,
+ const VarDecl *Var,
+ const APValue *Value) {
+ const auto *ArrayTy =
CGM.getContext().getAsConstantArrayType(Var->getType());
+ if (!ArrayTy)
+ return nullptr;
+
+ const QualType ElemQTy = ArrayTy->getElementType();
+ if (ElemQTy.isNull() || !ElemQTy->isIntegerType())
+ return nullptr;
+
+ const unsigned ElemBitWidth = CGM.getContext().getTypeSize(ElemQTy);
+ // ConstantDataArray only supports 8/16/32/64-bit elements, and
+ // getZExtValue() asserts on wider types (e.g. __int128).
+ if (ElemBitWidth > 64)
+ return nullptr;
+
+ const unsigned NumElts = Value->getArraySize();
+ const unsigned NumInits = Value->getArrayInitializedElts();
+
+ // Preallocate with filler value, then overwrite initialized elements.
+ uint64_t FillVal = 0;
+ if (Value->hasArrayFiller()) {
+ const APValue &Filler = Value->getArrayFiller();
+ if (!Filler.isInt())
+ return nullptr;
+ FillVal = Filler.getInt().getZExtValue();
+ }
+
+ SmallVector<uint64_t, 64> Vals(NumElts, FillVal);
+ for (unsigned I = 0; I < NumInits; ++I) {
+ const APValue &Elt = Value->getArrayInitializedElt(I);
+ if (!Elt.isInt())
+ return nullptr;
----------------
dzhidzhoev wrote:
Can we have non-int initialized elements if array type is integer?
If so, should each element be checked if it fits 64 bits (if non-integer values
may show up here, wide integers may show up here as well, I assume)?
https://github.com/llvm/llvm-project/pull/182442
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits