[cfe-commits] r51285 - /cfe/trunk/lib/CodeGen/CGExprConstant.cpp
Eli Friedman
eli.friedman at gmail.com
Mon May 19 17:11:07 PDT 2008
Author: efriedma
Date: Mon May 19 19:11:07 2008
New Revision: 51285
URL: http://llvm.org/viewvc/llvm-project?rev=51285&view=rev
Log:
Restructure constant structure init codegen so that it's possible to
implement bitfield codegen (although I don't envy the person who
implements it). This also prevents a crash on code like that from PR2309
(it's still broken, but it fails more gracefully).
Modified:
cfe/trunk/lib/CodeGen/CGExprConstant.cpp
Modified: cfe/trunk/lib/CodeGen/CGExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprConstant.cpp?rev=51285&r1=51284&r2=51285&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprConstant.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprConstant.cpp Mon May 19 19:11:07 2008
@@ -91,7 +91,6 @@
// Copy initializer elements.
unsigned i = 0;
for (; i < NumInitableElts; ++i) {
-
llvm::Constant *C = Visit(ILE->getInit(i));
assert (C && "Failed to create initializer expression");
Elts.push_back(C);
@@ -107,42 +106,35 @@
llvm::Constant *EmitStructInitialization(InitListExpr *ILE,
const llvm::StructType *SType) {
- TagDecl *TD = ILE->getType()->getAsRecordType()->getDecl();
+ RecordDecl *RD = ILE->getType()->getAsRecordType()->getDecl();
std::vector<llvm::Constant*> Elts;
- const CGRecordLayout *CGR = CGM.getTypes().getCGRecordLayout(TD);
- unsigned NumInitElements = ILE->getNumInits();
- unsigned NumElements = SType->getNumElements();
-
- // Initialising an structure requires us to automatically
- // initialise any elements that have not been initialised explicitly
- unsigned NumInitableElts = std::min(NumInitElements, NumElements);
+
+ // Initialize the whole structure to zero.
+ for (unsigned i = 0; i < SType->getNumElements(); ++i) {
+ const llvm::Type *FieldTy = SType->getElementType(i);
+ Elts.push_back(llvm::Constant::getNullValue(FieldTy));
+ }
// Copy initializer elements. Skip padding fields.
unsigned EltNo = 0; // Element no in ILE
- unsigned FieldNo = 0; // Field no in SType
- while (EltNo < NumInitableElts) {
-
- // Zero initialize padding field.
- if (CGR->isPaddingField(FieldNo)) {
- const llvm::Type *FieldTy = SType->getElementType(FieldNo);
- Elts.push_back(llvm::Constant::getNullValue(FieldTy));
- FieldNo++;
+ int FieldNo = 0; // Field no in RecordDecl
+ while (EltNo < ILE->getNumInits() && FieldNo < RD->getNumMembers()) {
+ FieldDecl* curField = RD->getMember(FieldNo);
+ FieldNo++;
+ if (!curField->getIdentifier())
continue;
- }
-
+
llvm::Constant *C = Visit(ILE->getInit(EltNo));
assert (C && "Failed to create initializer expression");
- Elts.push_back(C);
+
+ if (curField->isBitField()) {
+ CGM.WarnUnsupported(ILE->getInit(EltNo), "bitfield initialization");
+ } else {
+ Elts[CGM.getTypes().getLLVMFieldNo(curField)] = C;
+ }
EltNo++;
- FieldNo++;
- }
-
- // Initialize remaining structure elements.
- for (unsigned i = Elts.size(); i < NumElements; ++i) {
- const llvm::Type *FieldTy = SType->getElementType(i);
- Elts.push_back(llvm::Constant::getNullValue(FieldTy));
}
-
+
return llvm::ConstantStruct::get(SType, Elts);
}
@@ -158,7 +150,6 @@
// Copy initializer elements.
unsigned i = 0;
for (; i < NumElements; ++i) {
-
llvm::Constant *C = Visit(ILE->getInit(i));
assert (C && "Failed to create initializer expression");
Elts.push_back(C);
More information about the cfe-commits
mailing list