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