Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 1 | //===--- CGExprConstant.cpp - Emit LLVM Code from Constant Expressions ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to emit Constant Expr nodes as LLVM code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | #include "CodeGenModule.h" |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 16 | #include "CGCXXABI.h" |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 17 | #include "CGObjCRuntime.h" |
Daniel Dunbar | 2924ade | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 18 | #include "CGRecordLayout.h" |
Chris Lattner | f6b24ea | 2008-10-06 05:59:01 +0000 | [diff] [blame] | 19 | #include "clang/AST/APValue.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 21 | #include "clang/AST/RecordLayout.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 22 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 23 | #include "clang/Basic/Builtins.h" |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 24 | #include "llvm/Constants.h" |
| 25 | #include "llvm/Function.h" |
| 26 | #include "llvm/GlobalVariable.h" |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetData.h" |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | using namespace CodeGen; |
| 30 | |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // ConstStructBuilder |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
| 35 | namespace { |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 36 | class ConstStructBuilder { |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 37 | CodeGenModule &CGM; |
| 38 | CodeGenFunction *CGF; |
| 39 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 40 | bool Packed; |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 41 | CharUnits NextFieldOffsetInChars; |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 42 | CharUnits LLVMStructAlignment; |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 43 | std::vector<llvm::Constant *> Elements; |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 44 | public: |
| 45 | static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF, |
| 46 | InitListExpr *ILE); |
| 47 | |
| 48 | private: |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 49 | ConstStructBuilder(CodeGenModule &CGM, CodeGenFunction *CGF) |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 50 | : CGM(CGM), CGF(CGF), Packed(false), |
| 51 | NextFieldOffsetInChars(CharUnits::Zero()), |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 52 | LLVMStructAlignment(CharUnits::One()) { } |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 53 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | bool AppendField(const FieldDecl *Field, uint64_t FieldOffset, |
Chris Lattner | 8ce9e45 | 2010-04-13 18:16:19 +0000 | [diff] [blame] | 55 | llvm::Constant *InitExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 1880666 | 2010-07-05 17:04:23 +0000 | [diff] [blame] | 57 | void AppendBitField(const FieldDecl *Field, uint64_t FieldOffset, |
| 58 | llvm::ConstantInt *InitExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | |
Ken Dyck | c29ea8f | 2011-03-11 23:42:54 +0000 | [diff] [blame] | 60 | void AppendPadding(CharUnits PadSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
Ken Dyck | 106ca04 | 2011-03-11 02:17:05 +0000 | [diff] [blame] | 62 | void AppendTailPadding(CharUnits RecordSize); |
Anders Carlsson | d24393b | 2009-07-27 01:23:51 +0000 | [diff] [blame] | 63 | |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 64 | void ConvertStructToPacked(); |
Anders Carlsson | 31f2f9c | 2009-10-02 02:15:20 +0000 | [diff] [blame] | 65 | |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 66 | bool Build(InitListExpr *ILE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 67 | |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 68 | CharUnits getAlignment(const llvm::Constant *C) const { |
| 69 | if (Packed) return CharUnits::One(); |
| 70 | return CharUnits::fromQuantity( |
| 71 | CGM.getTargetData().getABITypeAlignment(C->getType())); |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 72 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | |
Ken Dyck | 15c152e | 2011-03-18 01:12:13 +0000 | [diff] [blame] | 74 | CharUnits getSizeInChars(const llvm::Constant *C) const { |
| 75 | return CharUnits::fromQuantity( |
| 76 | CGM.getTargetData().getTypeAllocSize(C->getType())); |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 77 | } |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 78 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 80 | bool ConstStructBuilder:: |
Chris Lattner | 8ce9e45 | 2010-04-13 18:16:19 +0000 | [diff] [blame] | 81 | AppendField(const FieldDecl *Field, uint64_t FieldOffset, |
| 82 | llvm::Constant *InitCst) { |
Ken Dyck | 6925cc4 | 2011-03-15 01:09:02 +0000 | [diff] [blame] | 83 | |
| 84 | const ASTContext &Context = CGM.getContext(); |
| 85 | |
| 86 | CharUnits FieldOffsetInChars = Context.toCharUnitsFromBits(FieldOffset); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 87 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 88 | assert(NextFieldOffsetInChars <= FieldOffsetInChars |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 89 | && "Field offset mismatch!"); |
| 90 | |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 91 | CharUnits FieldAlignment = getAlignment(InitCst); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 92 | |
| 93 | // Round up the field offset to the alignment of the field type. |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 94 | CharUnits AlignedNextFieldOffsetInChars = |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 95 | NextFieldOffsetInChars.RoundUpToAlignment(FieldAlignment); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 96 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 97 | if (AlignedNextFieldOffsetInChars > FieldOffsetInChars) { |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 98 | assert(!Packed && "Alignment is wrong even with a packed struct!"); |
| 99 | |
| 100 | // Convert the struct to a packed struct. |
| 101 | ConvertStructToPacked(); |
| 102 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 103 | AlignedNextFieldOffsetInChars = NextFieldOffsetInChars; |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 106 | if (AlignedNextFieldOffsetInChars < FieldOffsetInChars) { |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 107 | // We need to append padding. |
Ken Dyck | c29ea8f | 2011-03-11 23:42:54 +0000 | [diff] [blame] | 108 | AppendPadding( |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 109 | FieldOffsetInChars - NextFieldOffsetInChars); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 110 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 111 | assert(NextFieldOffsetInChars == FieldOffsetInChars && |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 112 | "Did not add enough padding!"); |
| 113 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 114 | AlignedNextFieldOffsetInChars = NextFieldOffsetInChars; |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // Add the field. |
Chris Lattner | 8ce9e45 | 2010-04-13 18:16:19 +0000 | [diff] [blame] | 118 | Elements.push_back(InitCst); |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 119 | NextFieldOffsetInChars = AlignedNextFieldOffsetInChars + |
Ken Dyck | 15c152e | 2011-03-18 01:12:13 +0000 | [diff] [blame] | 120 | getSizeInChars(InitCst); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 121 | |
| 122 | if (Packed) |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 123 | assert(LLVMStructAlignment == CharUnits::One() && |
| 124 | "Packed struct not byte-aligned!"); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 125 | else |
| 126 | LLVMStructAlignment = std::max(LLVMStructAlignment, FieldAlignment); |
| 127 | |
| 128 | return true; |
| 129 | } |
| 130 | |
Chris Lattner | 1880666 | 2010-07-05 17:04:23 +0000 | [diff] [blame] | 131 | void ConstStructBuilder::AppendBitField(const FieldDecl *Field, |
| 132 | uint64_t FieldOffset, |
| 133 | llvm::ConstantInt *CI) { |
Ken Dyck | f004da4 | 2011-03-12 12:03:11 +0000 | [diff] [blame] | 134 | const ASTContext &Context = CGM.getContext(); |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 135 | const uint64_t CharWidth = Context.getCharWidth(); |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 136 | uint64_t NextFieldOffsetInBits = Context.toBits(NextFieldOffsetInChars); |
| 137 | if (FieldOffset > NextFieldOffsetInBits) { |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 138 | // We need to add padding. |
Ken Dyck | f004da4 | 2011-03-12 12:03:11 +0000 | [diff] [blame] | 139 | CharUnits PadSize = Context.toCharUnitsFromBits( |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 140 | llvm::RoundUpToAlignment(FieldOffset - NextFieldOffsetInBits, |
Ken Dyck | f004da4 | 2011-03-12 12:03:11 +0000 | [diff] [blame] | 141 | Context.Target.getCharAlign())); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 142 | |
Ken Dyck | f004da4 | 2011-03-12 12:03:11 +0000 | [diff] [blame] | 143 | AppendPadding(PadSize); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | uint64_t FieldSize = |
Ken Dyck | f004da4 | 2011-03-12 12:03:11 +0000 | [diff] [blame] | 147 | Field->getBitWidth()->EvaluateAsInt(Context).getZExtValue(); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 148 | |
| 149 | llvm::APInt FieldValue = CI->getValue(); |
| 150 | |
| 151 | // Promote the size of FieldValue if necessary |
| 152 | // FIXME: This should never occur, but currently it can because initializer |
| 153 | // constants are cast to bool, and because clang is not enforcing bitfield |
| 154 | // width limits. |
| 155 | if (FieldSize > FieldValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 156 | FieldValue = FieldValue.zext(FieldSize); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 157 | |
| 158 | // Truncate the size of FieldValue to the bit field size. |
| 159 | if (FieldSize < FieldValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 160 | FieldValue = FieldValue.trunc(FieldSize); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 161 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 162 | NextFieldOffsetInBits = Context.toBits(NextFieldOffsetInChars); |
| 163 | if (FieldOffset < NextFieldOffsetInBits) { |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 164 | // Either part of the field or the entire field can go into the previous |
| 165 | // byte. |
| 166 | assert(!Elements.empty() && "Elements can't be empty!"); |
| 167 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 168 | unsigned BitsInPreviousByte = NextFieldOffsetInBits - FieldOffset; |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 169 | |
| 170 | bool FitsCompletelyInPreviousByte = |
| 171 | BitsInPreviousByte >= FieldValue.getBitWidth(); |
| 172 | |
| 173 | llvm::APInt Tmp = FieldValue; |
| 174 | |
| 175 | if (!FitsCompletelyInPreviousByte) { |
| 176 | unsigned NewFieldWidth = FieldSize - BitsInPreviousByte; |
| 177 | |
| 178 | if (CGM.getTargetData().isBigEndian()) { |
| 179 | Tmp = Tmp.lshr(NewFieldWidth); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 180 | Tmp = Tmp.trunc(BitsInPreviousByte); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 181 | |
| 182 | // We want the remaining high bits. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 183 | FieldValue = FieldValue.trunc(NewFieldWidth); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 184 | } else { |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 185 | Tmp = Tmp.trunc(BitsInPreviousByte); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 186 | |
| 187 | // We want the remaining low bits. |
| 188 | FieldValue = FieldValue.lshr(BitsInPreviousByte); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 189 | FieldValue = FieldValue.trunc(NewFieldWidth); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 193 | Tmp = Tmp.zext(CharWidth); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 194 | if (CGM.getTargetData().isBigEndian()) { |
| 195 | if (FitsCompletelyInPreviousByte) |
| 196 | Tmp = Tmp.shl(BitsInPreviousByte - FieldValue.getBitWidth()); |
| 197 | } else { |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 198 | Tmp = Tmp.shl(CharWidth - BitsInPreviousByte); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Chris Lattner | 2bc0025 | 2010-07-05 18:03:30 +0000 | [diff] [blame] | 201 | // 'or' in the bits that go into the previous byte. |
| 202 | llvm::Value *LastElt = Elements.back(); |
| 203 | if (llvm::ConstantInt *Val = dyn_cast<llvm::ConstantInt>(LastElt)) |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 204 | Tmp |= Val->getValue(); |
Chris Lattner | 2bc0025 | 2010-07-05 18:03:30 +0000 | [diff] [blame] | 205 | else { |
| 206 | assert(isa<llvm::UndefValue>(LastElt)); |
| 207 | // If there is an undef field that we're adding to, it can either be a |
| 208 | // scalar undef (in which case, we just replace it with our field) or it |
| 209 | // is an array. If it is an array, we have to pull one byte off the |
| 210 | // array so that the other undef bytes stay around. |
| 211 | if (!isa<llvm::IntegerType>(LastElt->getType())) { |
| 212 | // The undef padding will be a multibyte array, create a new smaller |
| 213 | // padding and then an hole for our i8 to get plopped into. |
| 214 | assert(isa<llvm::ArrayType>(LastElt->getType()) && |
| 215 | "Expected array padding of undefs"); |
| 216 | const llvm::ArrayType *AT = cast<llvm::ArrayType>(LastElt->getType()); |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 217 | assert(AT->getElementType()->isIntegerTy(CharWidth) && |
Chris Lattner | 2bc0025 | 2010-07-05 18:03:30 +0000 | [diff] [blame] | 218 | AT->getNumElements() != 0 && |
| 219 | "Expected non-empty array padding of undefs"); |
| 220 | |
| 221 | // Remove the padding array. |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 222 | NextFieldOffsetInChars -= CharUnits::fromQuantity(AT->getNumElements()); |
Chris Lattner | 2bc0025 | 2010-07-05 18:03:30 +0000 | [diff] [blame] | 223 | Elements.pop_back(); |
| 224 | |
| 225 | // Add the padding back in two chunks. |
Ken Dyck | c29ea8f | 2011-03-11 23:42:54 +0000 | [diff] [blame] | 226 | AppendPadding(CharUnits::fromQuantity(AT->getNumElements()-1)); |
| 227 | AppendPadding(CharUnits::One()); |
Chris Lattner | 2bc0025 | 2010-07-05 18:03:30 +0000 | [diff] [blame] | 228 | assert(isa<llvm::UndefValue>(Elements.back()) && |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 229 | Elements.back()->getType()->isIntegerTy(CharWidth) && |
Chris Lattner | 2bc0025 | 2010-07-05 18:03:30 +0000 | [diff] [blame] | 230 | "Padding addition didn't work right"); |
| 231 | } |
| 232 | } |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 233 | |
| 234 | Elements.back() = llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp); |
| 235 | |
| 236 | if (FitsCompletelyInPreviousByte) |
Chris Lattner | 1880666 | 2010-07-05 17:04:23 +0000 | [diff] [blame] | 237 | return; |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 240 | while (FieldValue.getBitWidth() > CharWidth) { |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 241 | llvm::APInt Tmp; |
| 242 | |
| 243 | if (CGM.getTargetData().isBigEndian()) { |
| 244 | // We want the high bits. |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 245 | Tmp = |
| 246 | FieldValue.lshr(FieldValue.getBitWidth() - CharWidth).trunc(CharWidth); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 247 | } else { |
| 248 | // We want the low bits. |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 249 | Tmp = FieldValue.trunc(CharWidth); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 250 | |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 251 | FieldValue = FieldValue.lshr(CharWidth); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp)); |
Ken Dyck | 199c189 | 2011-03-19 01:28:06 +0000 | [diff] [blame] | 255 | ++NextFieldOffsetInChars; |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 256 | |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 257 | FieldValue = FieldValue.trunc(FieldValue.getBitWidth() - CharWidth); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | assert(FieldValue.getBitWidth() > 0 && |
| 261 | "Should have at least one bit left!"); |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 262 | assert(FieldValue.getBitWidth() <= CharWidth && |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 263 | "Should not have more than a byte left!"); |
| 264 | |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 265 | if (FieldValue.getBitWidth() < CharWidth) { |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 266 | if (CGM.getTargetData().isBigEndian()) { |
| 267 | unsigned BitWidth = FieldValue.getBitWidth(); |
| 268 | |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 269 | FieldValue = FieldValue.zext(CharWidth) << (CharWidth - BitWidth); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 270 | } else |
Ken Dyck | 4f318c0 | 2011-03-19 00:57:28 +0000 | [diff] [blame] | 271 | FieldValue = FieldValue.zext(CharWidth); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | // Append the last element. |
| 275 | Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(), |
| 276 | FieldValue)); |
Ken Dyck | 199c189 | 2011-03-19 01:28:06 +0000 | [diff] [blame] | 277 | ++NextFieldOffsetInChars; |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Ken Dyck | c29ea8f | 2011-03-11 23:42:54 +0000 | [diff] [blame] | 280 | void ConstStructBuilder::AppendPadding(CharUnits PadSize) { |
| 281 | if (PadSize.isZero()) |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 282 | return; |
| 283 | |
| 284 | const llvm::Type *Ty = llvm::Type::getInt8Ty(CGM.getLLVMContext()); |
Ken Dyck | c29ea8f | 2011-03-11 23:42:54 +0000 | [diff] [blame] | 285 | if (PadSize > CharUnits::One()) |
| 286 | Ty = llvm::ArrayType::get(Ty, PadSize.getQuantity()); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 287 | |
Nuno Lopes | cdb30b4 | 2010-04-16 20:56:35 +0000 | [diff] [blame] | 288 | llvm::Constant *C = llvm::UndefValue::get(Ty); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 289 | Elements.push_back(C); |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 290 | assert(getAlignment(C) == CharUnits::One() && |
| 291 | "Padding must have 1 byte alignment!"); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 292 | |
Ken Dyck | 15c152e | 2011-03-18 01:12:13 +0000 | [diff] [blame] | 293 | NextFieldOffsetInChars += getSizeInChars(C); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Ken Dyck | 106ca04 | 2011-03-11 02:17:05 +0000 | [diff] [blame] | 296 | void ConstStructBuilder::AppendTailPadding(CharUnits RecordSize) { |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 297 | assert(NextFieldOffsetInChars <= RecordSize && |
Ken Dyck | 106ca04 | 2011-03-11 02:17:05 +0000 | [diff] [blame] | 298 | "Size mismatch!"); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 299 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 300 | AppendPadding(RecordSize - NextFieldOffsetInChars); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | void ConstStructBuilder::ConvertStructToPacked() { |
| 304 | std::vector<llvm::Constant *> PackedElements; |
Ken Dyck | 4a5c87e | 2011-03-18 00:55:06 +0000 | [diff] [blame] | 305 | CharUnits ElementOffsetInChars = CharUnits::Zero(); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 306 | |
| 307 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
| 308 | llvm::Constant *C = Elements[i]; |
| 309 | |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 310 | CharUnits ElementAlign = CharUnits::fromQuantity( |
| 311 | CGM.getTargetData().getABITypeAlignment(C->getType())); |
Ken Dyck | 4a5c87e | 2011-03-18 00:55:06 +0000 | [diff] [blame] | 312 | CharUnits AlignedElementOffsetInChars = |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 313 | ElementOffsetInChars.RoundUpToAlignment(ElementAlign); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 314 | |
Ken Dyck | 4a5c87e | 2011-03-18 00:55:06 +0000 | [diff] [blame] | 315 | if (AlignedElementOffsetInChars > ElementOffsetInChars) { |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 316 | // We need some padding. |
Ken Dyck | 4a5c87e | 2011-03-18 00:55:06 +0000 | [diff] [blame] | 317 | CharUnits NumChars = |
| 318 | AlignedElementOffsetInChars - ElementOffsetInChars; |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 319 | |
| 320 | const llvm::Type *Ty = llvm::Type::getInt8Ty(CGM.getLLVMContext()); |
Ken Dyck | 4a5c87e | 2011-03-18 00:55:06 +0000 | [diff] [blame] | 321 | if (NumChars > CharUnits::One()) |
| 322 | Ty = llvm::ArrayType::get(Ty, NumChars.getQuantity()); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 323 | |
Nuno Lopes | cdb30b4 | 2010-04-16 20:56:35 +0000 | [diff] [blame] | 324 | llvm::Constant *Padding = llvm::UndefValue::get(Ty); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 325 | PackedElements.push_back(Padding); |
Ken Dyck | 15c152e | 2011-03-18 01:12:13 +0000 | [diff] [blame] | 326 | ElementOffsetInChars += getSizeInChars(Padding); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | PackedElements.push_back(C); |
Ken Dyck | 15c152e | 2011-03-18 01:12:13 +0000 | [diff] [blame] | 330 | ElementOffsetInChars += getSizeInChars(C); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Ken Dyck | 4a5c87e | 2011-03-18 00:55:06 +0000 | [diff] [blame] | 333 | assert(ElementOffsetInChars == NextFieldOffsetInChars && |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 334 | "Packing the struct changed its size!"); |
| 335 | |
| 336 | Elements = PackedElements; |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 337 | LLVMStructAlignment = CharUnits::One(); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 338 | Packed = true; |
| 339 | } |
| 340 | |
| 341 | bool ConstStructBuilder::Build(InitListExpr *ILE) { |
| 342 | RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl(); |
| 343 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 344 | |
| 345 | unsigned FieldNo = 0; |
| 346 | unsigned ElementNo = 0; |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 347 | const FieldDecl *LastFD = 0; |
| 348 | bool IsMsStruct = RD->hasAttr<MsStructAttr>(); |
| 349 | |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 350 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
Chris Lattner | 8ce9e45 | 2010-04-13 18:16:19 +0000 | [diff] [blame] | 351 | FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) { |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 352 | if (IsMsStruct) { |
| 353 | // Zero-length bitfields following non-bitfield members are |
| 354 | // ignored: |
Fariborz Jahanian | 855a8e7 | 2011-05-03 20:21:04 +0000 | [diff] [blame] | 355 | if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((*Field), LastFD)) { |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 356 | --FieldNo; |
| 357 | continue; |
| 358 | } |
| 359 | LastFD = (*Field); |
| 360 | } |
Chris Lattner | 8ce9e45 | 2010-04-13 18:16:19 +0000 | [diff] [blame] | 361 | |
| 362 | // If this is a union, skip all the fields that aren't being initialized. |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 363 | if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field) |
| 364 | continue; |
| 365 | |
Chris Lattner | 8ce9e45 | 2010-04-13 18:16:19 +0000 | [diff] [blame] | 366 | // Don't emit anonymous bitfields, they just affect layout. |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 367 | if (Field->isBitField() && !Field->getIdentifier()) { |
| 368 | LastFD = (*Field); |
Chris Lattner | 8ce9e45 | 2010-04-13 18:16:19 +0000 | [diff] [blame] | 369 | continue; |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 370 | } |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 371 | |
Chris Lattner | 8ce9e45 | 2010-04-13 18:16:19 +0000 | [diff] [blame] | 372 | // Get the initializer. A struct can include fields without initializers, |
| 373 | // we just use explicit null values for them. |
| 374 | llvm::Constant *EltInit; |
| 375 | if (ElementNo < ILE->getNumInits()) |
| 376 | EltInit = CGM.EmitConstantExpr(ILE->getInit(ElementNo++), |
| 377 | Field->getType(), CGF); |
| 378 | else |
| 379 | EltInit = CGM.EmitNullConstant(Field->getType()); |
Eli Friedman | f0ca0ee | 2010-07-17 23:55:01 +0000 | [diff] [blame] | 380 | |
| 381 | if (!EltInit) |
| 382 | return false; |
Chris Lattner | 8ce9e45 | 2010-04-13 18:16:19 +0000 | [diff] [blame] | 383 | |
| 384 | if (!Field->isBitField()) { |
| 385 | // Handle non-bitfield members. |
| 386 | if (!AppendField(*Field, Layout.getFieldOffset(FieldNo), EltInit)) |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 387 | return false; |
| 388 | } else { |
Chris Lattner | 8ce9e45 | 2010-04-13 18:16:19 +0000 | [diff] [blame] | 389 | // Otherwise we have a bitfield. |
Chris Lattner | 1880666 | 2010-07-05 17:04:23 +0000 | [diff] [blame] | 390 | AppendBitField(*Field, Layout.getFieldOffset(FieldNo), |
| 391 | cast<llvm::ConstantInt>(EltInit)); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 392 | } |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 395 | CharUnits LayoutSizeInChars = Layout.getSize(); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 396 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 397 | if (NextFieldOffsetInChars > LayoutSizeInChars) { |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 398 | // If the struct is bigger than the size of the record type, |
| 399 | // we must have a flexible array member at the end. |
| 400 | assert(RD->hasFlexibleArrayMember() && |
| 401 | "Must have flexible array member if struct is bigger than type!"); |
| 402 | |
| 403 | // No tail padding is necessary. |
| 404 | return true; |
| 405 | } |
| 406 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 407 | CharUnits LLVMSizeInChars = |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 408 | NextFieldOffsetInChars.RoundUpToAlignment(LLVMStructAlignment); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 409 | |
| 410 | // Check if we need to convert the struct to a packed struct. |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 411 | if (NextFieldOffsetInChars <= LayoutSizeInChars && |
| 412 | LLVMSizeInChars > LayoutSizeInChars) { |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 413 | assert(!Packed && "Size mismatch!"); |
| 414 | |
| 415 | ConvertStructToPacked(); |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 416 | assert(NextFieldOffsetInChars <= LayoutSizeInChars && |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 417 | "Converting to packed did not help!"); |
| 418 | } |
| 419 | |
| 420 | // Append tail padding if necessary. |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 421 | AppendTailPadding(LayoutSizeInChars); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 422 | |
Ken Dyck | dc49639 | 2011-03-17 01:33:18 +0000 | [diff] [blame] | 423 | assert(LayoutSizeInChars == NextFieldOffsetInChars && |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 424 | "Tail padding mismatch!"); |
| 425 | |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | llvm::Constant *ConstStructBuilder:: |
| 430 | BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF, InitListExpr *ILE) { |
| 431 | ConstStructBuilder Builder(CGM, CGF); |
| 432 | |
| 433 | if (!Builder.Build(ILE)) |
| 434 | return 0; |
| 435 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 436 | // Pick the type to use. If the type is layout identical to the ConvertType |
| 437 | // type then use it, otherwise use whatever the builder produced for us. |
| 438 | const llvm::StructType *STy = |
| 439 | llvm::ConstantStruct::getTypeForElements(CGM.getLLVMContext(), |
| 440 | Builder.Elements,Builder.Packed); |
| 441 | const llvm::Type *ILETy = CGM.getTypes().ConvertType(ILE->getType()); |
| 442 | if (const llvm::StructType *ILESTy = dyn_cast<llvm::StructType>(ILETy)) { |
| 443 | if (ILESTy->isLayoutIdentical(STy)) |
| 444 | STy = ILESTy; |
| 445 | } |
| 446 | |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 447 | llvm::Constant *Result = |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 448 | llvm::ConstantStruct::get(STy, Builder.Elements); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 449 | |
Ken Dyck | 15c152e | 2011-03-18 01:12:13 +0000 | [diff] [blame] | 450 | assert(Builder.NextFieldOffsetInChars.RoundUpToAlignment( |
Ken Dyck | 7d2f9d5 | 2011-03-18 01:26:17 +0000 | [diff] [blame] | 451 | Builder.getAlignment(Result)) == |
Ken Dyck | 15c152e | 2011-03-18 01:12:13 +0000 | [diff] [blame] | 452 | Builder.getSizeInChars(Result) && "Size mismatch!"); |
Chris Lattner | eba3e5a | 2010-04-13 17:45:57 +0000 | [diff] [blame] | 453 | |
| 454 | return Result; |
| 455 | } |
| 456 | |
| 457 | |
| 458 | //===----------------------------------------------------------------------===// |
| 459 | // ConstExprEmitter |
| 460 | //===----------------------------------------------------------------------===// |
| 461 | |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 462 | class ConstExprEmitter : |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 463 | public StmtVisitor<ConstExprEmitter, llvm::Constant*> { |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 464 | CodeGenModule &CGM; |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 465 | CodeGenFunction *CGF; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 466 | llvm::LLVMContext &VMContext; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 467 | public: |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 468 | ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf) |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 469 | : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()) { |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 470 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 471 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 472 | //===--------------------------------------------------------------------===// |
| 473 | // Visitor Methods |
| 474 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 475 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 476 | llvm::Constant *VisitStmt(Stmt *S) { |
Anders Carlsson | 069880e | 2009-03-03 16:43:34 +0000 | [diff] [blame] | 477 | return 0; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 478 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | |
| 480 | llvm::Constant *VisitParenExpr(ParenExpr *PE) { |
| 481 | return Visit(PE->getSubExpr()); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 482 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 484 | llvm::Constant *VisitGenericSelectionExpr(GenericSelectionExpr *GE) { |
| 485 | return Visit(GE->getResultExpr()); |
| 486 | } |
| 487 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 488 | llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 489 | return Visit(E->getInitializer()); |
| 490 | } |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 491 | |
Anders Carlsson | f57b4e4 | 2009-10-03 15:02:02 +0000 | [diff] [blame] | 492 | llvm::Constant *VisitUnaryAddrOf(UnaryOperator *E) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 493 | if (E->getType()->isMemberPointerType()) |
| 494 | return CGM.getMemberPointerConstant(E); |
Anders Carlsson | 45147d0 | 2010-02-02 03:37:46 +0000 | [diff] [blame] | 495 | |
Anders Carlsson | f57b4e4 | 2009-10-03 15:02:02 +0000 | [diff] [blame] | 496 | return 0; |
| 497 | } |
| 498 | |
Chris Lattner | 3ae9f48 | 2009-10-13 07:14:16 +0000 | [diff] [blame] | 499 | llvm::Constant *VisitBinSub(BinaryOperator *E) { |
| 500 | // This must be a pointer/pointer subtraction. This only happens for |
| 501 | // address of label. |
| 502 | if (!isa<AddrLabelExpr>(E->getLHS()->IgnoreParenNoopCasts(CGM.getContext())) || |
| 503 | !isa<AddrLabelExpr>(E->getRHS()->IgnoreParenNoopCasts(CGM.getContext()))) |
| 504 | return 0; |
| 505 | |
| 506 | llvm::Constant *LHS = CGM.EmitConstantExpr(E->getLHS(), |
| 507 | E->getLHS()->getType(), CGF); |
| 508 | llvm::Constant *RHS = CGM.EmitConstantExpr(E->getRHS(), |
| 509 | E->getRHS()->getType(), CGF); |
| 510 | |
| 511 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 512 | LHS = llvm::ConstantExpr::getPtrToInt(LHS, ResultType); |
| 513 | RHS = llvm::ConstantExpr::getPtrToInt(RHS, ResultType); |
| 514 | |
| 515 | // No need to divide by element size, since addr of label is always void*, |
| 516 | // which has size 1 in GNUish. |
| 517 | return llvm::ConstantExpr::getSub(LHS, RHS); |
| 518 | } |
| 519 | |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 520 | llvm::Constant *VisitCastExpr(CastExpr* E) { |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 521 | Expr *subExpr = E->getSubExpr(); |
| 522 | llvm::Constant *C = CGM.EmitConstantExpr(subExpr, subExpr->getType(), CGF); |
| 523 | if (!C) return 0; |
| 524 | |
| 525 | const llvm::Type *destType = ConvertType(E->getType()); |
| 526 | |
Anders Carlsson | 0086ccb | 2009-08-22 23:54:44 +0000 | [diff] [blame] | 527 | switch (E->getCastKind()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 528 | case CK_ToUnion: { |
Anders Carlsson | 0086ccb | 2009-08-22 23:54:44 +0000 | [diff] [blame] | 529 | // GCC cast to union extension |
| 530 | assert(E->getType()->isUnionType() && |
| 531 | "Destination type is not union type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 532 | |
Anders Carlsson | aaed50d | 2009-07-31 21:38:39 +0000 | [diff] [blame] | 533 | // Build a struct with the union sub-element as the first member, |
| 534 | // and padded to the appropriate size |
| 535 | std::vector<llvm::Constant*> Elts; |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame^] | 536 | std::vector<llvm::Type*> Types; |
Anders Carlsson | aaed50d | 2009-07-31 21:38:39 +0000 | [diff] [blame] | 537 | Elts.push_back(C); |
| 538 | Types.push_back(C->getType()); |
| 539 | unsigned CurSize = CGM.getTargetData().getTypeAllocSize(C->getType()); |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 540 | unsigned TotalSize = CGM.getTargetData().getTypeAllocSize(destType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 541 | |
Anders Carlsson | aaed50d | 2009-07-31 21:38:39 +0000 | [diff] [blame] | 542 | assert(CurSize <= TotalSize && "Union size mismatch!"); |
| 543 | if (unsigned NumPadBytes = TotalSize - CurSize) { |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame^] | 544 | llvm::Type *Ty = llvm::Type::getInt8Ty(VMContext); |
Anders Carlsson | aaed50d | 2009-07-31 21:38:39 +0000 | [diff] [blame] | 545 | if (NumPadBytes > 1) |
| 546 | Ty = llvm::ArrayType::get(Ty, NumPadBytes); |
Anders Carlsson | 0086ccb | 2009-08-22 23:54:44 +0000 | [diff] [blame] | 547 | |
Nuno Lopes | cdb30b4 | 2010-04-16 20:56:35 +0000 | [diff] [blame] | 548 | Elts.push_back(llvm::UndefValue::get(Ty)); |
Anders Carlsson | aaed50d | 2009-07-31 21:38:39 +0000 | [diff] [blame] | 549 | Types.push_back(Ty); |
| 550 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 551 | |
Anders Carlsson | 0086ccb | 2009-08-22 23:54:44 +0000 | [diff] [blame] | 552 | llvm::StructType* STy = |
| 553 | llvm::StructType::get(C->getType()->getContext(), Types, false); |
Anders Carlsson | aaed50d | 2009-07-31 21:38:39 +0000 | [diff] [blame] | 554 | return llvm::ConstantStruct::get(STy, Elts); |
Nuno Lopes | 81e51e2 | 2009-01-17 00:48:48 +0000 | [diff] [blame] | 555 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 556 | case CK_NullToMemberPointer: { |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 557 | const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>(); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 558 | return CGM.getCXXABI().EmitNullMemberPointer(MPT); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 559 | } |
Anders Carlsson | 2c51f09 | 2009-10-03 15:13:22 +0000 | [diff] [blame] | 560 | |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 561 | case CK_DerivedToBaseMemberPointer: |
| 562 | case CK_BaseToDerivedMemberPointer: |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 563 | return CGM.getCXXABI().EmitMemberPointerConversion(C, E); |
Anders Carlsson | bb378cb | 2009-10-18 20:31:03 +0000 | [diff] [blame] | 564 | |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 565 | case CK_LValueToRValue: |
| 566 | case CK_NoOp: |
| 567 | return C; |
Anders Carlsson | bb378cb | 2009-10-18 20:31:03 +0000 | [diff] [blame] | 568 | |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 569 | case CK_AnyPointerToObjCPointerCast: |
| 570 | case CK_AnyPointerToBlockPointerCast: |
| 571 | case CK_LValueBitCast: |
| 572 | case CK_BitCast: |
| 573 | if (C->getType() == destType) return C; |
| 574 | return llvm::ConstantExpr::getBitCast(C, destType); |
Chris Lattner | 430656e | 2009-10-13 22:12:09 +0000 | [diff] [blame] | 575 | |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 576 | case CK_Dependent: llvm_unreachable("saw dependent cast!"); |
| 577 | |
| 578 | // These will never be supported. |
| 579 | case CK_ObjCObjectLValueCast: |
| 580 | case CK_GetObjCProperty: |
| 581 | case CK_ToVoid: |
| 582 | case CK_Dynamic: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 583 | case CK_ObjCProduceObject: |
| 584 | case CK_ObjCConsumeObject: |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 585 | case CK_ObjCReclaimReturnedObject: |
Anders Carlsson | 0086ccb | 2009-08-22 23:54:44 +0000 | [diff] [blame] | 586 | return 0; |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 587 | |
| 588 | // These might need to be supported for constexpr. |
| 589 | case CK_UserDefinedConversion: |
| 590 | case CK_ConstructorConversion: |
| 591 | return 0; |
| 592 | |
| 593 | // These should eventually be supported. |
| 594 | case CK_ArrayToPointerDecay: |
| 595 | case CK_FunctionToPointerDecay: |
| 596 | case CK_BaseToDerived: |
| 597 | case CK_DerivedToBase: |
| 598 | case CK_UncheckedDerivedToBase: |
| 599 | case CK_MemberPointerToBoolean: |
| 600 | case CK_VectorSplat: |
| 601 | case CK_FloatingRealToComplex: |
| 602 | case CK_FloatingComplexToReal: |
| 603 | case CK_FloatingComplexToBoolean: |
| 604 | case CK_FloatingComplexCast: |
| 605 | case CK_FloatingComplexToIntegralComplex: |
| 606 | case CK_IntegralRealToComplex: |
| 607 | case CK_IntegralComplexToReal: |
| 608 | case CK_IntegralComplexToBoolean: |
| 609 | case CK_IntegralComplexCast: |
| 610 | case CK_IntegralComplexToFloatingComplex: |
| 611 | return 0; |
| 612 | |
| 613 | case CK_PointerToIntegral: |
| 614 | if (!E->getType()->isBooleanType()) |
| 615 | return llvm::ConstantExpr::getPtrToInt(C, destType); |
| 616 | // fallthrough |
| 617 | |
| 618 | case CK_PointerToBoolean: |
| 619 | return llvm::ConstantExpr::getICmp(llvm::CmpInst::ICMP_EQ, C, |
| 620 | llvm::ConstantPointerNull::get(cast<llvm::PointerType>(C->getType()))); |
| 621 | |
| 622 | case CK_NullToPointer: |
| 623 | return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(destType)); |
| 624 | |
| 625 | case CK_IntegralCast: { |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 626 | bool isSigned = subExpr->getType()->isSignedIntegerOrEnumerationType(); |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 627 | return llvm::ConstantExpr::getIntegerCast(C, destType, isSigned); |
Eli Friedman | ddd2b6e | 2009-02-22 07:29:04 +0000 | [diff] [blame] | 628 | } |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 629 | |
| 630 | case CK_IntegralToPointer: { |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 631 | bool isSigned = subExpr->getType()->isSignedIntegerOrEnumerationType(); |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 632 | C = llvm::ConstantExpr::getIntegerCast(C, CGM.IntPtrTy, isSigned); |
| 633 | return llvm::ConstantExpr::getIntToPtr(C, destType); |
| 634 | } |
| 635 | |
| 636 | case CK_IntegralToBoolean: |
| 637 | return llvm::ConstantExpr::getICmp(llvm::CmpInst::ICMP_EQ, C, |
| 638 | llvm::Constant::getNullValue(C->getType())); |
| 639 | |
| 640 | case CK_IntegralToFloating: |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 641 | if (subExpr->getType()->isSignedIntegerOrEnumerationType()) |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 642 | return llvm::ConstantExpr::getSIToFP(C, destType); |
| 643 | else |
| 644 | return llvm::ConstantExpr::getUIToFP(C, destType); |
| 645 | |
| 646 | case CK_FloatingToIntegral: |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 647 | if (E->getType()->isSignedIntegerOrEnumerationType()) |
John McCall | 8bba1f4 | 2011-03-15 21:17:48 +0000 | [diff] [blame] | 648 | return llvm::ConstantExpr::getFPToSI(C, destType); |
| 649 | else |
| 650 | return llvm::ConstantExpr::getFPToUI(C, destType); |
| 651 | |
| 652 | case CK_FloatingToBoolean: |
| 653 | return llvm::ConstantExpr::getFCmp(llvm::CmpInst::FCMP_UNE, C, |
| 654 | llvm::Constant::getNullValue(C->getType())); |
| 655 | |
| 656 | case CK_FloatingCast: |
| 657 | return llvm::ConstantExpr::getFPCast(C, destType); |
Anders Carlsson | 0086ccb | 2009-08-22 23:54:44 +0000 | [diff] [blame] | 658 | } |
Matt Beaumont-Gay | 17d12fe | 2011-03-17 00:46:34 +0000 | [diff] [blame] | 659 | llvm_unreachable("Invalid CastKind"); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 660 | } |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 661 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 662 | llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { |
| 663 | return Visit(DAE->getExpr()); |
| 664 | } |
| 665 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 666 | llvm::Constant *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) { |
| 667 | return Visit(E->GetTemporaryExpr()); |
| 668 | } |
| 669 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 670 | llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) { |
Nuno Lopes | a75b71f | 2010-04-18 19:06:43 +0000 | [diff] [blame] | 671 | unsigned NumInitElements = ILE->getNumInits(); |
Benjamin Kramer | 0483a6f | 2011-03-02 21:27:44 +0000 | [diff] [blame] | 672 | if (NumInitElements == 1 && ILE->getType() == ILE->getInit(0)->getType() && |
Nuno Lopes | a75b71f | 2010-04-18 19:06:43 +0000 | [diff] [blame] | 673 | (isa<StringLiteral>(ILE->getInit(0)) || |
| 674 | isa<ObjCEncodeExpr>(ILE->getInit(0)))) |
| 675 | return Visit(ILE->getInit(0)); |
| 676 | |
Nuno Lopes | 6784704 | 2010-04-16 23:19:41 +0000 | [diff] [blame] | 677 | std::vector<llvm::Constant*> Elts; |
| 678 | const llvm::ArrayType *AType = |
| 679 | cast<llvm::ArrayType>(ConvertType(ILE->getType())); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 680 | const llvm::Type *ElemTy = AType->getElementType(); |
| 681 | unsigned NumElements = AType->getNumElements(); |
| 682 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 683 | // Initialising an array requires us to automatically |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 684 | // initialise any elements that have not been initialised explicitly |
| 685 | unsigned NumInitableElts = std::min(NumInitElements, NumElements); |
| 686 | |
| 687 | // Copy initializer elements. |
| 688 | unsigned i = 0; |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 689 | bool RewriteType = false; |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 690 | for (; i < NumInitableElts; ++i) { |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 691 | Expr *Init = ILE->getInit(i); |
| 692 | llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF); |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 693 | if (!C) |
| 694 | return 0; |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 695 | RewriteType |= (C->getType() != ElemTy); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 696 | Elts.push_back(C); |
| 697 | } |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 698 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 699 | // Initialize remaining array elements. |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 700 | // FIXME: This doesn't handle member pointers correctly! |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 701 | llvm::Constant *fillC; |
| 702 | if (Expr *filler = ILE->getArrayFiller()) |
| 703 | fillC = CGM.EmitConstantExpr(filler, filler->getType(), CGF); |
| 704 | else |
| 705 | fillC = llvm::Constant::getNullValue(ElemTy); |
| 706 | if (!fillC) |
| 707 | return 0; |
| 708 | RewriteType |= (fillC->getType() != ElemTy); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 709 | for (; i < NumElements; ++i) |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 710 | Elts.push_back(fillC); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 711 | |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 712 | if (RewriteType) { |
| 713 | // FIXME: Try to avoid packing the array |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame^] | 714 | std::vector<llvm::Type*> Types; |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 715 | for (unsigned i = 0; i < Elts.size(); ++i) |
| 716 | Types.push_back(Elts[i]->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 717 | const llvm::StructType *SType = llvm::StructType::get(AType->getContext(), |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 718 | Types, true); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 719 | return llvm::ConstantStruct::get(SType, Elts); |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 722 | return llvm::ConstantArray::get(AType, Elts); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 725 | llvm::Constant *EmitStructInitialization(InitListExpr *ILE) { |
Anders Carlsson | 8dca3b3 | 2009-07-27 05:54:15 +0000 | [diff] [blame] | 726 | return ConstStructBuilder::BuildStruct(CGM, CGF, ILE); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 727 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 728 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 729 | llvm::Constant *EmitUnionInitialization(InitListExpr *ILE) { |
Anders Carlsson | f6c4396 | 2009-07-31 21:34:04 +0000 | [diff] [blame] | 730 | return ConstStructBuilder::BuildStruct(CGM, CGF, ILE); |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Anders Carlsson | 7d801ea | 2009-01-30 06:13:25 +0000 | [diff] [blame] | 733 | llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E) { |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 734 | return CGM.EmitNullConstant(E->getType()); |
Anders Carlsson | 7d801ea | 2009-01-30 06:13:25 +0000 | [diff] [blame] | 735 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 736 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 737 | llvm::Constant *VisitInitListExpr(InitListExpr *ILE) { |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 738 | if (ILE->getType()->isScalarType()) { |
| 739 | // We have a scalar in braces. Just use the first element. |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 740 | if (ILE->getNumInits() > 0) { |
| 741 | Expr *Init = ILE->getInit(0); |
| 742 | return CGM.EmitConstantExpr(Init, Init->getType(), CGF); |
| 743 | } |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 744 | return CGM.EmitNullConstant(ILE->getType()); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 745 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 746 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 747 | if (ILE->getType()->isArrayType()) |
| 748 | return EmitArrayInitialization(ILE); |
Devang Patel | 47fb697 | 2008-01-29 23:23:18 +0000 | [diff] [blame] | 749 | |
Anders Carlsson | 01a79ac | 2009-11-21 23:56:04 +0000 | [diff] [blame] | 750 | if (ILE->getType()->isRecordType()) |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 751 | return EmitStructInitialization(ILE); |
| 752 | |
| 753 | if (ILE->getType()->isUnionType()) |
| 754 | return EmitUnionInitialization(ILE); |
| 755 | |
Eli Friedman | 8ac5549 | 2010-01-02 23:43:59 +0000 | [diff] [blame] | 756 | // If ILE was a constant vector, we would have handled it already. |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 757 | if (ILE->getType()->isVectorType()) |
Eli Friedman | 8ac5549 | 2010-01-02 23:43:59 +0000 | [diff] [blame] | 758 | return 0; |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 759 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 760 | assert(0 && "Unable to handle InitListExpr"); |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 761 | // Get rid of control reaches end of void function warning. |
| 762 | // Not reached. |
| 763 | return 0; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 764 | } |
Eli Friedman | 3b707e7 | 2008-02-21 17:57:49 +0000 | [diff] [blame] | 765 | |
John McCall | fb8b69a | 2010-02-02 08:02:49 +0000 | [diff] [blame] | 766 | llvm::Constant *VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 767 | if (!E->getConstructor()->isTrivial()) |
| 768 | return 0; |
| 769 | |
Anders Carlsson | 2ca4f63 | 2010-02-05 18:38:45 +0000 | [diff] [blame] | 770 | QualType Ty = E->getType(); |
| 771 | |
| 772 | // FIXME: We should not have to call getBaseElementType here. |
| 773 | const RecordType *RT = |
| 774 | CGM.getContext().getBaseElementType(Ty)->getAs<RecordType>(); |
| 775 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 776 | |
| 777 | // If the class doesn't have a trivial destructor, we can't emit it as a |
| 778 | // constant expr. |
| 779 | if (!RD->hasTrivialDestructor()) |
| 780 | return 0; |
| 781 | |
John McCall | fb8b69a | 2010-02-02 08:02:49 +0000 | [diff] [blame] | 782 | // Only copy and default constructors can be trivial. |
| 783 | |
John McCall | fb8b69a | 2010-02-02 08:02:49 +0000 | [diff] [blame] | 784 | |
| 785 | if (E->getNumArgs()) { |
| 786 | assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument"); |
| 787 | assert(E->getConstructor()->isCopyConstructor() && |
| 788 | "trivial ctor has argument but isn't a copy ctor"); |
| 789 | |
| 790 | Expr *Arg = E->getArg(0); |
| 791 | assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) && |
| 792 | "argument to copy ctor is of wrong type"); |
| 793 | |
Chandler Carruth | 878b55c | 2010-02-02 12:15:55 +0000 | [diff] [blame] | 794 | return Visit(Arg); |
John McCall | fb8b69a | 2010-02-02 08:02:49 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | return CGM.EmitNullConstant(Ty); |
| 798 | } |
| 799 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 800 | llvm::Constant *VisitStringLiteral(StringLiteral *E) { |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 801 | assert(!E->getType()->isPointerType() && "Strings are always arrays"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 802 | |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 803 | // This must be a string initializing an array in a static initializer. |
| 804 | // Don't emit it as the address of the string, emit the string data itself |
| 805 | // as an inline array. |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 806 | return llvm::ConstantArray::get(VMContext, |
| 807 | CGM.GetStringForStringLiteral(E), false); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 810 | llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E) { |
| 811 | // This must be an @encode initializing an array in a static initializer. |
| 812 | // Don't emit it as the address of the string, emit the string data itself |
| 813 | // as an inline array. |
| 814 | std::string Str; |
| 815 | CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str); |
| 816 | const ConstantArrayType *CAT = cast<ConstantArrayType>(E->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 817 | |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 818 | // Resize the string to the right size, adding zeros at the end, or |
| 819 | // truncating as needed. |
| 820 | Str.resize(CAT->getSize().getZExtValue(), '\0'); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 821 | return llvm::ConstantArray::get(VMContext, Str, false); |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 822 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 823 | |
Eli Friedman | bc5ed6e | 2008-05-29 11:22:45 +0000 | [diff] [blame] | 824 | llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) { |
| 825 | return Visit(E->getSubExpr()); |
| 826 | } |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 827 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 828 | // Utility methods |
| 829 | const llvm::Type *ConvertType(QualType T) { |
| 830 | return CGM.getTypes().ConvertType(T); |
| 831 | } |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 832 | |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 833 | public: |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 834 | llvm::Constant *EmitLValue(Expr *E) { |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 835 | switch (E->getStmtClass()) { |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 836 | default: break; |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 837 | case Expr::CompoundLiteralExprClass: { |
| 838 | // Note that due to the nature of compound literals, this is guaranteed |
| 839 | // to be the only use of the variable, so we just generate it here. |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 840 | CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); |
| 841 | llvm::Constant* C = Visit(CLE->getInitializer()); |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 842 | // FIXME: "Leaked" on failure. |
| 843 | if (C) |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 844 | C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), |
Eli Friedman | d15d8c6 | 2009-08-26 20:01:39 +0000 | [diff] [blame] | 845 | E->getType().isConstant(CGM.getContext()), |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 846 | llvm::GlobalValue::InternalLinkage, |
Eli Friedman | d15d8c6 | 2009-08-26 20:01:39 +0000 | [diff] [blame] | 847 | C, ".compoundliteral", 0, false, |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 848 | CGM.getContext().getTargetAddressSpace(E->getType())); |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 849 | return C; |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 850 | } |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 851 | case Expr::DeclRefExprClass: { |
Rafael Espindola | da18261 | 2010-03-04 21:26:03 +0000 | [diff] [blame] | 852 | ValueDecl *Decl = cast<DeclRefExpr>(E)->getDecl(); |
| 853 | if (Decl->hasAttr<WeakRefAttr>()) |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 854 | return CGM.GetWeakRefReference(Decl); |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 855 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl)) |
Anders Carlsson | 555b4bb | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 856 | return CGM.GetAddrOfFunction(FD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 857 | if (const VarDecl* VD = dyn_cast<VarDecl>(Decl)) { |
Daniel Dunbar | d01b669 | 2009-02-24 18:41:57 +0000 | [diff] [blame] | 858 | // We can never refer to a variable with local storage. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 859 | if (!VD->hasLocalStorage()) { |
Daniel Dunbar | d01b669 | 2009-02-24 18:41:57 +0000 | [diff] [blame] | 860 | if (VD->isFileVarDecl() || VD->hasExternalStorage()) |
| 861 | return CGM.GetAddrOfGlobalVar(VD); |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 862 | else if (VD->isLocalVarDecl()) { |
Daniel Dunbar | d01b669 | 2009-02-24 18:41:57 +0000 | [diff] [blame] | 863 | assert(CGF && "Can't access static local vars without CGF"); |
| 864 | return CGF->GetAddrOfStaticLocalVar(VD); |
| 865 | } |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 866 | } |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 867 | } |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 868 | break; |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 869 | } |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 870 | case Expr::StringLiteralClass: |
| 871 | return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E)); |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 872 | case Expr::ObjCEncodeExprClass: |
| 873 | return CGM.GetAddrOfConstantStringFromObjCEncode(cast<ObjCEncodeExpr>(E)); |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 874 | case Expr::ObjCStringLiteralClass: { |
| 875 | ObjCStringLiteral* SL = cast<ObjCStringLiteral>(E); |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 876 | llvm::Constant *C = |
| 877 | CGM.getObjCRuntime().GenerateConstantString(SL->getString()); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 878 | return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 879 | } |
Chris Lattner | 3534564 | 2008-12-12 05:18:02 +0000 | [diff] [blame] | 880 | case Expr::PredefinedExprClass: { |
Eli Friedman | f70b24e | 2009-11-14 08:37:13 +0000 | [diff] [blame] | 881 | unsigned Type = cast<PredefinedExpr>(E)->getIdentType(); |
| 882 | if (CGF) { |
Daniel Dunbar | 3ec0baf | 2010-08-21 03:01:12 +0000 | [diff] [blame] | 883 | LValue Res = CGF->EmitPredefinedLValue(cast<PredefinedExpr>(E)); |
Eli Friedman | f70b24e | 2009-11-14 08:37:13 +0000 | [diff] [blame] | 884 | return cast<llvm::Constant>(Res.getAddress()); |
| 885 | } else if (Type == PredefinedExpr::PrettyFunction) { |
| 886 | return CGM.GetAddrOfConstantCString("top level", ".tmp"); |
| 887 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 888 | |
Eli Friedman | f70b24e | 2009-11-14 08:37:13 +0000 | [diff] [blame] | 889 | return CGM.GetAddrOfConstantCString("", ".tmp"); |
Chris Lattner | 3534564 | 2008-12-12 05:18:02 +0000 | [diff] [blame] | 890 | } |
Eli Friedman | f011589 | 2009-01-25 01:21:06 +0000 | [diff] [blame] | 891 | case Expr::AddrLabelExprClass: { |
| 892 | assert(CGF && "Invalid address of label expression outside function."); |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 893 | llvm::Constant *Ptr = |
| 894 | CGF->GetAddrOfLabel(cast<AddrLabelExpr>(E)->getLabel()); |
| 895 | return llvm::ConstantExpr::getBitCast(Ptr, ConvertType(E->getType())); |
Eli Friedman | f011589 | 2009-01-25 01:21:06 +0000 | [diff] [blame] | 896 | } |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 897 | case Expr::CallExprClass: { |
| 898 | CallExpr* CE = cast<CallExpr>(E); |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 899 | unsigned builtin = CE->isBuiltinCall(CGM.getContext()); |
| 900 | if (builtin != |
| 901 | Builtin::BI__builtin___CFStringMakeConstantString && |
| 902 | builtin != |
| 903 | Builtin::BI__builtin___NSStringMakeConstantString) |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 904 | break; |
| 905 | const Expr *Arg = CE->getArg(0)->IgnoreParenCasts(); |
| 906 | const StringLiteral *Literal = cast<StringLiteral>(Arg); |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 907 | if (builtin == |
| 908 | Builtin::BI__builtin___NSStringMakeConstantString) { |
| 909 | return CGM.getObjCRuntime().GenerateConstantString(Literal); |
| 910 | } |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 911 | // FIXME: need to deal with UCN conversion issues. |
Steve Naroff | 8d4141f | 2009-04-01 13:55:36 +0000 | [diff] [blame] | 912 | return CGM.GetAddrOfConstantCFString(Literal); |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 913 | } |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 914 | case Expr::BlockExprClass: { |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 915 | std::string FunctionName; |
| 916 | if (CGF) |
| 917 | FunctionName = CGF->CurFn->getName(); |
| 918 | else |
| 919 | FunctionName = "global"; |
| 920 | |
| 921 | return CGM.GetAddrOfGlobalBlock(cast<BlockExpr>(E), FunctionName.c_str()); |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 922 | } |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 923 | } |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 924 | |
| 925 | return 0; |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 926 | } |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 927 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 928 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 929 | } // end anonymous namespace. |
| 930 | |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 931 | llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E, |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 932 | QualType DestType, |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 933 | CodeGenFunction *CGF) { |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 934 | Expr::EvalResult Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 935 | |
Anders Carlsson | 1e5dc6e | 2009-04-11 01:08:03 +0000 | [diff] [blame] | 936 | bool Success = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 937 | |
Eli Friedman | 50c39ea | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 938 | if (DestType->isReferenceType()) |
| 939 | Success = E->EvaluateAsLValue(Result, Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 940 | else |
Anders Carlsson | 1e5dc6e | 2009-04-11 01:08:03 +0000 | [diff] [blame] | 941 | Success = E->Evaluate(Result, Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 942 | |
Eli Friedman | 55f9bdd | 2009-11-14 08:51:33 +0000 | [diff] [blame] | 943 | if (Success && !Result.HasSideEffects) { |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 944 | switch (Result.Val.getKind()) { |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 945 | case APValue::Uninitialized: |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 946 | assert(0 && "Constant expressions should be initialized."); |
| 947 | return 0; |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 948 | case APValue::LValue: { |
Anders Carlsson | 1e5dc6e | 2009-04-11 01:08:03 +0000 | [diff] [blame] | 949 | const llvm::Type *DestTy = getTypes().ConvertTypeForMem(DestType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 950 | llvm::Constant *Offset = |
| 951 | llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 952 | Result.Val.getLValueOffset().getQuantity()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 953 | |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 954 | llvm::Constant *C; |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 955 | if (const Expr *LVBase = Result.Val.getLValueBase()) { |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 956 | C = ConstExprEmitter(*this, CGF).EmitLValue(const_cast<Expr*>(LVBase)); |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 957 | |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 958 | // Apply offset if necessary. |
| 959 | if (!Offset->isNullValue()) { |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 960 | const llvm::Type *Type = llvm::Type::getInt8PtrTy(VMContext); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 961 | llvm::Constant *Casted = llvm::ConstantExpr::getBitCast(C, Type); |
| 962 | Casted = llvm::ConstantExpr::getGetElementPtr(Casted, &Offset, 1); |
| 963 | C = llvm::ConstantExpr::getBitCast(Casted, C->getType()); |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | // Convert to the appropriate type; this could be an lvalue for |
| 967 | // an integer. |
Anders Carlsson | 12e330c | 2009-04-10 04:59:59 +0000 | [diff] [blame] | 968 | if (isa<llvm::PointerType>(DestTy)) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 969 | return llvm::ConstantExpr::getBitCast(C, DestTy); |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 970 | |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 971 | return llvm::ConstantExpr::getPtrToInt(C, DestTy); |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 972 | } else { |
| 973 | C = Offset; |
| 974 | |
| 975 | // Convert to the appropriate type; this could be an lvalue for |
| 976 | // an integer. |
Anders Carlsson | 12e330c | 2009-04-10 04:59:59 +0000 | [diff] [blame] | 977 | if (isa<llvm::PointerType>(DestTy)) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 978 | return llvm::ConstantExpr::getIntToPtr(C, DestTy); |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 979 | |
| 980 | // If the types don't match this should only be a truncate. |
Anders Carlsson | 12e330c | 2009-04-10 04:59:59 +0000 | [diff] [blame] | 981 | if (C->getType() != DestTy) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 982 | return llvm::ConstantExpr::getTrunc(C, DestTy); |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 983 | |
| 984 | return C; |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 985 | } |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 986 | } |
Eli Friedman | 7b30b1c | 2008-11-17 03:57:28 +0000 | [diff] [blame] | 987 | case APValue::Int: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 988 | llvm::Constant *C = llvm::ConstantInt::get(VMContext, |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 989 | Result.Val.getInt()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 990 | |
Chris Lattner | 7f215c1 | 2010-06-26 21:52:32 +0000 | [diff] [blame] | 991 | if (C->getType()->isIntegerTy(1)) { |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 992 | const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType()); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 993 | C = llvm::ConstantExpr::getZExt(C, BoolTy); |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 994 | } |
| 995 | return C; |
Eli Friedman | 7b30b1c | 2008-11-17 03:57:28 +0000 | [diff] [blame] | 996 | } |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 997 | case APValue::ComplexInt: { |
| 998 | llvm::Constant *Complex[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 999 | |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1000 | Complex[0] = llvm::ConstantInt::get(VMContext, |
| 1001 | Result.Val.getComplexIntReal()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1002 | Complex[1] = llvm::ConstantInt::get(VMContext, |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1003 | Result.Val.getComplexIntImag()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1004 | |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 1005 | // FIXME: the target may want to specify that this is packed. |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 1006 | llvm::StructType *STy = llvm::StructType::get(Complex[0]->getType(), |
| 1007 | Complex[1]->getType(), |
| 1008 | NULL); |
| 1009 | return llvm::ConstantStruct::get(STy, Complex); |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1010 | } |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 1011 | case APValue::Float: |
Owen Anderson | bc0a222 | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 1012 | return llvm::ConstantFP::get(VMContext, Result.Val.getFloat()); |
Anders Carlsson | aa43256 | 2008-11-17 01:58:55 +0000 | [diff] [blame] | 1013 | case APValue::ComplexFloat: { |
| 1014 | llvm::Constant *Complex[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1015 | |
| 1016 | Complex[0] = llvm::ConstantFP::get(VMContext, |
Owen Anderson | bc0a222 | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 1017 | Result.Val.getComplexFloatReal()); |
| 1018 | Complex[1] = llvm::ConstantFP::get(VMContext, |
| 1019 | Result.Val.getComplexFloatImag()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1020 | |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 1021 | // FIXME: the target may want to specify that this is packed. |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 1022 | llvm::StructType *STy = llvm::StructType::get(Complex[0]->getType(), |
| 1023 | Complex[1]->getType(), |
| 1024 | NULL); |
| 1025 | return llvm::ConstantStruct::get(STy, Complex); |
Anders Carlsson | aa43256 | 2008-11-17 01:58:55 +0000 | [diff] [blame] | 1026 | } |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 1027 | case APValue::Vector: { |
| 1028 | llvm::SmallVector<llvm::Constant *, 4> Inits; |
| 1029 | unsigned NumElts = Result.Val.getVectorLength(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1030 | |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 1031 | if (Context.getLangOptions().AltiVec && |
| 1032 | isa<CastExpr>(E) && |
| 1033 | cast<CastExpr>(E)->getCastKind() == CK_VectorSplat) { |
| 1034 | // AltiVec vector initialization with a single literal |
| 1035 | APValue &Elt = Result.Val.getVectorElt(0); |
| 1036 | |
| 1037 | llvm::Constant* InitValue = Elt.isInt() |
| 1038 | ? cast<llvm::Constant> |
| 1039 | (llvm::ConstantInt::get(VMContext, Elt.getInt())) |
| 1040 | : cast<llvm::Constant> |
| 1041 | (llvm::ConstantFP::get(VMContext, Elt.getFloat())); |
| 1042 | |
| 1043 | for (unsigned i = 0; i != NumElts; ++i) |
| 1044 | Inits.push_back(InitValue); |
| 1045 | |
| 1046 | } else { |
| 1047 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1048 | APValue &Elt = Result.Val.getVectorElt(i); |
| 1049 | if (Elt.isInt()) |
| 1050 | Inits.push_back(llvm::ConstantInt::get(VMContext, Elt.getInt())); |
| 1051 | else |
| 1052 | Inits.push_back(llvm::ConstantFP::get(VMContext, Elt.getFloat())); |
| 1053 | } |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 1054 | } |
Chris Lattner | fb018d1 | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 1055 | return llvm::ConstantVector::get(Inits); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 1056 | } |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 1057 | } |
| 1058 | } |
Eli Friedman | 7dfa639 | 2008-06-01 15:31:44 +0000 | [diff] [blame] | 1059 | |
| 1060 | llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E)); |
Chris Lattner | 7f215c1 | 2010-06-26 21:52:32 +0000 | [diff] [blame] | 1061 | if (C && C->getType()->isIntegerTy(1)) { |
Eli Friedman | 7dfa639 | 2008-06-01 15:31:44 +0000 | [diff] [blame] | 1062 | const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType()); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1063 | C = llvm::ConstantExpr::getZExt(C, BoolTy); |
Eli Friedman | 7dfa639 | 2008-06-01 15:31:44 +0000 | [diff] [blame] | 1064 | } |
| 1065 | return C; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 1066 | } |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 1067 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1068 | static uint64_t getFieldOffset(ASTContext &C, const FieldDecl *field) { |
| 1069 | const ASTRecordLayout &layout = C.getASTRecordLayout(field->getParent()); |
| 1070 | return layout.getFieldOffset(field->getFieldIndex()); |
| 1071 | } |
| 1072 | |
| 1073 | llvm::Constant * |
| 1074 | CodeGenModule::getMemberPointerConstant(const UnaryOperator *uo) { |
| 1075 | // Member pointer constants always have a very particular form. |
| 1076 | const MemberPointerType *type = cast<MemberPointerType>(uo->getType()); |
| 1077 | const ValueDecl *decl = cast<DeclRefExpr>(uo->getSubExpr())->getDecl(); |
| 1078 | |
| 1079 | // A member function pointer. |
| 1080 | if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(decl)) |
| 1081 | return getCXXABI().EmitMemberPointer(method); |
| 1082 | |
| 1083 | // Otherwise, a member data pointer. |
| 1084 | uint64_t fieldOffset; |
| 1085 | if (const FieldDecl *field = dyn_cast<FieldDecl>(decl)) |
| 1086 | fieldOffset = getFieldOffset(getContext(), field); |
| 1087 | else { |
| 1088 | const IndirectFieldDecl *ifield = cast<IndirectFieldDecl>(decl); |
| 1089 | |
| 1090 | fieldOffset = 0; |
| 1091 | for (IndirectFieldDecl::chain_iterator ci = ifield->chain_begin(), |
| 1092 | ce = ifield->chain_end(); ci != ce; ++ci) |
| 1093 | fieldOffset += getFieldOffset(getContext(), cast<FieldDecl>(*ci)); |
| 1094 | } |
| 1095 | |
| 1096 | CharUnits chars = getContext().toCharUnitsFromBits((int64_t) fieldOffset); |
| 1097 | return getCXXABI().EmitMemberDataPointer(type, chars); |
| 1098 | } |
| 1099 | |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1100 | static void |
| 1101 | FillInNullDataMemberPointers(CodeGenModule &CGM, QualType T, |
| 1102 | std::vector<llvm::Constant *> &Elements, |
| 1103 | uint64_t StartOffset) { |
Ken Dyck | c4d68db | 2011-03-22 00:24:31 +0000 | [diff] [blame] | 1104 | assert(StartOffset % CGM.getContext().getCharWidth() == 0 && |
| 1105 | "StartOffset not byte aligned!"); |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1106 | |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 1107 | if (CGM.getTypes().isZeroInitializable(T)) |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1108 | return; |
| 1109 | |
| 1110 | if (const ConstantArrayType *CAT = |
| 1111 | CGM.getContext().getAsConstantArrayType(T)) { |
| 1112 | QualType ElementTy = CAT->getElementType(); |
| 1113 | uint64_t ElementSize = CGM.getContext().getTypeSize(ElementTy); |
| 1114 | |
| 1115 | for (uint64_t I = 0, E = CAT->getSize().getZExtValue(); I != E; ++I) { |
| 1116 | FillInNullDataMemberPointers(CGM, ElementTy, Elements, |
| 1117 | StartOffset + I * ElementSize); |
| 1118 | } |
| 1119 | } else if (const RecordType *RT = T->getAs<RecordType>()) { |
| 1120 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 1121 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 1122 | |
| 1123 | // Go through all bases and fill in any null pointer to data members. |
| 1124 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1125 | E = RD->bases_end(); I != E; ++I) { |
Anders Carlsson | 0a87b37 | 2010-05-27 18:51:01 +0000 | [diff] [blame] | 1126 | if (I->isVirtual()) { |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1127 | // Ignore virtual bases. |
Anders Carlsson | 0a87b37 | 2010-05-27 18:51:01 +0000 | [diff] [blame] | 1128 | continue; |
| 1129 | } |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1130 | |
| 1131 | const CXXRecordDecl *BaseDecl = |
| 1132 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1133 | |
| 1134 | // Ignore empty bases. |
| 1135 | if (BaseDecl->isEmpty()) |
| 1136 | continue; |
| 1137 | |
| 1138 | // Ignore bases that don't have any pointer to data members. |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 1139 | if (CGM.getTypes().isZeroInitializable(BaseDecl)) |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1140 | continue; |
| 1141 | |
Anders Carlsson | a14f597 | 2010-10-31 23:22:37 +0000 | [diff] [blame] | 1142 | uint64_t BaseOffset = Layout.getBaseClassOffsetInBits(BaseDecl); |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1143 | FillInNullDataMemberPointers(CGM, I->getType(), |
| 1144 | Elements, StartOffset + BaseOffset); |
| 1145 | } |
| 1146 | |
| 1147 | // Visit all fields. |
| 1148 | unsigned FieldNo = 0; |
| 1149 | for (RecordDecl::field_iterator I = RD->field_begin(), |
| 1150 | E = RD->field_end(); I != E; ++I, ++FieldNo) { |
| 1151 | QualType FieldType = I->getType(); |
| 1152 | |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 1153 | if (CGM.getTypes().isZeroInitializable(FieldType)) |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1154 | continue; |
| 1155 | |
| 1156 | uint64_t FieldOffset = StartOffset + Layout.getFieldOffset(FieldNo); |
| 1157 | FillInNullDataMemberPointers(CGM, FieldType, Elements, FieldOffset); |
| 1158 | } |
| 1159 | } else { |
| 1160 | assert(T->isMemberPointerType() && "Should only see member pointers here!"); |
| 1161 | assert(!T->getAs<MemberPointerType>()->getPointeeType()->isFunctionType() && |
| 1162 | "Should only see pointers to data members here!"); |
| 1163 | |
Ken Dyck | c4d68db | 2011-03-22 00:24:31 +0000 | [diff] [blame] | 1164 | CharUnits StartIndex = CGM.getContext().toCharUnitsFromBits(StartOffset); |
| 1165 | CharUnits EndIndex = StartIndex + CGM.getContext().getTypeSizeInChars(T); |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1166 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1167 | // FIXME: hardcodes Itanium member pointer representation! |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1168 | llvm::Constant *NegativeOne = |
| 1169 | llvm::ConstantInt::get(llvm::Type::getInt8Ty(CGM.getLLVMContext()), |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1170 | -1ULL, /*isSigned*/true); |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1171 | |
| 1172 | // Fill in the null data member pointer. |
Ken Dyck | c4d68db | 2011-03-22 00:24:31 +0000 | [diff] [blame] | 1173 | for (CharUnits I = StartIndex; I != EndIndex; ++I) |
| 1174 | Elements[I.getQuantity()] = NegativeOne; |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1175 | } |
| 1176 | } |
| 1177 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1178 | static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM, |
| 1179 | const llvm::Type *baseType, |
| 1180 | const CXXRecordDecl *base); |
| 1181 | |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1182 | static llvm::Constant *EmitNullConstant(CodeGenModule &CGM, |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1183 | const CXXRecordDecl *record, |
| 1184 | bool asCompleteObject) { |
| 1185 | const CGRecordLayout &layout = CGM.getTypes().getCGRecordLayout(record); |
| 1186 | const llvm::StructType *structure = |
| 1187 | (asCompleteObject ? layout.getLLVMType() |
| 1188 | : layout.getBaseSubobjectLLVMType()); |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1189 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1190 | unsigned numElements = structure->getNumElements(); |
| 1191 | std::vector<llvm::Constant *> elements(numElements); |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1192 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1193 | // Fill in all the bases. |
| 1194 | for (CXXRecordDecl::base_class_const_iterator |
| 1195 | I = record->bases_begin(), E = record->bases_end(); I != E; ++I) { |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1196 | if (I->isVirtual()) { |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1197 | // Ignore virtual bases; if we're laying out for a complete |
| 1198 | // object, we'll lay these out later. |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1199 | continue; |
| 1200 | } |
| 1201 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1202 | const CXXRecordDecl *base = |
| 1203 | cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl()); |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1204 | |
| 1205 | // Ignore empty bases. |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1206 | if (base->isEmpty()) |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1207 | continue; |
| 1208 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1209 | unsigned fieldIndex = layout.getNonVirtualBaseLLVMFieldNo(base); |
| 1210 | const llvm::Type *baseType = structure->getElementType(fieldIndex); |
| 1211 | elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base); |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1214 | // Fill in all the fields. |
| 1215 | for (RecordDecl::field_iterator I = record->field_begin(), |
| 1216 | E = record->field_end(); I != E; ++I) { |
| 1217 | const FieldDecl *field = *I; |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1218 | |
| 1219 | // Ignore bit fields. |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1220 | if (field->isBitField()) |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1221 | continue; |
| 1222 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1223 | unsigned fieldIndex = layout.getLLVMFieldNo(field); |
| 1224 | elements[fieldIndex] = CGM.EmitNullConstant(field->getType()); |
| 1225 | } |
| 1226 | |
| 1227 | // Fill in the virtual bases, if we're working with the complete object. |
| 1228 | if (asCompleteObject) { |
| 1229 | for (CXXRecordDecl::base_class_const_iterator |
| 1230 | I = record->vbases_begin(), E = record->vbases_end(); I != E; ++I) { |
| 1231 | const CXXRecordDecl *base = |
| 1232 | cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl()); |
| 1233 | |
| 1234 | // Ignore empty bases. |
| 1235 | if (base->isEmpty()) |
| 1236 | continue; |
| 1237 | |
| 1238 | unsigned fieldIndex = layout.getVirtualBaseIndex(base); |
| 1239 | |
| 1240 | // We might have already laid this field out. |
| 1241 | if (elements[fieldIndex]) continue; |
| 1242 | |
| 1243 | const llvm::Type *baseType = structure->getElementType(fieldIndex); |
| 1244 | elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base); |
| 1245 | } |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | // Now go through all other fields and zero them out. |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1249 | for (unsigned i = 0; i != numElements; ++i) { |
| 1250 | if (!elements[i]) |
| 1251 | elements[i] = llvm::Constant::getNullValue(structure->getElementType(i)); |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1254 | return llvm::ConstantStruct::get(structure, elements); |
| 1255 | } |
| 1256 | |
| 1257 | /// Emit the null constant for a base subobject. |
| 1258 | static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM, |
| 1259 | const llvm::Type *baseType, |
| 1260 | const CXXRecordDecl *base) { |
| 1261 | const CGRecordLayout &baseLayout = CGM.getTypes().getCGRecordLayout(base); |
| 1262 | |
| 1263 | // Just zero out bases that don't have any pointer to data members. |
| 1264 | if (baseLayout.isZeroInitializableAsBase()) |
| 1265 | return llvm::Constant::getNullValue(baseType); |
| 1266 | |
| 1267 | // If the base type is a struct, we can just use its null constant. |
| 1268 | if (isa<llvm::StructType>(baseType)) { |
| 1269 | return EmitNullConstant(CGM, base, /*complete*/ false); |
| 1270 | } |
| 1271 | |
| 1272 | // Otherwise, some bases are represented as arrays of i8 if the size |
| 1273 | // of the base is smaller than its corresponding LLVM type. Figure |
| 1274 | // out how many elements this base array has. |
| 1275 | const llvm::ArrayType *baseArrayType = cast<llvm::ArrayType>(baseType); |
| 1276 | unsigned numBaseElements = baseArrayType->getNumElements(); |
| 1277 | |
| 1278 | // Fill in null data member pointers. |
| 1279 | std::vector<llvm::Constant *> baseElements(numBaseElements); |
| 1280 | FillInNullDataMemberPointers(CGM, CGM.getContext().getTypeDeclType(base), |
| 1281 | baseElements, 0); |
| 1282 | |
| 1283 | // Now go through all other elements and zero them out. |
| 1284 | if (numBaseElements) { |
| 1285 | const llvm::Type *i8 = llvm::Type::getInt8Ty(CGM.getLLVMContext()); |
| 1286 | llvm::Constant *i8_zero = llvm::Constant::getNullValue(i8); |
| 1287 | for (unsigned i = 0; i != numBaseElements; ++i) { |
| 1288 | if (!baseElements[i]) |
| 1289 | baseElements[i] = i8_zero; |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | return llvm::ConstantArray::get(baseArrayType, baseElements); |
Anders Carlsson | ada4b32 | 2010-11-22 18:42:14 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 1296 | llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) { |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 1297 | if (getTypes().isZeroInitializable(T)) |
Anders Carlsson | ad3e711 | 2009-08-24 17:16:23 +0000 | [diff] [blame] | 1298 | return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T)); |
Anders Carlsson | 2c12d03 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 1299 | |
Anders Carlsson | 237957c | 2009-08-09 18:26:27 +0000 | [diff] [blame] | 1300 | if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1301 | |
Anders Carlsson | 237957c | 2009-08-09 18:26:27 +0000 | [diff] [blame] | 1302 | QualType ElementTy = CAT->getElementType(); |
| 1303 | |
Anders Carlsson | 2c12d03 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 1304 | llvm::Constant *Element = EmitNullConstant(ElementTy); |
| 1305 | unsigned NumElements = CAT->getSize().getZExtValue(); |
| 1306 | std::vector<llvm::Constant *> Array(NumElements); |
| 1307 | for (unsigned i = 0; i != NumElements; ++i) |
| 1308 | Array[i] = Element; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1309 | |
Anders Carlsson | 2c12d03 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 1310 | const llvm::ArrayType *ATy = |
| 1311 | cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T)); |
| 1312 | return llvm::ConstantArray::get(ATy, Array); |
Anders Carlsson | 237957c | 2009-08-09 18:26:27 +0000 | [diff] [blame] | 1313 | } |
Anders Carlsson | fc3eaa4 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 1314 | |
| 1315 | if (const RecordType *RT = T->getAs<RecordType>()) { |
Anders Carlsson | 2c12d03 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 1316 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 1317 | return ::EmitNullConstant(*this, RD, /*complete object*/ true); |
Anders Carlsson | fc3eaa4 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 1318 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1319 | |
Anders Carlsson | a83fb4b | 2010-05-18 16:51:41 +0000 | [diff] [blame] | 1320 | assert(T->isMemberPointerType() && "Should only see member pointers here!"); |
Anders Carlsson | 2c12d03 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 1321 | assert(!T->getAs<MemberPointerType>()->getPointeeType()->isFunctionType() && |
| 1322 | "Should only see pointers to data members here!"); |
| 1323 | |
| 1324 | // Itanium C++ ABI 2.3: |
| 1325 | // A NULL pointer is represented as -1. |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1326 | return getCXXABI().EmitNullMemberPointer(T->castAs<MemberPointerType>()); |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 1327 | } |