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" |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 16 | #include "CGObjCRuntime.h" |
Chris Lattner | f6b24ea | 2008-10-06 05:59:01 +0000 | [diff] [blame] | 17 | #include "clang/AST/APValue.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 19 | #include "clang/AST/RecordLayout.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 20 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Builtins.h" |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
| 23 | #include "llvm/Function.h" |
| 24 | #include "llvm/GlobalVariable.h" |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetData.h" |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | using namespace CodeGen; |
| 28 | |
| 29 | namespace { |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 30 | class ConstStructBuilder { |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 31 | CodeGenModule &CGM; |
| 32 | CodeGenFunction *CGF; |
| 33 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 34 | bool Packed; |
Anders Carlsson | 4257bc6 | 2009-07-25 00:24:56 +0000 | [diff] [blame] | 35 | |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 36 | unsigned NextFieldOffsetInBytes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 37 | |
Anders Carlsson | 31f2f9c | 2009-10-02 02:15:20 +0000 | [diff] [blame] | 38 | unsigned LLVMStructAlignment; |
| 39 | |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 40 | std::vector<llvm::Constant *> Elements; |
| 41 | |
| 42 | ConstStructBuilder(CodeGenModule &CGM, CodeGenFunction *CGF) |
Anders Carlsson | 31f2f9c | 2009-10-02 02:15:20 +0000 | [diff] [blame] | 43 | : CGM(CGM), CGF(CGF), Packed(false), NextFieldOffsetInBytes(0), |
| 44 | LLVMStructAlignment(1) { } |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 45 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | bool AppendField(const FieldDecl *Field, uint64_t FieldOffset, |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 47 | const Expr *InitExpr) { |
| 48 | uint64_t FieldOffsetInBytes = FieldOffset / 8; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 49 | |
| 50 | assert(NextFieldOffsetInBytes <= FieldOffsetInBytes |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 51 | && "Field offset mismatch!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 53 | // Emit the field. |
| 54 | llvm::Constant *C = CGM.EmitConstantExpr(InitExpr, Field->getType(), CGF); |
| 55 | if (!C) |
| 56 | return false; |
| 57 | |
| 58 | unsigned FieldAlignment = getAlignment(C); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 60 | // Round up the field offset to the alignment of the field type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | uint64_t AlignedNextFieldOffsetInBytes = |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 62 | llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 64 | if (AlignedNextFieldOffsetInBytes > FieldOffsetInBytes) { |
Anders Carlsson | bea9c52 | 2009-07-25 01:40:29 +0000 | [diff] [blame] | 65 | assert(!Packed && "Alignment is wrong even with a packed struct!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | |
Anders Carlsson | bea9c52 | 2009-07-25 01:40:29 +0000 | [diff] [blame] | 67 | // Convert the struct to a packed struct. |
Anders Carlsson | 31f2f9c | 2009-10-02 02:15:20 +0000 | [diff] [blame] | 68 | ConvertStructToPacked(); |
| 69 | |
Anders Carlsson | bea9c52 | 2009-07-25 01:40:29 +0000 | [diff] [blame] | 70 | AlignedNextFieldOffsetInBytes = NextFieldOffsetInBytes; |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) { |
| 74 | // We need to append padding. |
Anders Carlsson | bea9c52 | 2009-07-25 01:40:29 +0000 | [diff] [blame] | 75 | AppendPadding(FieldOffsetInBytes - NextFieldOffsetInBytes); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 76 | |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 77 | assert(NextFieldOffsetInBytes == FieldOffsetInBytes && |
| 78 | "Did not add enough padding!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 80 | AlignedNextFieldOffsetInBytes = NextFieldOffsetInBytes; |
| 81 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 83 | // Add the field. |
| 84 | Elements.push_back(C); |
| 85 | NextFieldOffsetInBytes = AlignedNextFieldOffsetInBytes + getSizeInBytes(C); |
Anders Carlsson | 2df96e7 | 2009-10-02 04:52:12 +0000 | [diff] [blame] | 86 | |
| 87 | if (Packed) |
| 88 | assert(LLVMStructAlignment == 1 && "Packed struct not byte-aligned!"); |
| 89 | else |
| 90 | LLVMStructAlignment = std::max(LLVMStructAlignment, FieldAlignment); |
| 91 | |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 92 | return true; |
| 93 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | |
| 95 | bool AppendBitField(const FieldDecl *Field, uint64_t FieldOffset, |
Anders Carlsson | d24393b | 2009-07-27 01:23:51 +0000 | [diff] [blame] | 96 | const Expr *InitExpr) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | llvm::ConstantInt *CI = |
| 98 | cast_or_null<llvm::ConstantInt>(CGM.EmitConstantExpr(InitExpr, |
| 99 | Field->getType(), |
Anders Carlsson | d24393b | 2009-07-27 01:23:51 +0000 | [diff] [blame] | 100 | CGF)); |
| 101 | // FIXME: Can this ever happen? |
| 102 | if (!CI) |
| 103 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
Anders Carlsson | d24393b | 2009-07-27 01:23:51 +0000 | [diff] [blame] | 105 | if (FieldOffset > NextFieldOffsetInBytes * 8) { |
Anders Carlsson | 7a86d4b | 2009-07-27 02:56:37 +0000 | [diff] [blame] | 106 | // We need to add padding. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | uint64_t NumBytes = |
| 108 | llvm::RoundUpToAlignment(FieldOffset - |
Anders Carlsson | 7a86d4b | 2009-07-27 02:56:37 +0000 | [diff] [blame] | 109 | NextFieldOffsetInBytes * 8, 8) / 8; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 110 | |
Anders Carlsson | 7a86d4b | 2009-07-27 02:56:37 +0000 | [diff] [blame] | 111 | AppendPadding(NumBytes); |
Anders Carlsson | d24393b | 2009-07-27 01:23:51 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | uint64_t FieldSize = |
Anders Carlsson | d24393b | 2009-07-27 01:23:51 +0000 | [diff] [blame] | 115 | Field->getBitWidth()->EvaluateAsInt(CGM.getContext()).getZExtValue(); |
| 116 | |
| 117 | llvm::APInt FieldValue = CI->getValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Anders Carlsson | d24393b | 2009-07-27 01:23:51 +0000 | [diff] [blame] | 119 | // Promote the size of FieldValue if necessary |
| 120 | // FIXME: This should never occur, but currently it can because initializer |
| 121 | // constants are cast to bool, and because clang is not enforcing bitfield |
| 122 | // width limits. |
| 123 | if (FieldSize > FieldValue.getBitWidth()) |
| 124 | FieldValue.zext(FieldSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | |
Anders Carlsson | d24393b | 2009-07-27 01:23:51 +0000 | [diff] [blame] | 126 | // Truncate the size of FieldValue to the bit field size. |
| 127 | if (FieldSize < FieldValue.getBitWidth()) |
| 128 | FieldValue.trunc(FieldSize); |
| 129 | |
| 130 | if (FieldOffset < NextFieldOffsetInBytes * 8) { |
Anders Carlsson | 7a86d4b | 2009-07-27 02:56:37 +0000 | [diff] [blame] | 131 | // Either part of the field or the entire field can go into the previous |
| 132 | // byte. |
| 133 | assert(!Elements.empty() && "Elements can't be empty!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | |
| 135 | unsigned BitsInPreviousByte = |
Anders Carlsson | 7a86d4b | 2009-07-27 02:56:37 +0000 | [diff] [blame] | 136 | NextFieldOffsetInBytes * 8 - FieldOffset; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
| 138 | bool FitsCompletelyInPreviousByte = |
Anders Carlsson | 7a86d4b | 2009-07-27 02:56:37 +0000 | [diff] [blame] | 139 | BitsInPreviousByte >= FieldValue.getBitWidth(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | |
Anders Carlsson | 7a86d4b | 2009-07-27 02:56:37 +0000 | [diff] [blame] | 141 | llvm::APInt Tmp = FieldValue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | |
Anders Carlsson | 7a86d4b | 2009-07-27 02:56:37 +0000 | [diff] [blame] | 143 | if (!FitsCompletelyInPreviousByte) { |
| 144 | unsigned NewFieldWidth = FieldSize - BitsInPreviousByte; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 145 | |
Anders Carlsson | 7a86d4b | 2009-07-27 02:56:37 +0000 | [diff] [blame] | 146 | if (CGM.getTargetData().isBigEndian()) { |
| 147 | Tmp = Tmp.lshr(NewFieldWidth); |
| 148 | Tmp.trunc(BitsInPreviousByte); |
| 149 | |
| 150 | // We want the remaining high bits. |
| 151 | FieldValue.trunc(NewFieldWidth); |
| 152 | } else { |
| 153 | Tmp.trunc(BitsInPreviousByte); |
| 154 | |
| 155 | // We want the remaining low bits. |
| 156 | FieldValue = FieldValue.lshr(BitsInPreviousByte); |
| 157 | FieldValue.trunc(NewFieldWidth); |
| 158 | } |
| 159 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | |
Anders Carlsson | 7a86d4b | 2009-07-27 02:56:37 +0000 | [diff] [blame] | 161 | Tmp.zext(8); |
| 162 | if (CGM.getTargetData().isBigEndian()) { |
| 163 | if (FitsCompletelyInPreviousByte) |
| 164 | Tmp = Tmp.shl(BitsInPreviousByte - FieldValue.getBitWidth()); |
| 165 | } else { |
| 166 | Tmp = Tmp.shl(8 - BitsInPreviousByte); |
| 167 | } |
| 168 | |
| 169 | // Or in the bits that go into the previous byte. |
Daniel Dunbar | 88b172c | 2009-12-08 21:12:32 +0000 | [diff] [blame] | 170 | if (llvm::ConstantInt *Val = dyn_cast<llvm::ConstantInt>(Elements.back())) |
| 171 | Tmp |= Val->getValue(); |
| 172 | else |
| 173 | assert(isa<llvm::UndefValue>(Elements.back())); |
| 174 | |
Anders Carlsson | 7a86d4b | 2009-07-27 02:56:37 +0000 | [diff] [blame] | 175 | Elements.back() = llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | |
Anders Carlsson | 7a86d4b | 2009-07-27 02:56:37 +0000 | [diff] [blame] | 177 | if (FitsCompletelyInPreviousByte) |
| 178 | return true; |
Anders Carlsson | d24393b | 2009-07-27 01:23:51 +0000 | [diff] [blame] | 179 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 180 | |
Anders Carlsson | d24393b | 2009-07-27 01:23:51 +0000 | [diff] [blame] | 181 | while (FieldValue.getBitWidth() > 8) { |
| 182 | llvm::APInt Tmp; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | |
Anders Carlsson | d24393b | 2009-07-27 01:23:51 +0000 | [diff] [blame] | 184 | if (CGM.getTargetData().isBigEndian()) { |
Anders Carlsson | d24393b | 2009-07-27 01:23:51 +0000 | [diff] [blame] | 185 | // We want the high bits. |
Anders Carlsson | 7a86d4b | 2009-07-27 02:56:37 +0000 | [diff] [blame] | 186 | Tmp = FieldValue; |
| 187 | Tmp = Tmp.lshr(Tmp.getBitWidth() - 8); |
| 188 | Tmp.trunc(8); |
| 189 | } else { |
| 190 | // We want the low bits. |
| 191 | Tmp = FieldValue; |
| 192 | Tmp.trunc(8); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | |
Anders Carlsson | 7a86d4b | 2009-07-27 02:56:37 +0000 | [diff] [blame] | 194 | FieldValue = FieldValue.lshr(8); |
Anders Carlsson | d24393b | 2009-07-27 01:23:51 +0000 | [diff] [blame] | 195 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 196 | |
Anders Carlsson | d24393b | 2009-07-27 01:23:51 +0000 | [diff] [blame] | 197 | Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp)); |
| 198 | NextFieldOffsetInBytes++; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | |
Anders Carlsson | d24393b | 2009-07-27 01:23:51 +0000 | [diff] [blame] | 200 | FieldValue.trunc(FieldValue.getBitWidth() - 8); |
| 201 | } |
| 202 | |
| 203 | assert(FieldValue.getBitWidth() > 0 && |
| 204 | "Should have at least one bit left!"); |
| 205 | assert(FieldValue.getBitWidth() <= 8 && |
| 206 | "Should not have more than a byte left!"); |
| 207 | |
| 208 | if (FieldValue.getBitWidth() < 8) { |
| 209 | if (CGM.getTargetData().isBigEndian()) { |
| 210 | unsigned BitWidth = FieldValue.getBitWidth(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | |
Anders Carlsson | d24393b | 2009-07-27 01:23:51 +0000 | [diff] [blame] | 212 | FieldValue.zext(8); |
| 213 | FieldValue = FieldValue << (8 - BitWidth); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | } else |
Anders Carlsson | d24393b | 2009-07-27 01:23:51 +0000 | [diff] [blame] | 215 | FieldValue.zext(8); |
| 216 | } |
| 217 | |
| 218 | // Append the last element. |
| 219 | Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(), |
| 220 | FieldValue)); |
| 221 | NextFieldOffsetInBytes++; |
| 222 | return true; |
| 223 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 225 | void AppendPadding(uint64_t NumBytes) { |
| 226 | if (!NumBytes) |
| 227 | return; |
| 228 | |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 229 | const llvm::Type *Ty = llvm::Type::getInt8Ty(CGM.getLLVMContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | if (NumBytes > 1) |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 231 | Ty = llvm::ArrayType::get(Ty, NumBytes); |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 232 | |
Nuno Lopes | c6eb131 | 2009-10-29 11:27:06 +0000 | [diff] [blame] | 233 | llvm::Constant *C = llvm::UndefValue::get(Ty); |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 234 | Elements.push_back(C); |
| 235 | assert(getAlignment(C) == 1 && "Padding must have 1 byte alignment!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 237 | NextFieldOffsetInBytes += getSizeInBytes(C); |
| 238 | } |
| 239 | |
| 240 | void AppendTailPadding(uint64_t RecordSize) { |
| 241 | assert(RecordSize % 8 == 0 && "Invalid record size!"); |
| 242 | |
| 243 | uint64_t RecordSizeInBytes = RecordSize / 8; |
| 244 | assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 246 | unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes; |
| 247 | AppendPadding(NumPadBytes); |
| 248 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | |
Anders Carlsson | 31f2f9c | 2009-10-02 02:15:20 +0000 | [diff] [blame] | 250 | void ConvertStructToPacked() { |
| 251 | std::vector<llvm::Constant *> PackedElements; |
| 252 | uint64_t ElementOffsetInBytes = 0; |
| 253 | |
| 254 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
| 255 | llvm::Constant *C = Elements[i]; |
| 256 | |
| 257 | unsigned ElementAlign = |
| 258 | CGM.getTargetData().getABITypeAlignment(C->getType()); |
| 259 | uint64_t AlignedElementOffsetInBytes = |
| 260 | llvm::RoundUpToAlignment(ElementOffsetInBytes, ElementAlign); |
| 261 | |
| 262 | if (AlignedElementOffsetInBytes > ElementOffsetInBytes) { |
| 263 | // We need some padding. |
| 264 | uint64_t NumBytes = |
| 265 | AlignedElementOffsetInBytes - ElementOffsetInBytes; |
| 266 | |
Eli Friedman | 64241fc | 2009-11-13 21:23:46 +0000 | [diff] [blame] | 267 | const llvm::Type *Ty = llvm::Type::getInt8Ty(CGM.getLLVMContext()); |
Anders Carlsson | 31f2f9c | 2009-10-02 02:15:20 +0000 | [diff] [blame] | 268 | if (NumBytes > 1) |
| 269 | Ty = llvm::ArrayType::get(Ty, NumBytes); |
| 270 | |
Nuno Lopes | c6eb131 | 2009-10-29 11:27:06 +0000 | [diff] [blame] | 271 | llvm::Constant *Padding = llvm::UndefValue::get(Ty); |
Anders Carlsson | 31f2f9c | 2009-10-02 02:15:20 +0000 | [diff] [blame] | 272 | PackedElements.push_back(Padding); |
| 273 | ElementOffsetInBytes += getSizeInBytes(Padding); |
| 274 | } |
| 275 | |
| 276 | PackedElements.push_back(C); |
| 277 | ElementOffsetInBytes += getSizeInBytes(C); |
| 278 | } |
| 279 | |
| 280 | assert(ElementOffsetInBytes == NextFieldOffsetInBytes && |
| 281 | "Packing the struct changed its size!"); |
| 282 | |
| 283 | Elements = PackedElements; |
| 284 | LLVMStructAlignment = 1; |
| 285 | Packed = true; |
| 286 | } |
| 287 | |
Eli Friedman | c0879bb | 2009-08-01 23:11:24 +0000 | [diff] [blame] | 288 | bool Build(InitListExpr *ILE) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 289 | RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl(); |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 290 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 292 | unsigned FieldNo = 0; |
| 293 | unsigned ElementNo = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 295 | FieldEnd = RD->field_end(); |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 296 | ElementNo < ILE->getNumInits() && Field != FieldEnd; |
| 297 | ++Field, ++FieldNo) { |
Eli Friedman | c0879bb | 2009-08-01 23:11:24 +0000 | [diff] [blame] | 298 | if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field) |
| 299 | continue; |
| 300 | |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 301 | if (Field->isBitField()) { |
| 302 | if (!Field->getIdentifier()) |
| 303 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | |
Anders Carlsson | d24393b | 2009-07-27 01:23:51 +0000 | [diff] [blame] | 305 | if (!AppendBitField(*Field, Layout.getFieldOffset(FieldNo), |
| 306 | ILE->getInit(ElementNo))) |
| 307 | return false; |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 308 | } else { |
| 309 | if (!AppendField(*Field, Layout.getFieldOffset(FieldNo), |
| 310 | ILE->getInit(ElementNo))) |
| 311 | return false; |
| 312 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 313 | |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 314 | ElementNo++; |
| 315 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 316 | |
Anders Carlsson | 4257bc6 | 2009-07-25 00:24:56 +0000 | [diff] [blame] | 317 | uint64_t LayoutSizeInBytes = Layout.getSize() / 8; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | |
Anders Carlsson | 4257bc6 | 2009-07-25 00:24:56 +0000 | [diff] [blame] | 319 | if (NextFieldOffsetInBytes > LayoutSizeInBytes) { |
| 320 | // If the struct is bigger than the size of the record type, |
| 321 | // we must have a flexible array member at the end. |
| 322 | assert(RD->hasFlexibleArrayMember() && |
| 323 | "Must have flexible array member if struct is bigger than type!"); |
Anders Carlsson | 31f2f9c | 2009-10-02 02:15:20 +0000 | [diff] [blame] | 324 | |
Anders Carlsson | 4257bc6 | 2009-07-25 00:24:56 +0000 | [diff] [blame] | 325 | // No tail padding is necessary. |
| 326 | return true; |
| 327 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | |
Anders Carlsson | 31f2f9c | 2009-10-02 02:15:20 +0000 | [diff] [blame] | 329 | uint64_t LLVMSizeInBytes = llvm::RoundUpToAlignment(NextFieldOffsetInBytes, |
| 330 | LLVMStructAlignment); |
| 331 | |
| 332 | // Check if we need to convert the struct to a packed struct. |
| 333 | if (NextFieldOffsetInBytes <= LayoutSizeInBytes && |
| 334 | LLVMSizeInBytes > LayoutSizeInBytes) { |
| 335 | assert(!Packed && "Size mismatch!"); |
| 336 | |
| 337 | ConvertStructToPacked(); |
| 338 | assert(NextFieldOffsetInBytes == LayoutSizeInBytes && |
| 339 | "Converting to packed did not help!"); |
| 340 | } |
| 341 | |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 342 | // Append tail padding if necessary. |
| 343 | AppendTailPadding(Layout.getSize()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 344 | |
| 345 | assert(Layout.getSize() / 8 == NextFieldOffsetInBytes && |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 346 | "Tail padding mismatch!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 347 | |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 348 | return true; |
| 349 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 350 | |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 351 | unsigned getAlignment(const llvm::Constant *C) const { |
| 352 | if (Packed) |
| 353 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 355 | return CGM.getTargetData().getABITypeAlignment(C->getType()); |
| 356 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 358 | uint64_t getSizeInBytes(const llvm::Constant *C) const { |
| 359 | return CGM.getTargetData().getTypeAllocSize(C->getType()); |
| 360 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 361 | |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 362 | public: |
| 363 | static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF, |
Eli Friedman | c0879bb | 2009-08-01 23:11:24 +0000 | [diff] [blame] | 364 | InitListExpr *ILE) { |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 365 | ConstStructBuilder Builder(CGM, CGF); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 366 | |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 367 | if (!Builder.Build(ILE)) |
| 368 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 369 | |
| 370 | llvm::Constant *Result = |
Daniel Dunbar | a10f7ea | 2009-08-06 01:24:27 +0000 | [diff] [blame] | 371 | llvm::ConstantStruct::get(CGM.getLLVMContext(), |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 372 | Builder.Elements, Builder.Packed); |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 373 | |
Anders Carlsson | 4257bc6 | 2009-07-25 00:24:56 +0000 | [diff] [blame] | 374 | assert(llvm::RoundUpToAlignment(Builder.NextFieldOffsetInBytes, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 375 | Builder.getAlignment(Result)) == |
Anders Carlsson | 4257bc6 | 2009-07-25 00:24:56 +0000 | [diff] [blame] | 376 | Builder.getSizeInBytes(Result) && "Size mismatch!"); |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 377 | |
Anders Carlsson | 5602719 | 2009-07-24 18:20:38 +0000 | [diff] [blame] | 378 | return Result; |
Anders Carlsson | 4c98efd | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 379 | } |
| 380 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 381 | |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 382 | class ConstExprEmitter : |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 383 | public StmtVisitor<ConstExprEmitter, llvm::Constant*> { |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 384 | CodeGenModule &CGM; |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 385 | CodeGenFunction *CGF; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 386 | llvm::LLVMContext &VMContext; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 387 | public: |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 388 | ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf) |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 389 | : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()) { |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 390 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 391 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 392 | //===--------------------------------------------------------------------===// |
| 393 | // Visitor Methods |
| 394 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 395 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 396 | llvm::Constant *VisitStmt(Stmt *S) { |
Anders Carlsson | 069880e | 2009-03-03 16:43:34 +0000 | [diff] [blame] | 397 | return 0; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 398 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 399 | |
| 400 | llvm::Constant *VisitParenExpr(ParenExpr *PE) { |
| 401 | return Visit(PE->getSubExpr()); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 402 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 403 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 404 | llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 405 | return Visit(E->getInitializer()); |
| 406 | } |
Chris Lattner | 3ae9f48 | 2009-10-13 07:14:16 +0000 | [diff] [blame] | 407 | |
Anders Carlsson | f57b4e4 | 2009-10-03 15:02:02 +0000 | [diff] [blame] | 408 | llvm::Constant *EmitMemberFunctionPointer(CXXMethodDecl *MD) { |
| 409 | assert(MD->isInstance() && "Member function must not be static!"); |
| 410 | |
Anders Carlsson | 7af4ec7 | 2010-01-05 05:04:05 +0000 | [diff] [blame] | 411 | MD = MD->getCanonicalDecl(); |
| 412 | |
Anders Carlsson | f57b4e4 | 2009-10-03 15:02:02 +0000 | [diff] [blame] | 413 | const llvm::Type *PtrDiffTy = |
| 414 | CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType()); |
| 415 | |
| 416 | llvm::Constant *Values[2]; |
| 417 | |
| 418 | // Get the function pointer (or index if this is a virtual function). |
| 419 | if (MD->isVirtual()) { |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 420 | uint64_t Index = CGM.getVTables().getMethodVtableIndex(MD); |
Anders Carlsson | e8a81f7 | 2010-02-04 16:38:05 +0000 | [diff] [blame] | 421 | |
| 422 | // Itanium C++ ABI 2.3: |
| 423 | // For a non-virtual function, this field is a simple function pointer. |
| 424 | // For a virtual function, it is 1 plus the virtual table offset |
| 425 | // (in bytes) of the function, represented as a ptrdiff_t. |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 426 | Values[0] = llvm::ConstantInt::get(PtrDiffTy, (Index * 8) + 1); |
Anders Carlsson | f57b4e4 | 2009-10-03 15:02:02 +0000 | [diff] [blame] | 427 | } else { |
Anders Carlsson | 585fa68 | 2010-02-07 17:37:13 +0000 | [diff] [blame] | 428 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 429 | const llvm::Type *Ty = |
| 430 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 431 | FPT->isVariadic()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | |
Anders Carlsson | 585fa68 | 2010-02-07 17:37:13 +0000 | [diff] [blame] | 433 | llvm::Constant *FuncPtr = CGM.GetAddrOfFunction(MD, Ty); |
Anders Carlsson | f57b4e4 | 2009-10-03 15:02:02 +0000 | [diff] [blame] | 434 | Values[0] = llvm::ConstantExpr::getPtrToInt(FuncPtr, PtrDiffTy); |
| 435 | } |
| 436 | |
| 437 | // The adjustment will always be 0. |
| 438 | Values[1] = llvm::ConstantInt::get(PtrDiffTy, 0); |
| 439 | |
| 440 | return llvm::ConstantStruct::get(CGM.getLLVMContext(), |
| 441 | Values, 2, /*Packed=*/false); |
| 442 | } |
| 443 | |
| 444 | llvm::Constant *VisitUnaryAddrOf(UnaryOperator *E) { |
| 445 | if (const MemberPointerType *MPT = |
| 446 | E->getType()->getAs<MemberPointerType>()) { |
| 447 | QualType T = MPT->getPointeeType(); |
Anders Carlsson | 45147d0 | 2010-02-02 03:37:46 +0000 | [diff] [blame] | 448 | DeclRefExpr *DRE = cast<DeclRefExpr>(E->getSubExpr()); |
| 449 | |
| 450 | NamedDecl *ND = DRE->getDecl(); |
| 451 | if (T->isFunctionProtoType()) |
| 452 | return EmitMemberFunctionPointer(cast<CXXMethodDecl>(ND)); |
Anders Carlsson | f57b4e4 | 2009-10-03 15:02:02 +0000 | [diff] [blame] | 453 | |
Anders Carlsson | 45147d0 | 2010-02-02 03:37:46 +0000 | [diff] [blame] | 454 | // We have a pointer to data member. |
| 455 | return CGM.EmitPointerToDataMember(cast<FieldDecl>(ND)); |
Anders Carlsson | f57b4e4 | 2009-10-03 15:02:02 +0000 | [diff] [blame] | 456 | } |
Anders Carlsson | 45147d0 | 2010-02-02 03:37:46 +0000 | [diff] [blame] | 457 | |
Anders Carlsson | f57b4e4 | 2009-10-03 15:02:02 +0000 | [diff] [blame] | 458 | return 0; |
| 459 | } |
| 460 | |
Chris Lattner | 3ae9f48 | 2009-10-13 07:14:16 +0000 | [diff] [blame] | 461 | llvm::Constant *VisitBinSub(BinaryOperator *E) { |
| 462 | // This must be a pointer/pointer subtraction. This only happens for |
| 463 | // address of label. |
| 464 | if (!isa<AddrLabelExpr>(E->getLHS()->IgnoreParenNoopCasts(CGM.getContext())) || |
| 465 | !isa<AddrLabelExpr>(E->getRHS()->IgnoreParenNoopCasts(CGM.getContext()))) |
| 466 | return 0; |
| 467 | |
| 468 | llvm::Constant *LHS = CGM.EmitConstantExpr(E->getLHS(), |
| 469 | E->getLHS()->getType(), CGF); |
| 470 | llvm::Constant *RHS = CGM.EmitConstantExpr(E->getRHS(), |
| 471 | E->getRHS()->getType(), CGF); |
| 472 | |
| 473 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 474 | LHS = llvm::ConstantExpr::getPtrToInt(LHS, ResultType); |
| 475 | RHS = llvm::ConstantExpr::getPtrToInt(RHS, ResultType); |
| 476 | |
| 477 | // No need to divide by element size, since addr of label is always void*, |
| 478 | // which has size 1 in GNUish. |
| 479 | return llvm::ConstantExpr::getSub(LHS, RHS); |
| 480 | } |
| 481 | |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 482 | llvm::Constant *VisitCastExpr(CastExpr* E) { |
Anders Carlsson | 0086ccb | 2009-08-22 23:54:44 +0000 | [diff] [blame] | 483 | switch (E->getCastKind()) { |
| 484 | case CastExpr::CK_ToUnion: { |
| 485 | // GCC cast to union extension |
| 486 | assert(E->getType()->isUnionType() && |
| 487 | "Destination type is not union type!"); |
Nuno Lopes | 81e51e2 | 2009-01-17 00:48:48 +0000 | [diff] [blame] | 488 | const llvm::Type *Ty = ConvertType(E->getType()); |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 489 | Expr *SubExpr = E->getSubExpr(); |
Anders Carlsson | 0086ccb | 2009-08-22 23:54:44 +0000 | [diff] [blame] | 490 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | llvm::Constant *C = |
Anders Carlsson | aaed50d | 2009-07-31 21:38:39 +0000 | [diff] [blame] | 492 | CGM.EmitConstantExpr(SubExpr, SubExpr->getType(), CGF); |
| 493 | if (!C) |
| 494 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | |
Anders Carlsson | aaed50d | 2009-07-31 21:38:39 +0000 | [diff] [blame] | 496 | // Build a struct with the union sub-element as the first member, |
| 497 | // and padded to the appropriate size |
| 498 | std::vector<llvm::Constant*> Elts; |
| 499 | std::vector<const llvm::Type*> Types; |
| 500 | Elts.push_back(C); |
| 501 | Types.push_back(C->getType()); |
| 502 | unsigned CurSize = CGM.getTargetData().getTypeAllocSize(C->getType()); |
| 503 | unsigned TotalSize = CGM.getTargetData().getTypeAllocSize(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 504 | |
Anders Carlsson | aaed50d | 2009-07-31 21:38:39 +0000 | [diff] [blame] | 505 | assert(CurSize <= TotalSize && "Union size mismatch!"); |
| 506 | if (unsigned NumPadBytes = TotalSize - CurSize) { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 507 | const llvm::Type *Ty = llvm::Type::getInt8Ty(VMContext); |
Anders Carlsson | aaed50d | 2009-07-31 21:38:39 +0000 | [diff] [blame] | 508 | if (NumPadBytes > 1) |
| 509 | Ty = llvm::ArrayType::get(Ty, NumPadBytes); |
Anders Carlsson | 0086ccb | 2009-08-22 23:54:44 +0000 | [diff] [blame] | 510 | |
Nuno Lopes | c6eb131 | 2009-10-29 11:27:06 +0000 | [diff] [blame] | 511 | Elts.push_back(llvm::UndefValue::get(Ty)); |
Anders Carlsson | aaed50d | 2009-07-31 21:38:39 +0000 | [diff] [blame] | 512 | Types.push_back(Ty); |
| 513 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 514 | |
Anders Carlsson | 0086ccb | 2009-08-22 23:54:44 +0000 | [diff] [blame] | 515 | llvm::StructType* STy = |
| 516 | llvm::StructType::get(C->getType()->getContext(), Types, false); |
Anders Carlsson | aaed50d | 2009-07-31 21:38:39 +0000 | [diff] [blame] | 517 | return llvm::ConstantStruct::get(STy, Elts); |
Nuno Lopes | 81e51e2 | 2009-01-17 00:48:48 +0000 | [diff] [blame] | 518 | } |
Anders Carlsson | d1a9bac | 2009-08-23 00:02:11 +0000 | [diff] [blame] | 519 | case CastExpr::CK_NullToMemberPointer: |
| 520 | return CGM.EmitNullConstant(E->getType()); |
Anders Carlsson | 2c51f09 | 2009-10-03 15:13:22 +0000 | [diff] [blame] | 521 | |
| 522 | case CastExpr::CK_BaseToDerivedMemberPointer: { |
| 523 | Expr *SubExpr = E->getSubExpr(); |
| 524 | |
| 525 | const MemberPointerType *SrcTy = |
| 526 | SubExpr->getType()->getAs<MemberPointerType>(); |
| 527 | const MemberPointerType *DestTy = |
| 528 | E->getType()->getAs<MemberPointerType>(); |
| 529 | |
| 530 | const CXXRecordDecl *BaseClass = |
| 531 | cast<CXXRecordDecl>(cast<RecordType>(SrcTy->getClass())->getDecl()); |
| 532 | const CXXRecordDecl *DerivedClass = |
| 533 | cast<CXXRecordDecl>(cast<RecordType>(DestTy->getClass())->getDecl()); |
| 534 | |
| 535 | if (SrcTy->getPointeeType()->isFunctionProtoType()) { |
| 536 | llvm::Constant *C = |
| 537 | CGM.EmitConstantExpr(SubExpr, SubExpr->getType(), CGF); |
| 538 | if (!C) |
| 539 | return 0; |
| 540 | |
| 541 | llvm::ConstantStruct *CS = cast<llvm::ConstantStruct>(C); |
| 542 | |
| 543 | // Check if we need to update the adjustment. |
Anders Carlsson | bb7e17b | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 544 | if (llvm::Constant *Offset = |
| 545 | CGM.GetNonVirtualBaseClassOffset(DerivedClass, BaseClass)) { |
Anders Carlsson | 2c51f09 | 2009-10-03 15:13:22 +0000 | [diff] [blame] | 546 | llvm::Constant *Values[2]; |
| 547 | |
| 548 | Values[0] = CS->getOperand(0); |
| 549 | Values[1] = llvm::ConstantExpr::getAdd(CS->getOperand(1), Offset); |
| 550 | return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2, |
| 551 | /*Packed=*/false); |
| 552 | } |
| 553 | |
| 554 | return CS; |
| 555 | } |
| 556 | } |
Anders Carlsson | bb378cb | 2009-10-18 20:31:03 +0000 | [diff] [blame] | 557 | |
| 558 | case CastExpr::CK_BitCast: |
| 559 | // This must be a member function pointer cast. |
| 560 | return Visit(E->getSubExpr()); |
| 561 | |
Anders Carlsson | 0086ccb | 2009-08-22 23:54:44 +0000 | [diff] [blame] | 562 | default: { |
| 563 | // FIXME: This should be handled by the CK_NoOp cast kind. |
| 564 | // Explicit and implicit no-op casts |
| 565 | QualType Ty = E->getType(), SubTy = E->getSubExpr()->getType(); |
| 566 | if (CGM.getContext().hasSameUnqualifiedType(Ty, SubTy)) |
Chris Lattner | 430656e | 2009-10-13 22:12:09 +0000 | [diff] [blame] | 567 | return Visit(E->getSubExpr()); |
| 568 | |
| 569 | // Handle integer->integer casts for address-of-label differences. |
| 570 | if (Ty->isIntegerType() && SubTy->isIntegerType() && |
| 571 | CGF) { |
| 572 | llvm::Value *Src = Visit(E->getSubExpr()); |
| 573 | if (Src == 0) return 0; |
| 574 | |
| 575 | // Use EmitScalarConversion to perform the conversion. |
| 576 | return cast<llvm::Constant>(CGF->EmitScalarConversion(Src, SubTy, Ty)); |
| 577 | } |
| 578 | |
Anders Carlsson | 0086ccb | 2009-08-22 23:54:44 +0000 | [diff] [blame] | 579 | return 0; |
Eli Friedman | ddd2b6e | 2009-02-22 07:29:04 +0000 | [diff] [blame] | 580 | } |
Anders Carlsson | 0086ccb | 2009-08-22 23:54:44 +0000 | [diff] [blame] | 581 | } |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 582 | } |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 583 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 584 | llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { |
| 585 | return Visit(DAE->getExpr()); |
| 586 | } |
| 587 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 588 | llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) { |
Eli Friedman | 4d1c018 | 2008-02-21 17:45:41 +0000 | [diff] [blame] | 589 | std::vector<llvm::Constant*> Elts; |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 590 | const llvm::ArrayType *AType = |
| 591 | cast<llvm::ArrayType>(ConvertType(ILE->getType())); |
Eli Friedman | 4d1c018 | 2008-02-21 17:45:41 +0000 | [diff] [blame] | 592 | unsigned NumInitElements = ILE->getNumInits(); |
| 593 | // FIXME: Check for wide strings |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 594 | // FIXME: Check for NumInitElements exactly equal to 1?? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 595 | if (NumInitElements > 0 && |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 596 | (isa<StringLiteral>(ILE->getInit(0)) || |
| 597 | isa<ObjCEncodeExpr>(ILE->getInit(0))) && |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 598 | ILE->getType()->getArrayElementTypeNoTypeQual()->isCharType()) |
Eli Friedman | 4d1c018 | 2008-02-21 17:45:41 +0000 | [diff] [blame] | 599 | return Visit(ILE->getInit(0)); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 600 | const llvm::Type *ElemTy = AType->getElementType(); |
| 601 | unsigned NumElements = AType->getNumElements(); |
| 602 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 603 | // Initialising an array requires us to automatically |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 604 | // initialise any elements that have not been initialised explicitly |
| 605 | unsigned NumInitableElts = std::min(NumInitElements, NumElements); |
| 606 | |
| 607 | // Copy initializer elements. |
| 608 | unsigned i = 0; |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 609 | bool RewriteType = false; |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 610 | for (; i < NumInitableElts; ++i) { |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 611 | Expr *Init = ILE->getInit(i); |
| 612 | llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF); |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 613 | if (!C) |
| 614 | return 0; |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 615 | RewriteType |= (C->getType() != ElemTy); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 616 | Elts.push_back(C); |
| 617 | } |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 618 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 619 | // Initialize remaining array elements. |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 620 | // FIXME: This doesn't handle member pointers correctly! |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 621 | for (; i < NumElements; ++i) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 622 | Elts.push_back(llvm::Constant::getNullValue(ElemTy)); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 623 | |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 624 | if (RewriteType) { |
| 625 | // FIXME: Try to avoid packing the array |
| 626 | std::vector<const llvm::Type*> Types; |
| 627 | for (unsigned i = 0; i < Elts.size(); ++i) |
| 628 | Types.push_back(Elts[i]->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 629 | const llvm::StructType *SType = llvm::StructType::get(AType->getContext(), |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 630 | Types, true); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 631 | return llvm::ConstantStruct::get(SType, Elts); |
Eli Friedman | a04a153 | 2008-05-30 19:58:50 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 634 | return llvm::ConstantArray::get(AType, Elts); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 637 | llvm::Constant *EmitStructInitialization(InitListExpr *ILE) { |
Anders Carlsson | 8dca3b3 | 2009-07-27 05:54:15 +0000 | [diff] [blame] | 638 | return ConstStructBuilder::BuildStruct(CGM, CGF, ILE); |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 639 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 640 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 641 | llvm::Constant *EmitUnionInitialization(InitListExpr *ILE) { |
Anders Carlsson | f6c4396 | 2009-07-31 21:34:04 +0000 | [diff] [blame] | 642 | return ConstStructBuilder::BuildStruct(CGM, CGF, ILE); |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Anders Carlsson | 7d801ea | 2009-01-30 06:13:25 +0000 | [diff] [blame] | 645 | llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E) { |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 646 | return CGM.EmitNullConstant(E->getType()); |
Anders Carlsson | 7d801ea | 2009-01-30 06:13:25 +0000 | [diff] [blame] | 647 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 648 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 649 | llvm::Constant *VisitInitListExpr(InitListExpr *ILE) { |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 650 | if (ILE->getType()->isScalarType()) { |
| 651 | // We have a scalar in braces. Just use the first element. |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 652 | if (ILE->getNumInits() > 0) { |
| 653 | Expr *Init = ILE->getInit(0); |
| 654 | return CGM.EmitConstantExpr(Init, Init->getType(), CGF); |
| 655 | } |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 656 | return CGM.EmitNullConstant(ILE->getType()); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 657 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 658 | |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 659 | if (ILE->getType()->isArrayType()) |
| 660 | return EmitArrayInitialization(ILE); |
Devang Patel | 47fb697 | 2008-01-29 23:23:18 +0000 | [diff] [blame] | 661 | |
Anders Carlsson | 01a79ac | 2009-11-21 23:56:04 +0000 | [diff] [blame] | 662 | if (ILE->getType()->isRecordType()) |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 663 | return EmitStructInitialization(ILE); |
| 664 | |
| 665 | if (ILE->getType()->isUnionType()) |
| 666 | return EmitUnionInitialization(ILE); |
| 667 | |
Eli Friedman | 8ac5549 | 2010-01-02 23:43:59 +0000 | [diff] [blame] | 668 | // If ILE was a constant vector, we would have handled it already. |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 669 | if (ILE->getType()->isVectorType()) |
Eli Friedman | 8ac5549 | 2010-01-02 23:43:59 +0000 | [diff] [blame] | 670 | return 0; |
Eli Friedman | a823400 | 2008-05-30 10:24:46 +0000 | [diff] [blame] | 671 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 672 | assert(0 && "Unable to handle InitListExpr"); |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 673 | // Get rid of control reaches end of void function warning. |
| 674 | // Not reached. |
| 675 | return 0; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 676 | } |
Eli Friedman | 3b707e7 | 2008-02-21 17:57:49 +0000 | [diff] [blame] | 677 | |
John McCall | fb8b69a | 2010-02-02 08:02:49 +0000 | [diff] [blame] | 678 | llvm::Constant *VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 679 | if (!E->getConstructor()->isTrivial()) |
| 680 | return 0; |
| 681 | |
Anders Carlsson | 2ca4f63 | 2010-02-05 18:38:45 +0000 | [diff] [blame] | 682 | QualType Ty = E->getType(); |
| 683 | |
| 684 | // FIXME: We should not have to call getBaseElementType here. |
| 685 | const RecordType *RT = |
| 686 | CGM.getContext().getBaseElementType(Ty)->getAs<RecordType>(); |
| 687 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 688 | |
| 689 | // If the class doesn't have a trivial destructor, we can't emit it as a |
| 690 | // constant expr. |
| 691 | if (!RD->hasTrivialDestructor()) |
| 692 | return 0; |
| 693 | |
John McCall | fb8b69a | 2010-02-02 08:02:49 +0000 | [diff] [blame] | 694 | // Only copy and default constructors can be trivial. |
| 695 | |
John McCall | fb8b69a | 2010-02-02 08:02:49 +0000 | [diff] [blame] | 696 | |
| 697 | if (E->getNumArgs()) { |
| 698 | assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument"); |
| 699 | assert(E->getConstructor()->isCopyConstructor() && |
| 700 | "trivial ctor has argument but isn't a copy ctor"); |
| 701 | |
| 702 | Expr *Arg = E->getArg(0); |
| 703 | assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) && |
| 704 | "argument to copy ctor is of wrong type"); |
| 705 | |
Chandler Carruth | 878b55c | 2010-02-02 12:15:55 +0000 | [diff] [blame] | 706 | return Visit(Arg); |
John McCall | fb8b69a | 2010-02-02 08:02:49 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | return CGM.EmitNullConstant(Ty); |
| 710 | } |
| 711 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 712 | llvm::Constant *VisitStringLiteral(StringLiteral *E) { |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 713 | assert(!E->getType()->isPointerType() && "Strings are always arrays"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 715 | // This must be a string initializing an array in a static initializer. |
| 716 | // Don't emit it as the address of the string, emit the string data itself |
| 717 | // as an inline array. |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 718 | return llvm::ConstantArray::get(VMContext, |
| 719 | CGM.GetStringForStringLiteral(E), false); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 722 | llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E) { |
| 723 | // This must be an @encode initializing an array in a static initializer. |
| 724 | // Don't emit it as the address of the string, emit the string data itself |
| 725 | // as an inline array. |
| 726 | std::string Str; |
| 727 | CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str); |
| 728 | const ConstantArrayType *CAT = cast<ConstantArrayType>(E->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 729 | |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 730 | // Resize the string to the right size, adding zeros at the end, or |
| 731 | // truncating as needed. |
| 732 | Str.resize(CAT->getSize().getZExtValue(), '\0'); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 733 | return llvm::ConstantArray::get(VMContext, Str, false); |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 734 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 735 | |
Eli Friedman | bc5ed6e | 2008-05-29 11:22:45 +0000 | [diff] [blame] | 736 | llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) { |
| 737 | return Visit(E->getSubExpr()); |
| 738 | } |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 739 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 740 | // Utility methods |
| 741 | const llvm::Type *ConvertType(QualType T) { |
| 742 | return CGM.getTypes().ConvertType(T); |
| 743 | } |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 744 | |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 745 | public: |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 746 | llvm::Constant *EmitLValue(Expr *E) { |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 747 | switch (E->getStmtClass()) { |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 748 | default: break; |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 749 | case Expr::CompoundLiteralExprClass: { |
| 750 | // Note that due to the nature of compound literals, this is guaranteed |
| 751 | // 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] | 752 | CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); |
| 753 | llvm::Constant* C = Visit(CLE->getInitializer()); |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 754 | // FIXME: "Leaked" on failure. |
| 755 | if (C) |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 756 | C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), |
Eli Friedman | d15d8c6 | 2009-08-26 20:01:39 +0000 | [diff] [blame] | 757 | E->getType().isConstant(CGM.getContext()), |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 758 | llvm::GlobalValue::InternalLinkage, |
Eli Friedman | d15d8c6 | 2009-08-26 20:01:39 +0000 | [diff] [blame] | 759 | C, ".compoundliteral", 0, false, |
| 760 | E->getType().getAddressSpace()); |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 761 | return C; |
Anders Carlsson | 84005b4 | 2008-01-26 04:30:23 +0000 | [diff] [blame] | 762 | } |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 763 | case Expr::DeclRefExprClass: { |
Rafael Espindola | da18261 | 2010-03-04 21:26:03 +0000 | [diff] [blame] | 764 | ValueDecl *Decl = cast<DeclRefExpr>(E)->getDecl(); |
| 765 | if (Decl->hasAttr<WeakRefAttr>()) |
| 766 | return CGM.GetWeakRefReference(Decl); |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 767 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl)) |
Anders Carlsson | 555b4bb | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 768 | return CGM.GetAddrOfFunction(FD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 769 | if (const VarDecl* VD = dyn_cast<VarDecl>(Decl)) { |
Daniel Dunbar | d01b669 | 2009-02-24 18:41:57 +0000 | [diff] [blame] | 770 | // We can never refer to a variable with local storage. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 771 | if (!VD->hasLocalStorage()) { |
Daniel Dunbar | d01b669 | 2009-02-24 18:41:57 +0000 | [diff] [blame] | 772 | if (VD->isFileVarDecl() || VD->hasExternalStorage()) |
| 773 | return CGM.GetAddrOfGlobalVar(VD); |
| 774 | else if (VD->isBlockVarDecl()) { |
| 775 | assert(CGF && "Can't access static local vars without CGF"); |
| 776 | return CGF->GetAddrOfStaticLocalVar(VD); |
| 777 | } |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 778 | } |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 779 | } |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 780 | break; |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 781 | } |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 782 | case Expr::StringLiteralClass: |
| 783 | return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E)); |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 784 | case Expr::ObjCEncodeExprClass: |
| 785 | return CGM.GetAddrOfConstantStringFromObjCEncode(cast<ObjCEncodeExpr>(E)); |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 786 | case Expr::ObjCStringLiteralClass: { |
| 787 | ObjCStringLiteral* SL = cast<ObjCStringLiteral>(E); |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 788 | llvm::Constant *C = |
| 789 | CGM.getObjCRuntime().GenerateConstantString(SL->getString()); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 790 | return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 791 | } |
Chris Lattner | 3534564 | 2008-12-12 05:18:02 +0000 | [diff] [blame] | 792 | case Expr::PredefinedExprClass: { |
Eli Friedman | f70b24e | 2009-11-14 08:37:13 +0000 | [diff] [blame] | 793 | unsigned Type = cast<PredefinedExpr>(E)->getIdentType(); |
| 794 | if (CGF) { |
| 795 | LValue Res = CGF->EmitPredefinedFunctionName(Type); |
| 796 | return cast<llvm::Constant>(Res.getAddress()); |
| 797 | } else if (Type == PredefinedExpr::PrettyFunction) { |
| 798 | return CGM.GetAddrOfConstantCString("top level", ".tmp"); |
| 799 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 800 | |
Eli Friedman | f70b24e | 2009-11-14 08:37:13 +0000 | [diff] [blame] | 801 | return CGM.GetAddrOfConstantCString("", ".tmp"); |
Chris Lattner | 3534564 | 2008-12-12 05:18:02 +0000 | [diff] [blame] | 802 | } |
Eli Friedman | f011589 | 2009-01-25 01:21:06 +0000 | [diff] [blame] | 803 | case Expr::AddrLabelExprClass: { |
| 804 | assert(CGF && "Invalid address of label expression outside function."); |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 805 | llvm::Constant *Ptr = |
| 806 | CGF->GetAddrOfLabel(cast<AddrLabelExpr>(E)->getLabel()); |
| 807 | return llvm::ConstantExpr::getBitCast(Ptr, ConvertType(E->getType())); |
Eli Friedman | f011589 | 2009-01-25 01:21:06 +0000 | [diff] [blame] | 808 | } |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 809 | case Expr::CallExprClass: { |
| 810 | CallExpr* CE = cast<CallExpr>(E); |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 811 | unsigned builtin = CE->isBuiltinCall(CGM.getContext()); |
| 812 | if (builtin != |
| 813 | Builtin::BI__builtin___CFStringMakeConstantString && |
| 814 | builtin != |
| 815 | Builtin::BI__builtin___NSStringMakeConstantString) |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 816 | break; |
| 817 | const Expr *Arg = CE->getArg(0)->IgnoreParenCasts(); |
| 818 | const StringLiteral *Literal = cast<StringLiteral>(Arg); |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 819 | if (builtin == |
| 820 | Builtin::BI__builtin___NSStringMakeConstantString) { |
| 821 | return CGM.getObjCRuntime().GenerateConstantString(Literal); |
| 822 | } |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 823 | // FIXME: need to deal with UCN conversion issues. |
Steve Naroff | 8d4141f | 2009-04-01 13:55:36 +0000 | [diff] [blame] | 824 | return CGM.GetAddrOfConstantCFString(Literal); |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 825 | } |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 826 | case Expr::BlockExprClass: { |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 827 | std::string FunctionName; |
| 828 | if (CGF) |
| 829 | FunctionName = CGF->CurFn->getName(); |
| 830 | else |
| 831 | FunctionName = "global"; |
| 832 | |
| 833 | return CGM.GetAddrOfGlobalBlock(cast<BlockExpr>(E), FunctionName.c_str()); |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 834 | } |
Eli Friedman | 546d94c | 2008-02-11 00:23:10 +0000 | [diff] [blame] | 835 | } |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 836 | |
| 837 | return 0; |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 838 | } |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame] | 839 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 840 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 841 | } // end anonymous namespace. |
| 842 | |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 843 | llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E, |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 844 | QualType DestType, |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 845 | CodeGenFunction *CGF) { |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 846 | Expr::EvalResult Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 847 | |
Anders Carlsson | 1e5dc6e | 2009-04-11 01:08:03 +0000 | [diff] [blame] | 848 | bool Success = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 849 | |
Eli Friedman | 50c39ea | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 850 | if (DestType->isReferenceType()) |
| 851 | Success = E->EvaluateAsLValue(Result, Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 852 | else |
Anders Carlsson | 1e5dc6e | 2009-04-11 01:08:03 +0000 | [diff] [blame] | 853 | Success = E->Evaluate(Result, Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 854 | |
Eli Friedman | 55f9bdd | 2009-11-14 08:51:33 +0000 | [diff] [blame] | 855 | if (Success && !Result.HasSideEffects) { |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 856 | switch (Result.Val.getKind()) { |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 857 | case APValue::Uninitialized: |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 858 | assert(0 && "Constant expressions should be initialized."); |
| 859 | return 0; |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 860 | case APValue::LValue: { |
Anders Carlsson | 1e5dc6e | 2009-04-11 01:08:03 +0000 | [diff] [blame] | 861 | const llvm::Type *DestTy = getTypes().ConvertTypeForMem(DestType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 862 | llvm::Constant *Offset = |
| 863 | llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 864 | Result.Val.getLValueOffset().getQuantity()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 865 | |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 866 | llvm::Constant *C; |
Anders Carlsson | 4b3f9c0 | 2008-12-01 02:42:14 +0000 | [diff] [blame] | 867 | if (const Expr *LVBase = Result.Val.getLValueBase()) { |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 868 | C = ConstExprEmitter(*this, CGF).EmitLValue(const_cast<Expr*>(LVBase)); |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 869 | |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 870 | // Apply offset if necessary. |
| 871 | if (!Offset->isNullValue()) { |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 872 | const llvm::Type *Type = llvm::Type::getInt8PtrTy(VMContext); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 873 | llvm::Constant *Casted = llvm::ConstantExpr::getBitCast(C, Type); |
| 874 | Casted = llvm::ConstantExpr::getGetElementPtr(Casted, &Offset, 1); |
| 875 | C = llvm::ConstantExpr::getBitCast(Casted, C->getType()); |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | // Convert to the appropriate type; this could be an lvalue for |
| 879 | // an integer. |
Anders Carlsson | 12e330c | 2009-04-10 04:59:59 +0000 | [diff] [blame] | 880 | if (isa<llvm::PointerType>(DestTy)) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 881 | return llvm::ConstantExpr::getBitCast(C, DestTy); |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 882 | |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 883 | return llvm::ConstantExpr::getPtrToInt(C, DestTy); |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 884 | } else { |
| 885 | C = Offset; |
| 886 | |
| 887 | // Convert to the appropriate type; this could be an lvalue for |
| 888 | // an integer. |
Anders Carlsson | 12e330c | 2009-04-10 04:59:59 +0000 | [diff] [blame] | 889 | if (isa<llvm::PointerType>(DestTy)) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 890 | return llvm::ConstantExpr::getIntToPtr(C, DestTy); |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 891 | |
| 892 | // If the types don't match this should only be a truncate. |
Anders Carlsson | 12e330c | 2009-04-10 04:59:59 +0000 | [diff] [blame] | 893 | if (C->getType() != DestTy) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 894 | return llvm::ConstantExpr::getTrunc(C, DestTy); |
Daniel Dunbar | e20de51 | 2009-02-19 21:44:24 +0000 | [diff] [blame] | 895 | |
| 896 | return C; |
Anders Carlsson | 5ea7d07 | 2008-11-16 06:23:45 +0000 | [diff] [blame] | 897 | } |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 898 | } |
Eli Friedman | 7b30b1c | 2008-11-17 03:57:28 +0000 | [diff] [blame] | 899 | case APValue::Int: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 900 | llvm::Constant *C = llvm::ConstantInt::get(VMContext, |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 901 | Result.Val.getInt()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 902 | |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 903 | if (C->getType() == llvm::Type::getInt1Ty(VMContext)) { |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 904 | const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType()); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 905 | C = llvm::ConstantExpr::getZExt(C, BoolTy); |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 906 | } |
| 907 | return C; |
Eli Friedman | 7b30b1c | 2008-11-17 03:57:28 +0000 | [diff] [blame] | 908 | } |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 909 | case APValue::ComplexInt: { |
| 910 | llvm::Constant *Complex[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 911 | |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 912 | Complex[0] = llvm::ConstantInt::get(VMContext, |
| 913 | Result.Val.getComplexIntReal()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 914 | Complex[1] = llvm::ConstantInt::get(VMContext, |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 915 | Result.Val.getComplexIntImag()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 916 | |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 917 | // FIXME: the target may want to specify that this is packed. |
| 918 | return llvm::ConstantStruct::get(VMContext, Complex, 2, false); |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 919 | } |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 920 | case APValue::Float: |
Owen Anderson | bc0a222 | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 921 | return llvm::ConstantFP::get(VMContext, Result.Val.getFloat()); |
Anders Carlsson | aa43256 | 2008-11-17 01:58:55 +0000 | [diff] [blame] | 922 | case APValue::ComplexFloat: { |
| 923 | llvm::Constant *Complex[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 924 | |
| 925 | Complex[0] = llvm::ConstantFP::get(VMContext, |
Owen Anderson | bc0a222 | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 926 | Result.Val.getComplexFloatReal()); |
| 927 | Complex[1] = llvm::ConstantFP::get(VMContext, |
| 928 | Result.Val.getComplexFloatImag()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 929 | |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 930 | // FIXME: the target may want to specify that this is packed. |
| 931 | return llvm::ConstantStruct::get(VMContext, Complex, 2, false); |
Anders Carlsson | aa43256 | 2008-11-17 01:58:55 +0000 | [diff] [blame] | 932 | } |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 933 | case APValue::Vector: { |
| 934 | llvm::SmallVector<llvm::Constant *, 4> Inits; |
| 935 | unsigned NumElts = Result.Val.getVectorLength(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 936 | |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 937 | for (unsigned i = 0; i != NumElts; ++i) { |
| 938 | APValue &Elt = Result.Val.getVectorElt(i); |
| 939 | if (Elt.isInt()) |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 940 | Inits.push_back(llvm::ConstantInt::get(VMContext, Elt.getInt())); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 941 | else |
Owen Anderson | bc0a222 | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 942 | Inits.push_back(llvm::ConstantFP::get(VMContext, Elt.getFloat())); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 943 | } |
Owen Anderson | 4a28932 | 2009-07-28 21:22:35 +0000 | [diff] [blame] | 944 | return llvm::ConstantVector::get(&Inits[0], Inits.size()); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 945 | } |
Anders Carlsson | 5a9a457 | 2008-11-15 20:45:50 +0000 | [diff] [blame] | 946 | } |
| 947 | } |
Eli Friedman | 7dfa639 | 2008-06-01 15:31:44 +0000 | [diff] [blame] | 948 | |
| 949 | llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E)); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 950 | if (C && C->getType() == llvm::Type::getInt1Ty(VMContext)) { |
Eli Friedman | 7dfa639 | 2008-06-01 15:31:44 +0000 | [diff] [blame] | 951 | const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType()); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 952 | C = llvm::ConstantExpr::getZExt(C, BoolTy); |
Eli Friedman | 7dfa639 | 2008-06-01 15:31:44 +0000 | [diff] [blame] | 953 | } |
| 954 | return C; |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 955 | } |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 956 | |
Anders Carlsson | 2c12d03 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 957 | static bool containsPointerToDataMember(CodeGenTypes &Types, QualType T) { |
| 958 | // No need to check for member pointers when not compiling C++. |
| 959 | if (!Types.getContext().getLangOptions().CPlusPlus) |
| 960 | return false; |
| 961 | |
| 962 | T = Types.getContext().getBaseElementType(T); |
| 963 | |
| 964 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 965 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 966 | |
| 967 | // FIXME: It would be better if there was a way to explicitly compute the |
| 968 | // record layout instead of converting to a type. |
| 969 | Types.ConvertTagDeclType(RD); |
| 970 | |
| 971 | const CGRecordLayout &Layout = Types.getCGRecordLayout(RD); |
| 972 | return Layout.containsPointerToDataMember(); |
| 973 | } |
| 974 | |
Anders Carlsson | e9d34dc | 2009-09-29 02:09:01 +0000 | [diff] [blame] | 975 | if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) |
| 976 | return !MPT->getPointeeType()->isFunctionType(); |
| 977 | |
| 978 | return false; |
| 979 | } |
| 980 | |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 981 | llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) { |
Anders Carlsson | 2c12d03 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 982 | if (!containsPointerToDataMember(getTypes(), T)) |
Anders Carlsson | ad3e711 | 2009-08-24 17:16:23 +0000 | [diff] [blame] | 983 | return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T)); |
Anders Carlsson | 2c12d03 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 984 | |
Anders Carlsson | 237957c | 2009-08-09 18:26:27 +0000 | [diff] [blame] | 985 | if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 986 | |
Anders Carlsson | 237957c | 2009-08-09 18:26:27 +0000 | [diff] [blame] | 987 | QualType ElementTy = CAT->getElementType(); |
| 988 | |
Anders Carlsson | 2c12d03 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 989 | llvm::Constant *Element = EmitNullConstant(ElementTy); |
| 990 | unsigned NumElements = CAT->getSize().getZExtValue(); |
| 991 | std::vector<llvm::Constant *> Array(NumElements); |
| 992 | for (unsigned i = 0; i != NumElements; ++i) |
| 993 | Array[i] = Element; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 994 | |
Anders Carlsson | 2c12d03 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 995 | const llvm::ArrayType *ATy = |
| 996 | cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T)); |
| 997 | return llvm::ConstantArray::get(ATy, Array); |
Anders Carlsson | 237957c | 2009-08-09 18:26:27 +0000 | [diff] [blame] | 998 | } |
Anders Carlsson | fc3eaa4 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 999 | |
| 1000 | if (const RecordType *RT = T->getAs<RecordType>()) { |
Anders Carlsson | 2c12d03 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 1001 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 1002 | assert(!RD->getNumBases() && |
| 1003 | "FIXME: Handle zero-initializing structs with bases and " |
| 1004 | "pointers to data members."); |
| 1005 | const llvm::StructType *STy = |
| 1006 | cast<llvm::StructType>(getTypes().ConvertTypeForMem(T)); |
| 1007 | unsigned NumElements = STy->getNumElements(); |
| 1008 | std::vector<llvm::Constant *> Elements(NumElements); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1009 | |
Anders Carlsson | 2c12d03 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 1010 | for (RecordDecl::field_iterator I = RD->field_begin(), |
| 1011 | E = RD->field_end(); I != E; ++I) { |
| 1012 | const FieldDecl *FD = *I; |
| 1013 | |
| 1014 | unsigned FieldNo = getTypes().getLLVMFieldNo(FD); |
| 1015 | Elements[FieldNo] = EmitNullConstant(FD->getType()); |
Anders Carlsson | fc3eaa4 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 1016 | } |
Anders Carlsson | 2c12d03 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 1017 | |
| 1018 | // Now go through all other fields and zero them out. |
| 1019 | for (unsigned i = 0; i != NumElements; ++i) { |
| 1020 | if (!Elements[i]) |
| 1021 | Elements[i] = llvm::Constant::getNullValue(STy->getElementType(i)); |
| 1022 | } |
| 1023 | |
| 1024 | return llvm::ConstantStruct::get(STy, Elements); |
Anders Carlsson | fc3eaa4 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 1025 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1026 | |
Anders Carlsson | 2c12d03 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 1027 | assert(!T->getAs<MemberPointerType>()->getPointeeType()->isFunctionType() && |
| 1028 | "Should only see pointers to data members here!"); |
| 1029 | |
| 1030 | // Itanium C++ ABI 2.3: |
| 1031 | // A NULL pointer is represented as -1. |
Chris Lattner | c0f3df3 | 2010-02-10 21:41:43 +0000 | [diff] [blame] | 1032 | return llvm::ConstantInt::get(getTypes().ConvertTypeForMem(T), -1ULL, |
Anders Carlsson | 2c12d03 | 2010-02-02 05:17:25 +0000 | [diff] [blame] | 1033 | /*isSigned=*/true); |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 1034 | } |
Anders Carlsson | 45147d0 | 2010-02-02 03:37:46 +0000 | [diff] [blame] | 1035 | |
| 1036 | llvm::Constant * |
| 1037 | CodeGenModule::EmitPointerToDataMember(const FieldDecl *FD) { |
| 1038 | |
| 1039 | // Itanium C++ ABI 2.3: |
| 1040 | // A pointer to data member is an offset from the base address of the class |
| 1041 | // object containing it, represented as a ptrdiff_t |
| 1042 | |
| 1043 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(FD->getParent()); |
| 1044 | QualType ClassType = |
| 1045 | getContext().getTypeDeclType(const_cast<CXXRecordDecl *>(ClassDecl)); |
| 1046 | |
| 1047 | const llvm::StructType *ClassLTy = |
| 1048 | cast<llvm::StructType>(getTypes().ConvertType(ClassType)); |
| 1049 | |
| 1050 | unsigned FieldNo = getTypes().getLLVMFieldNo(FD); |
| 1051 | uint64_t Offset = |
| 1052 | getTargetData().getStructLayout(ClassLTy)->getElementOffset(FieldNo); |
| 1053 | |
| 1054 | const llvm::Type *PtrDiffTy = |
| 1055 | getTypes().ConvertType(getContext().getPointerDiffType()); |
| 1056 | |
| 1057 | return llvm::ConstantInt::get(PtrDiffTy, Offset); |
| 1058 | } |