blob: 0622c101651575192bd167f426fa8bc4722611ba [file] [log] [blame]
Anders Carlsson3b1d57b2008-01-26 01:36:00 +00001//===--- CGExprConstant.cpp - Emit LLVM Code from Constant Expressions ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Constant Expr nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
John McCall4c40d982010-08-31 07:33:07 +000016#include "CGCXXABI.h"
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000017#include "CGObjCRuntime.h"
Daniel Dunbar2924ade2010-03-30 22:26:10 +000018#include "CGRecordLayout.h"
Chris Lattnerf6b24ea2008-10-06 05:59:01 +000019#include "clang/AST/APValue.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000020#include "clang/AST/ASTContext.h"
Anders Carlsson4c98efd2009-07-24 15:20:52 +000021#include "clang/AST/RecordLayout.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Anders Carlsson3b1d57b2008-01-26 01:36:00 +000024#include "llvm/Constants.h"
25#include "llvm/Function.h"
26#include "llvm/GlobalVariable.h"
Eli Friedmana8234002008-05-30 10:24:46 +000027#include "llvm/Target/TargetData.h"
Anders Carlsson3b1d57b2008-01-26 01:36:00 +000028using namespace clang;
29using namespace CodeGen;
30
Chris Lattnereba3e5a2010-04-13 17:45:57 +000031//===----------------------------------------------------------------------===//
32// ConstStructBuilder
33//===----------------------------------------------------------------------===//
34
35namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +000036class ConstStructBuilder {
Anders Carlsson4c98efd2009-07-24 15:20:52 +000037 CodeGenModule &CGM;
38 CodeGenFunction *CGF;
39
Mike Stump1eb44332009-09-09 15:08:12 +000040 bool Packed;
Ken Dyckdc496392011-03-17 01:33:18 +000041 CharUnits NextFieldOffsetInChars;
Ken Dyck7d2f9d52011-03-18 01:26:17 +000042 CharUnits LLVMStructAlignment;
Anders Carlsson4c98efd2009-07-24 15:20:52 +000043 std::vector<llvm::Constant *> Elements;
Chris Lattnereba3e5a2010-04-13 17:45:57 +000044public:
45 static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF,
46 InitListExpr *ILE);
47
48private:
Anders Carlsson4c98efd2009-07-24 15:20:52 +000049 ConstStructBuilder(CodeGenModule &CGM, CodeGenFunction *CGF)
Ken Dyckdc496392011-03-17 01:33:18 +000050 : CGM(CGM), CGF(CGF), Packed(false),
51 NextFieldOffsetInChars(CharUnits::Zero()),
Ken Dyck7d2f9d52011-03-18 01:26:17 +000052 LLVMStructAlignment(CharUnits::One()) { }
Anders Carlsson4c98efd2009-07-24 15:20:52 +000053
Mike Stump1eb44332009-09-09 15:08:12 +000054 bool AppendField(const FieldDecl *Field, uint64_t FieldOffset,
Chris Lattner8ce9e452010-04-13 18:16:19 +000055 llvm::Constant *InitExpr);
Mike Stump1eb44332009-09-09 15:08:12 +000056
Chris Lattner18806662010-07-05 17:04:23 +000057 void AppendBitField(const FieldDecl *Field, uint64_t FieldOffset,
58 llvm::ConstantInt *InitExpr);
Mike Stump1eb44332009-09-09 15:08:12 +000059
Ken Dyckc29ea8f2011-03-11 23:42:54 +000060 void AppendPadding(CharUnits PadSize);
Mike Stump1eb44332009-09-09 15:08:12 +000061
Ken Dyck106ca042011-03-11 02:17:05 +000062 void AppendTailPadding(CharUnits RecordSize);
Anders Carlssond24393b2009-07-27 01:23:51 +000063
Chris Lattnereba3e5a2010-04-13 17:45:57 +000064 void ConvertStructToPacked();
Anders Carlsson31f2f9c2009-10-02 02:15:20 +000065
Chris Lattnereba3e5a2010-04-13 17:45:57 +000066 bool Build(InitListExpr *ILE);
Mike Stump1eb44332009-09-09 15:08:12 +000067
Ken Dyck7d2f9d52011-03-18 01:26:17 +000068 CharUnits getAlignment(const llvm::Constant *C) const {
69 if (Packed) return CharUnits::One();
70 return CharUnits::fromQuantity(
71 CGM.getTargetData().getABITypeAlignment(C->getType()));
Anders Carlsson4c98efd2009-07-24 15:20:52 +000072 }
Mike Stump1eb44332009-09-09 15:08:12 +000073
Ken Dyck15c152e2011-03-18 01:12:13 +000074 CharUnits getSizeInChars(const llvm::Constant *C) const {
75 return CharUnits::fromQuantity(
76 CGM.getTargetData().getTypeAllocSize(C->getType()));
Anders Carlsson4c98efd2009-07-24 15:20:52 +000077 }
Anders Carlsson4c98efd2009-07-24 15:20:52 +000078};
Mike Stump1eb44332009-09-09 15:08:12 +000079
Chris Lattnereba3e5a2010-04-13 17:45:57 +000080bool ConstStructBuilder::
Chris Lattner8ce9e452010-04-13 18:16:19 +000081AppendField(const FieldDecl *Field, uint64_t FieldOffset,
82 llvm::Constant *InitCst) {
Ken Dyck6925cc42011-03-15 01:09:02 +000083
84 const ASTContext &Context = CGM.getContext();
85
86 CharUnits FieldOffsetInChars = Context.toCharUnitsFromBits(FieldOffset);
Chris Lattnereba3e5a2010-04-13 17:45:57 +000087
Ken Dyckdc496392011-03-17 01:33:18 +000088 assert(NextFieldOffsetInChars <= FieldOffsetInChars
Chris Lattnereba3e5a2010-04-13 17:45:57 +000089 && "Field offset mismatch!");
90
Ken Dyck7d2f9d52011-03-18 01:26:17 +000091 CharUnits FieldAlignment = getAlignment(InitCst);
Chris Lattnereba3e5a2010-04-13 17:45:57 +000092
93 // Round up the field offset to the alignment of the field type.
Ken Dyckdc496392011-03-17 01:33:18 +000094 CharUnits AlignedNextFieldOffsetInChars =
Ken Dyck7d2f9d52011-03-18 01:26:17 +000095 NextFieldOffsetInChars.RoundUpToAlignment(FieldAlignment);
Chris Lattnereba3e5a2010-04-13 17:45:57 +000096
Ken Dyckdc496392011-03-17 01:33:18 +000097 if (AlignedNextFieldOffsetInChars > FieldOffsetInChars) {
Chris Lattnereba3e5a2010-04-13 17:45:57 +000098 assert(!Packed && "Alignment is wrong even with a packed struct!");
99
100 // Convert the struct to a packed struct.
101 ConvertStructToPacked();
102
Ken Dyckdc496392011-03-17 01:33:18 +0000103 AlignedNextFieldOffsetInChars = NextFieldOffsetInChars;
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000104 }
105
Ken Dyckdc496392011-03-17 01:33:18 +0000106 if (AlignedNextFieldOffsetInChars < FieldOffsetInChars) {
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000107 // We need to append padding.
Ken Dyckc29ea8f2011-03-11 23:42:54 +0000108 AppendPadding(
Ken Dyckdc496392011-03-17 01:33:18 +0000109 FieldOffsetInChars - NextFieldOffsetInChars);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000110
Ken Dyckdc496392011-03-17 01:33:18 +0000111 assert(NextFieldOffsetInChars == FieldOffsetInChars &&
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000112 "Did not add enough padding!");
113
Ken Dyckdc496392011-03-17 01:33:18 +0000114 AlignedNextFieldOffsetInChars = NextFieldOffsetInChars;
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000115 }
116
117 // Add the field.
Chris Lattner8ce9e452010-04-13 18:16:19 +0000118 Elements.push_back(InitCst);
Ken Dyckdc496392011-03-17 01:33:18 +0000119 NextFieldOffsetInChars = AlignedNextFieldOffsetInChars +
Ken Dyck15c152e2011-03-18 01:12:13 +0000120 getSizeInChars(InitCst);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000121
122 if (Packed)
Ken Dyck7d2f9d52011-03-18 01:26:17 +0000123 assert(LLVMStructAlignment == CharUnits::One() &&
124 "Packed struct not byte-aligned!");
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000125 else
126 LLVMStructAlignment = std::max(LLVMStructAlignment, FieldAlignment);
127
128 return true;
129}
130
Chris Lattner18806662010-07-05 17:04:23 +0000131void ConstStructBuilder::AppendBitField(const FieldDecl *Field,
132 uint64_t FieldOffset,
133 llvm::ConstantInt *CI) {
Ken Dyckf004da42011-03-12 12:03:11 +0000134 const ASTContext &Context = CGM.getContext();
Ken Dyck4f318c02011-03-19 00:57:28 +0000135 const uint64_t CharWidth = Context.getCharWidth();
Ken Dyckdc496392011-03-17 01:33:18 +0000136 uint64_t NextFieldOffsetInBits = Context.toBits(NextFieldOffsetInChars);
137 if (FieldOffset > NextFieldOffsetInBits) {
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000138 // We need to add padding.
Ken Dyckf004da42011-03-12 12:03:11 +0000139 CharUnits PadSize = Context.toCharUnitsFromBits(
Ken Dyckdc496392011-03-17 01:33:18 +0000140 llvm::RoundUpToAlignment(FieldOffset - NextFieldOffsetInBits,
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000141 Context.getTargetInfo().getCharAlign()));
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000142
Ken Dyckf004da42011-03-12 12:03:11 +0000143 AppendPadding(PadSize);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000144 }
145
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000146 uint64_t FieldSize = Field->getBitWidthValue(Context);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000147
148 llvm::APInt FieldValue = CI->getValue();
149
150 // Promote the size of FieldValue if necessary
151 // FIXME: This should never occur, but currently it can because initializer
152 // constants are cast to bool, and because clang is not enforcing bitfield
153 // width limits.
154 if (FieldSize > FieldValue.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000155 FieldValue = FieldValue.zext(FieldSize);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000156
157 // Truncate the size of FieldValue to the bit field size.
158 if (FieldSize < FieldValue.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000159 FieldValue = FieldValue.trunc(FieldSize);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000160
Ken Dyckdc496392011-03-17 01:33:18 +0000161 NextFieldOffsetInBits = Context.toBits(NextFieldOffsetInChars);
162 if (FieldOffset < NextFieldOffsetInBits) {
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000163 // Either part of the field or the entire field can go into the previous
164 // byte.
165 assert(!Elements.empty() && "Elements can't be empty!");
166
Ken Dyckdc496392011-03-17 01:33:18 +0000167 unsigned BitsInPreviousByte = NextFieldOffsetInBits - FieldOffset;
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000168
169 bool FitsCompletelyInPreviousByte =
170 BitsInPreviousByte >= FieldValue.getBitWidth();
171
172 llvm::APInt Tmp = FieldValue;
173
174 if (!FitsCompletelyInPreviousByte) {
175 unsigned NewFieldWidth = FieldSize - BitsInPreviousByte;
176
177 if (CGM.getTargetData().isBigEndian()) {
178 Tmp = Tmp.lshr(NewFieldWidth);
Jay Foad9f71a8f2010-12-07 08:25:34 +0000179 Tmp = Tmp.trunc(BitsInPreviousByte);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000180
181 // We want the remaining high bits.
Jay Foad9f71a8f2010-12-07 08:25:34 +0000182 FieldValue = FieldValue.trunc(NewFieldWidth);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000183 } else {
Jay Foad9f71a8f2010-12-07 08:25:34 +0000184 Tmp = Tmp.trunc(BitsInPreviousByte);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000185
186 // We want the remaining low bits.
187 FieldValue = FieldValue.lshr(BitsInPreviousByte);
Jay Foad9f71a8f2010-12-07 08:25:34 +0000188 FieldValue = FieldValue.trunc(NewFieldWidth);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000189 }
190 }
191
Ken Dyck4f318c02011-03-19 00:57:28 +0000192 Tmp = Tmp.zext(CharWidth);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000193 if (CGM.getTargetData().isBigEndian()) {
194 if (FitsCompletelyInPreviousByte)
195 Tmp = Tmp.shl(BitsInPreviousByte - FieldValue.getBitWidth());
196 } else {
Ken Dyck4f318c02011-03-19 00:57:28 +0000197 Tmp = Tmp.shl(CharWidth - BitsInPreviousByte);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000198 }
199
Chris Lattner2bc00252010-07-05 18:03:30 +0000200 // 'or' in the bits that go into the previous byte.
201 llvm::Value *LastElt = Elements.back();
202 if (llvm::ConstantInt *Val = dyn_cast<llvm::ConstantInt>(LastElt))
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000203 Tmp |= Val->getValue();
Chris Lattner2bc00252010-07-05 18:03:30 +0000204 else {
205 assert(isa<llvm::UndefValue>(LastElt));
206 // If there is an undef field that we're adding to, it can either be a
207 // scalar undef (in which case, we just replace it with our field) or it
208 // is an array. If it is an array, we have to pull one byte off the
209 // array so that the other undef bytes stay around.
210 if (!isa<llvm::IntegerType>(LastElt->getType())) {
211 // The undef padding will be a multibyte array, create a new smaller
212 // padding and then an hole for our i8 to get plopped into.
213 assert(isa<llvm::ArrayType>(LastElt->getType()) &&
214 "Expected array padding of undefs");
Chris Lattner2acc6e32011-07-18 04:24:23 +0000215 llvm::ArrayType *AT = cast<llvm::ArrayType>(LastElt->getType());
Ken Dyck4f318c02011-03-19 00:57:28 +0000216 assert(AT->getElementType()->isIntegerTy(CharWidth) &&
Chris Lattner2bc00252010-07-05 18:03:30 +0000217 AT->getNumElements() != 0 &&
218 "Expected non-empty array padding of undefs");
219
220 // Remove the padding array.
Ken Dyckdc496392011-03-17 01:33:18 +0000221 NextFieldOffsetInChars -= CharUnits::fromQuantity(AT->getNumElements());
Chris Lattner2bc00252010-07-05 18:03:30 +0000222 Elements.pop_back();
223
224 // Add the padding back in two chunks.
Ken Dyckc29ea8f2011-03-11 23:42:54 +0000225 AppendPadding(CharUnits::fromQuantity(AT->getNumElements()-1));
226 AppendPadding(CharUnits::One());
Chris Lattner2bc00252010-07-05 18:03:30 +0000227 assert(isa<llvm::UndefValue>(Elements.back()) &&
Ken Dyck4f318c02011-03-19 00:57:28 +0000228 Elements.back()->getType()->isIntegerTy(CharWidth) &&
Chris Lattner2bc00252010-07-05 18:03:30 +0000229 "Padding addition didn't work right");
230 }
231 }
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000232
233 Elements.back() = llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp);
234
235 if (FitsCompletelyInPreviousByte)
Chris Lattner18806662010-07-05 17:04:23 +0000236 return;
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000237 }
238
Ken Dyck4f318c02011-03-19 00:57:28 +0000239 while (FieldValue.getBitWidth() > CharWidth) {
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000240 llvm::APInt Tmp;
241
242 if (CGM.getTargetData().isBigEndian()) {
243 // We want the high bits.
Ken Dyck4f318c02011-03-19 00:57:28 +0000244 Tmp =
245 FieldValue.lshr(FieldValue.getBitWidth() - CharWidth).trunc(CharWidth);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000246 } else {
247 // We want the low bits.
Ken Dyck4f318c02011-03-19 00:57:28 +0000248 Tmp = FieldValue.trunc(CharWidth);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000249
Ken Dyck4f318c02011-03-19 00:57:28 +0000250 FieldValue = FieldValue.lshr(CharWidth);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000251 }
252
253 Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp));
Ken Dyck199c1892011-03-19 01:28:06 +0000254 ++NextFieldOffsetInChars;
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000255
Ken Dyck4f318c02011-03-19 00:57:28 +0000256 FieldValue = FieldValue.trunc(FieldValue.getBitWidth() - CharWidth);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000257 }
258
259 assert(FieldValue.getBitWidth() > 0 &&
260 "Should have at least one bit left!");
Ken Dyck4f318c02011-03-19 00:57:28 +0000261 assert(FieldValue.getBitWidth() <= CharWidth &&
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000262 "Should not have more than a byte left!");
263
Ken Dyck4f318c02011-03-19 00:57:28 +0000264 if (FieldValue.getBitWidth() < CharWidth) {
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000265 if (CGM.getTargetData().isBigEndian()) {
266 unsigned BitWidth = FieldValue.getBitWidth();
267
Ken Dyck4f318c02011-03-19 00:57:28 +0000268 FieldValue = FieldValue.zext(CharWidth) << (CharWidth - BitWidth);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000269 } else
Ken Dyck4f318c02011-03-19 00:57:28 +0000270 FieldValue = FieldValue.zext(CharWidth);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000271 }
272
273 // Append the last element.
274 Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(),
275 FieldValue));
Ken Dyck199c1892011-03-19 01:28:06 +0000276 ++NextFieldOffsetInChars;
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000277}
278
Ken Dyckc29ea8f2011-03-11 23:42:54 +0000279void ConstStructBuilder::AppendPadding(CharUnits PadSize) {
280 if (PadSize.isZero())
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000281 return;
282
Chris Lattner2acc6e32011-07-18 04:24:23 +0000283 llvm::Type *Ty = llvm::Type::getInt8Ty(CGM.getLLVMContext());
Ken Dyckc29ea8f2011-03-11 23:42:54 +0000284 if (PadSize > CharUnits::One())
285 Ty = llvm::ArrayType::get(Ty, PadSize.getQuantity());
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000286
Nuno Lopescdb30b42010-04-16 20:56:35 +0000287 llvm::Constant *C = llvm::UndefValue::get(Ty);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000288 Elements.push_back(C);
Ken Dyck7d2f9d52011-03-18 01:26:17 +0000289 assert(getAlignment(C) == CharUnits::One() &&
290 "Padding must have 1 byte alignment!");
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000291
Ken Dyck15c152e2011-03-18 01:12:13 +0000292 NextFieldOffsetInChars += getSizeInChars(C);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000293}
294
Ken Dyck106ca042011-03-11 02:17:05 +0000295void ConstStructBuilder::AppendTailPadding(CharUnits RecordSize) {
Ken Dyckdc496392011-03-17 01:33:18 +0000296 assert(NextFieldOffsetInChars <= RecordSize &&
Ken Dyck106ca042011-03-11 02:17:05 +0000297 "Size mismatch!");
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000298
Ken Dyckdc496392011-03-17 01:33:18 +0000299 AppendPadding(RecordSize - NextFieldOffsetInChars);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000300}
301
302void ConstStructBuilder::ConvertStructToPacked() {
303 std::vector<llvm::Constant *> PackedElements;
Ken Dyck4a5c87e2011-03-18 00:55:06 +0000304 CharUnits ElementOffsetInChars = CharUnits::Zero();
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000305
306 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
307 llvm::Constant *C = Elements[i];
308
Ken Dyck7d2f9d52011-03-18 01:26:17 +0000309 CharUnits ElementAlign = CharUnits::fromQuantity(
310 CGM.getTargetData().getABITypeAlignment(C->getType()));
Ken Dyck4a5c87e2011-03-18 00:55:06 +0000311 CharUnits AlignedElementOffsetInChars =
Ken Dyck7d2f9d52011-03-18 01:26:17 +0000312 ElementOffsetInChars.RoundUpToAlignment(ElementAlign);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000313
Ken Dyck4a5c87e2011-03-18 00:55:06 +0000314 if (AlignedElementOffsetInChars > ElementOffsetInChars) {
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000315 // We need some padding.
Ken Dyck4a5c87e2011-03-18 00:55:06 +0000316 CharUnits NumChars =
317 AlignedElementOffsetInChars - ElementOffsetInChars;
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000318
Chris Lattner2acc6e32011-07-18 04:24:23 +0000319 llvm::Type *Ty = llvm::Type::getInt8Ty(CGM.getLLVMContext());
Ken Dyck4a5c87e2011-03-18 00:55:06 +0000320 if (NumChars > CharUnits::One())
321 Ty = llvm::ArrayType::get(Ty, NumChars.getQuantity());
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000322
Nuno Lopescdb30b42010-04-16 20:56:35 +0000323 llvm::Constant *Padding = llvm::UndefValue::get(Ty);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000324 PackedElements.push_back(Padding);
Ken Dyck15c152e2011-03-18 01:12:13 +0000325 ElementOffsetInChars += getSizeInChars(Padding);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000326 }
327
328 PackedElements.push_back(C);
Ken Dyck15c152e2011-03-18 01:12:13 +0000329 ElementOffsetInChars += getSizeInChars(C);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000330 }
331
Ken Dyck4a5c87e2011-03-18 00:55:06 +0000332 assert(ElementOffsetInChars == NextFieldOffsetInChars &&
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000333 "Packing the struct changed its size!");
334
335 Elements = PackedElements;
Ken Dyck7d2f9d52011-03-18 01:26:17 +0000336 LLVMStructAlignment = CharUnits::One();
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000337 Packed = true;
338}
339
340bool ConstStructBuilder::Build(InitListExpr *ILE) {
341 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
342 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
343
344 unsigned FieldNo = 0;
345 unsigned ElementNo = 0;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +0000346 const FieldDecl *LastFD = 0;
347 bool IsMsStruct = RD->hasAttr<MsStructAttr>();
348
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000349 for (RecordDecl::field_iterator Field = RD->field_begin(),
Chris Lattner8ce9e452010-04-13 18:16:19 +0000350 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Fariborz Jahanian07a8a212011-04-28 22:49:46 +0000351 if (IsMsStruct) {
352 // Zero-length bitfields following non-bitfield members are
353 // ignored:
Fariborz Jahanian855a8e72011-05-03 20:21:04 +0000354 if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((*Field), LastFD)) {
Fariborz Jahanian07a8a212011-04-28 22:49:46 +0000355 --FieldNo;
356 continue;
357 }
358 LastFD = (*Field);
359 }
Chris Lattner8ce9e452010-04-13 18:16:19 +0000360
361 // If this is a union, skip all the fields that aren't being initialized.
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000362 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
363 continue;
364
Chris Lattner8ce9e452010-04-13 18:16:19 +0000365 // Don't emit anonymous bitfields, they just affect layout.
Eli Friedmandbff1a02011-08-23 20:22:34 +0000366 if (Field->isUnnamedBitfield()) {
Fariborz Jahanian07a8a212011-04-28 22:49:46 +0000367 LastFD = (*Field);
Chris Lattner8ce9e452010-04-13 18:16:19 +0000368 continue;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +0000369 }
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000370
Chris Lattner8ce9e452010-04-13 18:16:19 +0000371 // Get the initializer. A struct can include fields without initializers,
372 // we just use explicit null values for them.
373 llvm::Constant *EltInit;
374 if (ElementNo < ILE->getNumInits())
375 EltInit = CGM.EmitConstantExpr(ILE->getInit(ElementNo++),
376 Field->getType(), CGF);
377 else
378 EltInit = CGM.EmitNullConstant(Field->getType());
Eli Friedmanf0ca0ee2010-07-17 23:55:01 +0000379
380 if (!EltInit)
381 return false;
Chris Lattner8ce9e452010-04-13 18:16:19 +0000382
383 if (!Field->isBitField()) {
384 // Handle non-bitfield members.
385 if (!AppendField(*Field, Layout.getFieldOffset(FieldNo), EltInit))
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000386 return false;
387 } else {
Chris Lattner8ce9e452010-04-13 18:16:19 +0000388 // Otherwise we have a bitfield.
Chris Lattner18806662010-07-05 17:04:23 +0000389 AppendBitField(*Field, Layout.getFieldOffset(FieldNo),
390 cast<llvm::ConstantInt>(EltInit));
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000391 }
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000392 }
393
Ken Dyckdc496392011-03-17 01:33:18 +0000394 CharUnits LayoutSizeInChars = Layout.getSize();
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000395
Ken Dyckdc496392011-03-17 01:33:18 +0000396 if (NextFieldOffsetInChars > LayoutSizeInChars) {
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000397 // If the struct is bigger than the size of the record type,
398 // we must have a flexible array member at the end.
399 assert(RD->hasFlexibleArrayMember() &&
400 "Must have flexible array member if struct is bigger than type!");
401
402 // No tail padding is necessary.
403 return true;
404 }
405
Ken Dyckdc496392011-03-17 01:33:18 +0000406 CharUnits LLVMSizeInChars =
Ken Dyck7d2f9d52011-03-18 01:26:17 +0000407 NextFieldOffsetInChars.RoundUpToAlignment(LLVMStructAlignment);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000408
409 // Check if we need to convert the struct to a packed struct.
Ken Dyckdc496392011-03-17 01:33:18 +0000410 if (NextFieldOffsetInChars <= LayoutSizeInChars &&
411 LLVMSizeInChars > LayoutSizeInChars) {
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000412 assert(!Packed && "Size mismatch!");
413
414 ConvertStructToPacked();
Ken Dyckdc496392011-03-17 01:33:18 +0000415 assert(NextFieldOffsetInChars <= LayoutSizeInChars &&
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000416 "Converting to packed did not help!");
417 }
418
419 // Append tail padding if necessary.
Ken Dyckdc496392011-03-17 01:33:18 +0000420 AppendTailPadding(LayoutSizeInChars);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000421
Ken Dyckdc496392011-03-17 01:33:18 +0000422 assert(LayoutSizeInChars == NextFieldOffsetInChars &&
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000423 "Tail padding mismatch!");
424
425 return true;
426}
427
428llvm::Constant *ConstStructBuilder::
429 BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF, InitListExpr *ILE) {
430 ConstStructBuilder Builder(CGM, CGF);
431
432 if (!Builder.Build(ILE))
433 return 0;
434
Chris Lattnerc5cbb902011-06-20 04:01:35 +0000435 // Pick the type to use. If the type is layout identical to the ConvertType
436 // type then use it, otherwise use whatever the builder produced for us.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000437 llvm::StructType *STy =
Chris Lattnerc5cbb902011-06-20 04:01:35 +0000438 llvm::ConstantStruct::getTypeForElements(CGM.getLLVMContext(),
439 Builder.Elements,Builder.Packed);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000440 llvm::Type *ILETy = CGM.getTypes().ConvertType(ILE->getType());
441 if (llvm::StructType *ILESTy = dyn_cast<llvm::StructType>(ILETy)) {
Chris Lattnerc5cbb902011-06-20 04:01:35 +0000442 if (ILESTy->isLayoutIdentical(STy))
443 STy = ILESTy;
444 }
445
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000446 llvm::Constant *Result =
Chris Lattnerc5cbb902011-06-20 04:01:35 +0000447 llvm::ConstantStruct::get(STy, Builder.Elements);
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000448
Ken Dyck15c152e2011-03-18 01:12:13 +0000449 assert(Builder.NextFieldOffsetInChars.RoundUpToAlignment(
Ken Dyck7d2f9d52011-03-18 01:26:17 +0000450 Builder.getAlignment(Result)) ==
Ken Dyck15c152e2011-03-18 01:12:13 +0000451 Builder.getSizeInChars(Result) && "Size mismatch!");
Chris Lattnereba3e5a2010-04-13 17:45:57 +0000452
453 return Result;
454}
455
456
457//===----------------------------------------------------------------------===//
458// ConstExprEmitter
459//===----------------------------------------------------------------------===//
460
Benjamin Kramer85b45212009-11-28 19:45:26 +0000461class ConstExprEmitter :
Anders Carlsson84005b42008-01-26 04:30:23 +0000462 public StmtVisitor<ConstExprEmitter, llvm::Constant*> {
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000463 CodeGenModule &CGM;
Lauro Ramos Venancio81373352008-02-26 21:41:45 +0000464 CodeGenFunction *CGF;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000465 llvm::LLVMContext &VMContext;
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000466public:
Lauro Ramos Venancio81373352008-02-26 21:41:45 +0000467 ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000468 : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()) {
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000469 }
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000471 //===--------------------------------------------------------------------===//
472 // Visitor Methods
473 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000475 llvm::Constant *VisitStmt(Stmt *S) {
Anders Carlsson069880e2009-03-03 16:43:34 +0000476 return 0;
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000477 }
Mike Stump1eb44332009-09-09 15:08:12 +0000478
479 llvm::Constant *VisitParenExpr(ParenExpr *PE) {
480 return Visit(PE->getSubExpr());
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000481 }
Mike Stump1eb44332009-09-09 15:08:12 +0000482
John McCall91a57552011-07-15 05:09:51 +0000483 llvm::Constant *
484 VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) {
485 return Visit(PE->getReplacement());
486 }
487
Peter Collingbournef111d932011-04-15 00:35:48 +0000488 llvm::Constant *VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
489 return Visit(GE->getResultExpr());
490 }
491
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000492 llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
493 return Visit(E->getInitializer());
494 }
John McCall5808ce42011-02-03 08:15:49 +0000495
Anders Carlssonf57b4e42009-10-03 15:02:02 +0000496 llvm::Constant *VisitUnaryAddrOf(UnaryOperator *E) {
John McCall5808ce42011-02-03 08:15:49 +0000497 if (E->getType()->isMemberPointerType())
498 return CGM.getMemberPointerConstant(E);
Anders Carlsson45147d02010-02-02 03:37:46 +0000499
Anders Carlssonf57b4e42009-10-03 15:02:02 +0000500 return 0;
501 }
502
Chris Lattner3ae9f482009-10-13 07:14:16 +0000503 llvm::Constant *VisitBinSub(BinaryOperator *E) {
504 // This must be a pointer/pointer subtraction. This only happens for
505 // address of label.
506 if (!isa<AddrLabelExpr>(E->getLHS()->IgnoreParenNoopCasts(CGM.getContext())) ||
507 !isa<AddrLabelExpr>(E->getRHS()->IgnoreParenNoopCasts(CGM.getContext())))
508 return 0;
509
510 llvm::Constant *LHS = CGM.EmitConstantExpr(E->getLHS(),
511 E->getLHS()->getType(), CGF);
512 llvm::Constant *RHS = CGM.EmitConstantExpr(E->getRHS(),
513 E->getRHS()->getType(), CGF);
514
Chris Lattner2acc6e32011-07-18 04:24:23 +0000515 llvm::Type *ResultType = ConvertType(E->getType());
Chris Lattner3ae9f482009-10-13 07:14:16 +0000516 LHS = llvm::ConstantExpr::getPtrToInt(LHS, ResultType);
517 RHS = llvm::ConstantExpr::getPtrToInt(RHS, ResultType);
518
519 // No need to divide by element size, since addr of label is always void*,
520 // which has size 1 in GNUish.
521 return llvm::ConstantExpr::getSub(LHS, RHS);
522 }
523
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000524 llvm::Constant *VisitCastExpr(CastExpr* E) {
John McCall8bba1f42011-03-15 21:17:48 +0000525 Expr *subExpr = E->getSubExpr();
526 llvm::Constant *C = CGM.EmitConstantExpr(subExpr, subExpr->getType(), CGF);
527 if (!C) return 0;
528
Chris Lattner2acc6e32011-07-18 04:24:23 +0000529 llvm::Type *destType = ConvertType(E->getType());
John McCall8bba1f42011-03-15 21:17:48 +0000530
Anders Carlsson0086ccb2009-08-22 23:54:44 +0000531 switch (E->getCastKind()) {
John McCall2de56d12010-08-25 11:45:40 +0000532 case CK_ToUnion: {
Anders Carlsson0086ccb2009-08-22 23:54:44 +0000533 // GCC cast to union extension
534 assert(E->getType()->isUnionType() &&
535 "Destination type is not union type!");
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Anders Carlssonaaed50d2009-07-31 21:38:39 +0000537 // Build a struct with the union sub-element as the first member,
538 // and padded to the appropriate size
539 std::vector<llvm::Constant*> Elts;
Jay Foadef6de3d2011-07-11 09:56:20 +0000540 std::vector<llvm::Type*> Types;
Anders Carlssonaaed50d2009-07-31 21:38:39 +0000541 Elts.push_back(C);
542 Types.push_back(C->getType());
543 unsigned CurSize = CGM.getTargetData().getTypeAllocSize(C->getType());
John McCall8bba1f42011-03-15 21:17:48 +0000544 unsigned TotalSize = CGM.getTargetData().getTypeAllocSize(destType);
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Anders Carlssonaaed50d2009-07-31 21:38:39 +0000546 assert(CurSize <= TotalSize && "Union size mismatch!");
547 if (unsigned NumPadBytes = TotalSize - CurSize) {
Jay Foadef6de3d2011-07-11 09:56:20 +0000548 llvm::Type *Ty = llvm::Type::getInt8Ty(VMContext);
Anders Carlssonaaed50d2009-07-31 21:38:39 +0000549 if (NumPadBytes > 1)
550 Ty = llvm::ArrayType::get(Ty, NumPadBytes);
Anders Carlsson0086ccb2009-08-22 23:54:44 +0000551
Nuno Lopescdb30b42010-04-16 20:56:35 +0000552 Elts.push_back(llvm::UndefValue::get(Ty));
Anders Carlssonaaed50d2009-07-31 21:38:39 +0000553 Types.push_back(Ty);
554 }
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Anders Carlsson0086ccb2009-08-22 23:54:44 +0000556 llvm::StructType* STy =
557 llvm::StructType::get(C->getType()->getContext(), Types, false);
Anders Carlssonaaed50d2009-07-31 21:38:39 +0000558 return llvm::ConstantStruct::get(STy, Elts);
Nuno Lopes81e51e22009-01-17 00:48:48 +0000559 }
John McCall2de56d12010-08-25 11:45:40 +0000560 case CK_NullToMemberPointer: {
John McCalld608cdb2010-08-22 10:59:02 +0000561 const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>();
John McCall0bab0cd2010-08-23 01:21:21 +0000562 return CGM.getCXXABI().EmitNullMemberPointer(MPT);
John McCalld608cdb2010-08-22 10:59:02 +0000563 }
Anders Carlsson2c51f092009-10-03 15:13:22 +0000564
John McCall8bba1f42011-03-15 21:17:48 +0000565 case CK_DerivedToBaseMemberPointer:
566 case CK_BaseToDerivedMemberPointer:
John McCall0bab0cd2010-08-23 01:21:21 +0000567 return CGM.getCXXABI().EmitMemberPointerConversion(C, E);
Anders Carlssonbb378cb2009-10-18 20:31:03 +0000568
John McCall8bba1f42011-03-15 21:17:48 +0000569 case CK_LValueToRValue:
570 case CK_NoOp:
571 return C;
Anders Carlssonbb378cb2009-10-18 20:31:03 +0000572
John McCall1d9b3b22011-09-09 05:25:32 +0000573 case CK_CPointerToObjCPointerCast:
574 case CK_BlockPointerToObjCPointerCast:
John McCall8bba1f42011-03-15 21:17:48 +0000575 case CK_AnyPointerToBlockPointerCast:
John McCall8bba1f42011-03-15 21:17:48 +0000576 case CK_BitCast:
577 if (C->getType() == destType) return C;
578 return llvm::ConstantExpr::getBitCast(C, destType);
Chris Lattner430656e2009-10-13 22:12:09 +0000579
John McCall8bba1f42011-03-15 21:17:48 +0000580 case CK_Dependent: llvm_unreachable("saw dependent cast!");
581
582 // These will never be supported.
583 case CK_ObjCObjectLValueCast:
584 case CK_GetObjCProperty:
585 case CK_ToVoid:
586 case CK_Dynamic:
John McCall33e56f32011-09-10 06:18:15 +0000587 case CK_ARCProduceObject:
588 case CK_ARCConsumeObject:
589 case CK_ARCReclaimReturnedObject:
590 case CK_ARCExtendBlockObject:
Eli Friedmanb0004592011-10-24 22:25:55 +0000591 case CK_LValueBitCast:
Anders Carlsson0086ccb2009-08-22 23:54:44 +0000592 return 0;
John McCall8bba1f42011-03-15 21:17:48 +0000593
594 // These might need to be supported for constexpr.
595 case CK_UserDefinedConversion:
596 case CK_ConstructorConversion:
597 return 0;
598
599 // These should eventually be supported.
600 case CK_ArrayToPointerDecay:
601 case CK_FunctionToPointerDecay:
602 case CK_BaseToDerived:
603 case CK_DerivedToBase:
604 case CK_UncheckedDerivedToBase:
605 case CK_MemberPointerToBoolean:
606 case CK_VectorSplat:
607 case CK_FloatingRealToComplex:
608 case CK_FloatingComplexToReal:
609 case CK_FloatingComplexToBoolean:
610 case CK_FloatingComplexCast:
611 case CK_FloatingComplexToIntegralComplex:
612 case CK_IntegralRealToComplex:
613 case CK_IntegralComplexToReal:
614 case CK_IntegralComplexToBoolean:
615 case CK_IntegralComplexCast:
616 case CK_IntegralComplexToFloatingComplex:
617 return 0;
618
619 case CK_PointerToIntegral:
620 if (!E->getType()->isBooleanType())
621 return llvm::ConstantExpr::getPtrToInt(C, destType);
622 // fallthrough
623
624 case CK_PointerToBoolean:
625 return llvm::ConstantExpr::getICmp(llvm::CmpInst::ICMP_EQ, C,
626 llvm::ConstantPointerNull::get(cast<llvm::PointerType>(C->getType())));
627
628 case CK_NullToPointer:
629 return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(destType));
630
631 case CK_IntegralCast: {
Douglas Gregor575a1c92011-05-20 16:38:50 +0000632 bool isSigned = subExpr->getType()->isSignedIntegerOrEnumerationType();
John McCall8bba1f42011-03-15 21:17:48 +0000633 return llvm::ConstantExpr::getIntegerCast(C, destType, isSigned);
Eli Friedmanddd2b6e2009-02-22 07:29:04 +0000634 }
John McCall8bba1f42011-03-15 21:17:48 +0000635
636 case CK_IntegralToPointer: {
Douglas Gregor575a1c92011-05-20 16:38:50 +0000637 bool isSigned = subExpr->getType()->isSignedIntegerOrEnumerationType();
John McCall8bba1f42011-03-15 21:17:48 +0000638 C = llvm::ConstantExpr::getIntegerCast(C, CGM.IntPtrTy, isSigned);
639 return llvm::ConstantExpr::getIntToPtr(C, destType);
640 }
641
642 case CK_IntegralToBoolean:
643 return llvm::ConstantExpr::getICmp(llvm::CmpInst::ICMP_EQ, C,
644 llvm::Constant::getNullValue(C->getType()));
645
646 case CK_IntegralToFloating:
Douglas Gregor575a1c92011-05-20 16:38:50 +0000647 if (subExpr->getType()->isSignedIntegerOrEnumerationType())
John McCall8bba1f42011-03-15 21:17:48 +0000648 return llvm::ConstantExpr::getSIToFP(C, destType);
649 else
650 return llvm::ConstantExpr::getUIToFP(C, destType);
651
652 case CK_FloatingToIntegral:
Douglas Gregor575a1c92011-05-20 16:38:50 +0000653 if (E->getType()->isSignedIntegerOrEnumerationType())
John McCall8bba1f42011-03-15 21:17:48 +0000654 return llvm::ConstantExpr::getFPToSI(C, destType);
655 else
656 return llvm::ConstantExpr::getFPToUI(C, destType);
657
658 case CK_FloatingToBoolean:
659 return llvm::ConstantExpr::getFCmp(llvm::CmpInst::FCMP_UNE, C,
660 llvm::Constant::getNullValue(C->getType()));
661
662 case CK_FloatingCast:
663 return llvm::ConstantExpr::getFPCast(C, destType);
Anders Carlsson0086ccb2009-08-22 23:54:44 +0000664 }
Matt Beaumont-Gay17d12fe2011-03-17 00:46:34 +0000665 llvm_unreachable("Invalid CastKind");
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000666 }
Devang Pateleae15602008-02-05 02:39:50 +0000667
Chris Lattner04421082008-04-08 04:40:51 +0000668 llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
669 return Visit(DAE->getExpr());
670 }
671
Douglas Gregor03e80032011-06-21 17:03:29 +0000672 llvm::Constant *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
673 return Visit(E->GetTemporaryExpr());
674 }
675
Eli Friedmana8234002008-05-30 10:24:46 +0000676 llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) {
Nuno Lopesa75b71f2010-04-18 19:06:43 +0000677 unsigned NumInitElements = ILE->getNumInits();
Benjamin Kramer0483a6f2011-03-02 21:27:44 +0000678 if (NumInitElements == 1 && ILE->getType() == ILE->getInit(0)->getType() &&
Nuno Lopesa75b71f2010-04-18 19:06:43 +0000679 (isa<StringLiteral>(ILE->getInit(0)) ||
680 isa<ObjCEncodeExpr>(ILE->getInit(0))))
681 return Visit(ILE->getInit(0));
682
Nuno Lopes67847042010-04-16 23:19:41 +0000683 std::vector<llvm::Constant*> Elts;
Chris Lattner2acc6e32011-07-18 04:24:23 +0000684 llvm::ArrayType *AType =
Nuno Lopes67847042010-04-16 23:19:41 +0000685 cast<llvm::ArrayType>(ConvertType(ILE->getType()));
Chris Lattner2acc6e32011-07-18 04:24:23 +0000686 llvm::Type *ElemTy = AType->getElementType();
Devang Pateleae15602008-02-05 02:39:50 +0000687 unsigned NumElements = AType->getNumElements();
688
Mike Stump1eb44332009-09-09 15:08:12 +0000689 // Initialising an array requires us to automatically
Devang Pateleae15602008-02-05 02:39:50 +0000690 // initialise any elements that have not been initialised explicitly
691 unsigned NumInitableElts = std::min(NumInitElements, NumElements);
692
693 // Copy initializer elements.
694 unsigned i = 0;
Eli Friedmana04a1532008-05-30 19:58:50 +0000695 bool RewriteType = false;
Devang Pateleae15602008-02-05 02:39:50 +0000696 for (; i < NumInitableElts; ++i) {
Anders Carlssone9352cc2009-04-08 04:48:15 +0000697 Expr *Init = ILE->getInit(i);
698 llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF);
Daniel Dunbard60f2fb2009-02-17 18:43:32 +0000699 if (!C)
700 return 0;
Eli Friedmana04a1532008-05-30 19:58:50 +0000701 RewriteType |= (C->getType() != ElemTy);
Devang Pateleae15602008-02-05 02:39:50 +0000702 Elts.push_back(C);
703 }
Eli Friedmana04a1532008-05-30 19:58:50 +0000704
Devang Pateleae15602008-02-05 02:39:50 +0000705 // Initialize remaining array elements.
Eli Friedman0f593122009-04-13 21:47:26 +0000706 // FIXME: This doesn't handle member pointers correctly!
Argyrios Kyrtzidis4423ac02011-04-21 00:27:41 +0000707 llvm::Constant *fillC;
708 if (Expr *filler = ILE->getArrayFiller())
709 fillC = CGM.EmitConstantExpr(filler, filler->getType(), CGF);
710 else
711 fillC = llvm::Constant::getNullValue(ElemTy);
712 if (!fillC)
713 return 0;
714 RewriteType |= (fillC->getType() != ElemTy);
Devang Pateleae15602008-02-05 02:39:50 +0000715 for (; i < NumElements; ++i)
Argyrios Kyrtzidis4423ac02011-04-21 00:27:41 +0000716 Elts.push_back(fillC);
Devang Pateleae15602008-02-05 02:39:50 +0000717
Eli Friedmana04a1532008-05-30 19:58:50 +0000718 if (RewriteType) {
719 // FIXME: Try to avoid packing the array
Jay Foadef6de3d2011-07-11 09:56:20 +0000720 std::vector<llvm::Type*> Types;
Eli Friedmana04a1532008-05-30 19:58:50 +0000721 for (unsigned i = 0; i < Elts.size(); ++i)
722 Types.push_back(Elts[i]->getType());
Chris Lattner2acc6e32011-07-18 04:24:23 +0000723 llvm::StructType *SType = llvm::StructType::get(AType->getContext(),
Owen Anderson47a434f2009-08-05 23:18:46 +0000724 Types, true);
Owen Anderson08e25242009-07-27 22:29:56 +0000725 return llvm::ConstantStruct::get(SType, Elts);
Eli Friedmana04a1532008-05-30 19:58:50 +0000726 }
727
Mike Stump1eb44332009-09-09 15:08:12 +0000728 return llvm::ConstantArray::get(AType, Elts);
Devang Pateleae15602008-02-05 02:39:50 +0000729 }
730
Eli Friedmana8234002008-05-30 10:24:46 +0000731 llvm::Constant *EmitStructInitialization(InitListExpr *ILE) {
Anders Carlsson8dca3b32009-07-27 05:54:15 +0000732 return ConstStructBuilder::BuildStruct(CGM, CGF, ILE);
Devang Pateleae15602008-02-05 02:39:50 +0000733 }
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Eli Friedmana8234002008-05-30 10:24:46 +0000735 llvm::Constant *EmitUnionInitialization(InitListExpr *ILE) {
Anders Carlssonf6c43962009-07-31 21:34:04 +0000736 return ConstStructBuilder::BuildStruct(CGM, CGF, ILE);
Eli Friedmana8234002008-05-30 10:24:46 +0000737 }
738
Anders Carlsson7d801ea2009-01-30 06:13:25 +0000739 llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E) {
Eli Friedman0f593122009-04-13 21:47:26 +0000740 return CGM.EmitNullConstant(E->getType());
Anders Carlsson7d801ea2009-01-30 06:13:25 +0000741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000743 llvm::Constant *VisitInitListExpr(InitListExpr *ILE) {
Eli Friedman0c706c22011-09-19 23:17:44 +0000744 if (ILE->getType()->isAnyComplexType() && ILE->getNumInits() == 2) {
745 // Complex type with element initializers
746 Expr *Real = ILE->getInit(0);
747 Expr *Imag = ILE->getInit(1);
748 llvm::Constant *Complex[2];
749 Complex[0] = CGM.EmitConstantExpr(Real, Real->getType(), CGF);
750 if (!Complex[0])
751 return 0;
752 Complex[1] = CGM.EmitConstantExpr(Imag, Imag->getType(), CGF);
753 if (!Complex[1])
754 return 0;
755 llvm::StructType *STy =
756 cast<llvm::StructType>(ConvertType(ILE->getType()));
757 return llvm::ConstantStruct::get(STy, Complex);
758 }
759
Eli Friedmana8234002008-05-30 10:24:46 +0000760 if (ILE->getType()->isScalarType()) {
761 // We have a scalar in braces. Just use the first element.
Anders Carlssone9352cc2009-04-08 04:48:15 +0000762 if (ILE->getNumInits() > 0) {
763 Expr *Init = ILE->getInit(0);
764 return CGM.EmitConstantExpr(Init, Init->getType(), CGF);
765 }
Eli Friedman0f593122009-04-13 21:47:26 +0000766 return CGM.EmitNullConstant(ILE->getType());
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000767 }
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Eli Friedmana8234002008-05-30 10:24:46 +0000769 if (ILE->getType()->isArrayType())
770 return EmitArrayInitialization(ILE);
Devang Patel47fb6972008-01-29 23:23:18 +0000771
Anders Carlsson01a79ac2009-11-21 23:56:04 +0000772 if (ILE->getType()->isRecordType())
Eli Friedmana8234002008-05-30 10:24:46 +0000773 return EmitStructInitialization(ILE);
774
775 if (ILE->getType()->isUnionType())
776 return EmitUnionInitialization(ILE);
777
Eli Friedman8ac55492010-01-02 23:43:59 +0000778 // If ILE was a constant vector, we would have handled it already.
Eli Friedmana8234002008-05-30 10:24:46 +0000779 if (ILE->getType()->isVectorType())
Eli Friedman8ac55492010-01-02 23:43:59 +0000780 return 0;
Eli Friedmana8234002008-05-30 10:24:46 +0000781
David Blaikieb219cfc2011-09-23 05:06:16 +0000782 llvm_unreachable("Unable to handle InitListExpr");
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000783 }
Eli Friedman3b707e72008-02-21 17:57:49 +0000784
John McCallfb8b69a2010-02-02 08:02:49 +0000785 llvm::Constant *VisitCXXConstructExpr(CXXConstructExpr *E) {
786 if (!E->getConstructor()->isTrivial())
787 return 0;
788
Anders Carlsson2ca4f632010-02-05 18:38:45 +0000789 QualType Ty = E->getType();
790
791 // FIXME: We should not have to call getBaseElementType here.
792 const RecordType *RT =
793 CGM.getContext().getBaseElementType(Ty)->getAs<RecordType>();
794 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
795
796 // If the class doesn't have a trivial destructor, we can't emit it as a
797 // constant expr.
798 if (!RD->hasTrivialDestructor())
799 return 0;
800
John McCallfb8b69a2010-02-02 08:02:49 +0000801 // Only copy and default constructors can be trivial.
802
John McCallfb8b69a2010-02-02 08:02:49 +0000803
804 if (E->getNumArgs()) {
805 assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument");
Sebastian Redl85ea7aa2011-08-30 19:58:05 +0000806 assert(E->getConstructor()->isCopyOrMoveConstructor() &&
807 "trivial ctor has argument but isn't a copy/move ctor");
John McCallfb8b69a2010-02-02 08:02:49 +0000808
809 Expr *Arg = E->getArg(0);
810 assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) &&
811 "argument to copy ctor is of wrong type");
812
Chandler Carruth878b55c2010-02-02 12:15:55 +0000813 return Visit(Arg);
John McCallfb8b69a2010-02-02 08:02:49 +0000814 }
815
816 return CGM.EmitNullConstant(Ty);
817 }
818
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000819 llvm::Constant *VisitStringLiteral(StringLiteral *E) {
Chris Lattnera7ad98f2008-02-11 00:02:17 +0000820 assert(!E->getType()->isPointerType() && "Strings are always arrays");
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000822 // This must be a string initializing an array in a static initializer.
823 // Don't emit it as the address of the string, emit the string data itself
824 // as an inline array.
Owen Anderson0032b272009-08-13 21:57:51 +0000825 return llvm::ConstantArray::get(VMContext,
826 CGM.GetStringForStringLiteral(E), false);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000827 }
828
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000829 llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
830 // This must be an @encode initializing an array in a static initializer.
831 // Don't emit it as the address of the string, emit the string data itself
832 // as an inline array.
833 std::string Str;
834 CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str);
835 const ConstantArrayType *CAT = cast<ConstantArrayType>(E->getType());
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000837 // Resize the string to the right size, adding zeros at the end, or
838 // truncating as needed.
839 Str.resize(CAT->getSize().getZExtValue(), '\0');
Owen Anderson0032b272009-08-13 21:57:51 +0000840 return llvm::ConstantArray::get(VMContext, Str, false);
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000841 }
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Eli Friedmanbc5ed6e2008-05-29 11:22:45 +0000843 llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) {
844 return Visit(E->getSubExpr());
845 }
Mike Stumpb83d2872009-02-19 22:01:56 +0000846
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000847 // Utility methods
Chris Lattner2acc6e32011-07-18 04:24:23 +0000848 llvm::Type *ConvertType(QualType T) {
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000849 return CGM.getTypes().ConvertType(T);
850 }
Anders Carlsson84005b42008-01-26 04:30:23 +0000851
Anders Carlsson5ea7d072008-11-16 06:23:45 +0000852public:
Anders Carlsson84005b42008-01-26 04:30:23 +0000853 llvm::Constant *EmitLValue(Expr *E) {
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000854 switch (E->getStmtClass()) {
Eli Friedman546d94c2008-02-11 00:23:10 +0000855 default: break;
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000856 case Expr::CompoundLiteralExprClass: {
857 // Note that due to the nature of compound literals, this is guaranteed
858 // to be the only use of the variable, so we just generate it here.
Anders Carlsson84005b42008-01-26 04:30:23 +0000859 CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E);
860 llvm::Constant* C = Visit(CLE->getInitializer());
Daniel Dunbard60f2fb2009-02-17 18:43:32 +0000861 // FIXME: "Leaked" on failure.
862 if (C)
Owen Anderson1c431b32009-07-08 19:05:04 +0000863 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(),
Eli Friedmand15d8c62009-08-26 20:01:39 +0000864 E->getType().isConstant(CGM.getContext()),
Daniel Dunbard60f2fb2009-02-17 18:43:32 +0000865 llvm::GlobalValue::InternalLinkage,
Eli Friedmand15d8c62009-08-26 20:01:39 +0000866 C, ".compoundliteral", 0, false,
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000867 CGM.getContext().getTargetAddressSpace(E->getType()));
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000868 return C;
Anders Carlsson84005b42008-01-26 04:30:23 +0000869 }
Douglas Gregora2813ce2009-10-23 18:54:35 +0000870 case Expr::DeclRefExprClass: {
Rafael Espindolada182612010-03-04 21:26:03 +0000871 ValueDecl *Decl = cast<DeclRefExpr>(E)->getDecl();
872 if (Decl->hasAttr<WeakRefAttr>())
Eli Friedmana7e68452010-08-22 01:00:03 +0000873 return CGM.GetWeakRefReference(Decl);
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000874 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl))
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000875 return CGM.GetAddrOfFunction(FD);
Steve Naroff248a7532008-04-15 22:42:06 +0000876 if (const VarDecl* VD = dyn_cast<VarDecl>(Decl)) {
Daniel Dunbard01b6692009-02-24 18:41:57 +0000877 // We can never refer to a variable with local storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000878 if (!VD->hasLocalStorage()) {
Daniel Dunbard01b6692009-02-24 18:41:57 +0000879 if (VD->isFileVarDecl() || VD->hasExternalStorage())
880 return CGM.GetAddrOfGlobalVar(VD);
John McCallb6bbcc92010-10-15 04:57:14 +0000881 else if (VD->isLocalVarDecl()) {
Daniel Dunbard01b6692009-02-24 18:41:57 +0000882 assert(CGF && "Can't access static local vars without CGF");
883 return CGF->GetAddrOfStaticLocalVar(VD);
884 }
Steve Naroff248a7532008-04-15 22:42:06 +0000885 }
Lauro Ramos Venancio81373352008-02-26 21:41:45 +0000886 }
Eli Friedman546d94c2008-02-11 00:23:10 +0000887 break;
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000888 }
Daniel Dunbar61432932008-08-13 23:20:05 +0000889 case Expr::StringLiteralClass:
890 return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E));
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000891 case Expr::ObjCEncodeExprClass:
892 return CGM.GetAddrOfConstantStringFromObjCEncode(cast<ObjCEncodeExpr>(E));
Anders Carlsson5ea7d072008-11-16 06:23:45 +0000893 case Expr::ObjCStringLiteralClass: {
894 ObjCStringLiteral* SL = cast<ObjCStringLiteral>(E);
David Chisnall0d13f6f2010-01-23 02:40:42 +0000895 llvm::Constant *C =
896 CGM.getObjCRuntime().GenerateConstantString(SL->getString());
Owen Anderson3c4972d2009-07-29 18:54:39 +0000897 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Anders Carlsson5ea7d072008-11-16 06:23:45 +0000898 }
Chris Lattner35345642008-12-12 05:18:02 +0000899 case Expr::PredefinedExprClass: {
Eli Friedmanf70b24e2009-11-14 08:37:13 +0000900 unsigned Type = cast<PredefinedExpr>(E)->getIdentType();
901 if (CGF) {
Daniel Dunbar3ec0baf2010-08-21 03:01:12 +0000902 LValue Res = CGF->EmitPredefinedLValue(cast<PredefinedExpr>(E));
Eli Friedmanf70b24e2009-11-14 08:37:13 +0000903 return cast<llvm::Constant>(Res.getAddress());
904 } else if (Type == PredefinedExpr::PrettyFunction) {
905 return CGM.GetAddrOfConstantCString("top level", ".tmp");
906 }
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Eli Friedmanf70b24e2009-11-14 08:37:13 +0000908 return CGM.GetAddrOfConstantCString("", ".tmp");
Chris Lattner35345642008-12-12 05:18:02 +0000909 }
Eli Friedmanf0115892009-01-25 01:21:06 +0000910 case Expr::AddrLabelExprClass: {
911 assert(CGF && "Invalid address of label expression outside function.");
Chris Lattnerd9becd12009-10-28 23:59:40 +0000912 llvm::Constant *Ptr =
913 CGF->GetAddrOfLabel(cast<AddrLabelExpr>(E)->getLabel());
914 return llvm::ConstantExpr::getBitCast(Ptr, ConvertType(E->getType()));
Eli Friedmanf0115892009-01-25 01:21:06 +0000915 }
Eli Friedman3941b182009-01-25 01:54:01 +0000916 case Expr::CallExprClass: {
917 CallExpr* CE = cast<CallExpr>(E);
David Chisnall0d13f6f2010-01-23 02:40:42 +0000918 unsigned builtin = CE->isBuiltinCall(CGM.getContext());
919 if (builtin !=
920 Builtin::BI__builtin___CFStringMakeConstantString &&
921 builtin !=
922 Builtin::BI__builtin___NSStringMakeConstantString)
Eli Friedman3941b182009-01-25 01:54:01 +0000923 break;
924 const Expr *Arg = CE->getArg(0)->IgnoreParenCasts();
925 const StringLiteral *Literal = cast<StringLiteral>(Arg);
David Chisnall0d13f6f2010-01-23 02:40:42 +0000926 if (builtin ==
927 Builtin::BI__builtin___NSStringMakeConstantString) {
928 return CGM.getObjCRuntime().GenerateConstantString(Literal);
929 }
Steve Naroff33fdb732009-03-31 16:53:37 +0000930 // FIXME: need to deal with UCN conversion issues.
Steve Naroff8d4141f2009-04-01 13:55:36 +0000931 return CGM.GetAddrOfConstantCFString(Literal);
Eli Friedman3941b182009-01-25 01:54:01 +0000932 }
Mike Stumpbd65cac2009-02-19 01:01:04 +0000933 case Expr::BlockExprClass: {
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000934 std::string FunctionName;
935 if (CGF)
936 FunctionName = CGF->CurFn->getName();
937 else
938 FunctionName = "global";
939
940 return CGM.GetAddrOfGlobalBlock(cast<BlockExpr>(E), FunctionName.c_str());
Mike Stumpbd65cac2009-02-19 01:01:04 +0000941 }
Eli Friedman546d94c2008-02-11 00:23:10 +0000942 }
Daniel Dunbard60f2fb2009-02-17 18:43:32 +0000943
944 return 0;
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000945 }
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000946};
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000948} // end anonymous namespace.
949
Lauro Ramos Venancio81373352008-02-26 21:41:45 +0000950llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E,
Anders Carlssone9352cc2009-04-08 04:48:15 +0000951 QualType DestType,
Chris Lattner96196622008-07-26 22:37:01 +0000952 CodeGenFunction *CGF) {
Anders Carlsson4b3f9c02008-12-01 02:42:14 +0000953 Expr::EvalResult Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Anders Carlsson1e5dc6e2009-04-11 01:08:03 +0000955 bool Success = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Eli Friedman50c39ea2009-05-27 06:04:58 +0000957 if (DestType->isReferenceType())
958 Success = E->EvaluateAsLValue(Result, Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000959 else
Richard Smith51f47082011-10-29 00:50:52 +0000960 Success = E->EvaluateAsRValue(Result, Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Eli Friedman55f9bdd2009-11-14 08:51:33 +0000962 if (Success && !Result.HasSideEffects) {
Anders Carlsson4b3f9c02008-12-01 02:42:14 +0000963 switch (Result.Val.getKind()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +0000964 case APValue::Uninitialized:
David Blaikieb219cfc2011-09-23 05:06:16 +0000965 llvm_unreachable("Constant expressions should be initialized.");
Anders Carlsson5a9a4572008-11-15 20:45:50 +0000966 case APValue::LValue: {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000967 llvm::Type *DestTy = getTypes().ConvertTypeForMem(DestType);
Mike Stump1eb44332009-09-09 15:08:12 +0000968 llvm::Constant *Offset =
969 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dycka7305832010-01-15 12:37:54 +0000970 Result.Val.getLValueOffset().getQuantity());
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Daniel Dunbare20de512009-02-19 21:44:24 +0000972 llvm::Constant *C;
Anders Carlsson4b3f9c02008-12-01 02:42:14 +0000973 if (const Expr *LVBase = Result.Val.getLValueBase()) {
Daniel Dunbare20de512009-02-19 21:44:24 +0000974 C = ConstExprEmitter(*this, CGF).EmitLValue(const_cast<Expr*>(LVBase));
Anders Carlsson5ea7d072008-11-16 06:23:45 +0000975
Daniel Dunbare20de512009-02-19 21:44:24 +0000976 // Apply offset if necessary.
977 if (!Offset->isNullValue()) {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000978 llvm::Type *Type = llvm::Type::getInt8PtrTy(VMContext);
Owen Anderson3c4972d2009-07-29 18:54:39 +0000979 llvm::Constant *Casted = llvm::ConstantExpr::getBitCast(C, Type);
Jay Foada5c04342011-07-21 14:31:17 +0000980 Casted = llvm::ConstantExpr::getGetElementPtr(Casted, Offset);
Owen Anderson3c4972d2009-07-29 18:54:39 +0000981 C = llvm::ConstantExpr::getBitCast(Casted, C->getType());
Daniel Dunbare20de512009-02-19 21:44:24 +0000982 }
983
984 // Convert to the appropriate type; this could be an lvalue for
985 // an integer.
Anders Carlsson12e330c2009-04-10 04:59:59 +0000986 if (isa<llvm::PointerType>(DestTy))
Owen Anderson3c4972d2009-07-29 18:54:39 +0000987 return llvm::ConstantExpr::getBitCast(C, DestTy);
Daniel Dunbare20de512009-02-19 21:44:24 +0000988
Owen Anderson3c4972d2009-07-29 18:54:39 +0000989 return llvm::ConstantExpr::getPtrToInt(C, DestTy);
Daniel Dunbare20de512009-02-19 21:44:24 +0000990 } else {
991 C = Offset;
992
993 // Convert to the appropriate type; this could be an lvalue for
994 // an integer.
Anders Carlsson12e330c2009-04-10 04:59:59 +0000995 if (isa<llvm::PointerType>(DestTy))
Owen Anderson3c4972d2009-07-29 18:54:39 +0000996 return llvm::ConstantExpr::getIntToPtr(C, DestTy);
Daniel Dunbare20de512009-02-19 21:44:24 +0000997
998 // If the types don't match this should only be a truncate.
Anders Carlsson12e330c2009-04-10 04:59:59 +0000999 if (C->getType() != DestTy)
Owen Anderson3c4972d2009-07-29 18:54:39 +00001000 return llvm::ConstantExpr::getTrunc(C, DestTy);
Daniel Dunbare20de512009-02-19 21:44:24 +00001001
1002 return C;
Anders Carlsson5ea7d072008-11-16 06:23:45 +00001003 }
Anders Carlsson5a9a4572008-11-15 20:45:50 +00001004 }
Eli Friedman7b30b1c2008-11-17 03:57:28 +00001005 case APValue::Int: {
Mike Stump1eb44332009-09-09 15:08:12 +00001006 llvm::Constant *C = llvm::ConstantInt::get(VMContext,
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001007 Result.Val.getInt());
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Chris Lattner7f215c12010-06-26 21:52:32 +00001009 if (C->getType()->isIntegerTy(1)) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00001010 llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
Owen Anderson3c4972d2009-07-29 18:54:39 +00001011 C = llvm::ConstantExpr::getZExt(C, BoolTy);
Anders Carlsson5a9a4572008-11-15 20:45:50 +00001012 }
1013 return C;
Eli Friedman7b30b1c2008-11-17 03:57:28 +00001014 }
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001015 case APValue::ComplexInt: {
1016 llvm::Constant *Complex[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001018 Complex[0] = llvm::ConstantInt::get(VMContext,
1019 Result.Val.getComplexIntReal());
Mike Stump1eb44332009-09-09 15:08:12 +00001020 Complex[1] = llvm::ConstantInt::get(VMContext,
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001021 Result.Val.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001023 // FIXME: the target may want to specify that this is packed.
Chris Lattnerc5cbb902011-06-20 04:01:35 +00001024 llvm::StructType *STy = llvm::StructType::get(Complex[0]->getType(),
1025 Complex[1]->getType(),
1026 NULL);
1027 return llvm::ConstantStruct::get(STy, Complex);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001028 }
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001029 case APValue::Float: {
1030 const llvm::APFloat &Init = Result.Val.getFloat();
1031 if (&Init.getSemantics() == &llvm::APFloat::IEEEhalf)
1032 return llvm::ConstantInt::get(VMContext, Init.bitcastToAPInt());
1033 else
1034 return llvm::ConstantFP::get(VMContext, Init);
1035 }
Anders Carlssonaa432562008-11-17 01:58:55 +00001036 case APValue::ComplexFloat: {
1037 llvm::Constant *Complex[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001038
1039 Complex[0] = llvm::ConstantFP::get(VMContext,
Owen Andersonbc0a2222009-07-27 21:00:51 +00001040 Result.Val.getComplexFloatReal());
1041 Complex[1] = llvm::ConstantFP::get(VMContext,
1042 Result.Val.getComplexFloatImag());
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001044 // FIXME: the target may want to specify that this is packed.
Chris Lattnerc5cbb902011-06-20 04:01:35 +00001045 llvm::StructType *STy = llvm::StructType::get(Complex[0]->getType(),
1046 Complex[1]->getType(),
1047 NULL);
1048 return llvm::ConstantStruct::get(STy, Complex);
Anders Carlssonaa432562008-11-17 01:58:55 +00001049 }
Nate Begeman3d309f92009-01-18 01:01:34 +00001050 case APValue::Vector: {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001051 SmallVector<llvm::Constant *, 4> Inits;
Nate Begeman3d309f92009-01-18 01:01:34 +00001052 unsigned NumElts = Result.Val.getVectorLength();
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Anton Yartsevd06fea82011-03-27 09:32:40 +00001054 if (Context.getLangOptions().AltiVec &&
1055 isa<CastExpr>(E) &&
1056 cast<CastExpr>(E)->getCastKind() == CK_VectorSplat) {
1057 // AltiVec vector initialization with a single literal
1058 APValue &Elt = Result.Val.getVectorElt(0);
1059
1060 llvm::Constant* InitValue = Elt.isInt()
1061 ? cast<llvm::Constant>
1062 (llvm::ConstantInt::get(VMContext, Elt.getInt()))
1063 : cast<llvm::Constant>
1064 (llvm::ConstantFP::get(VMContext, Elt.getFloat()));
1065
1066 for (unsigned i = 0; i != NumElts; ++i)
1067 Inits.push_back(InitValue);
1068
1069 } else {
1070 for (unsigned i = 0; i != NumElts; ++i) {
1071 APValue &Elt = Result.Val.getVectorElt(i);
1072 if (Elt.isInt())
1073 Inits.push_back(llvm::ConstantInt::get(VMContext, Elt.getInt()));
1074 else
1075 Inits.push_back(llvm::ConstantFP::get(VMContext, Elt.getFloat()));
1076 }
Nate Begeman3d309f92009-01-18 01:01:34 +00001077 }
Chris Lattnerfb018d12011-02-15 00:14:06 +00001078 return llvm::ConstantVector::get(Inits);
Nate Begeman3d309f92009-01-18 01:01:34 +00001079 }
Anders Carlsson5a9a4572008-11-15 20:45:50 +00001080 }
1081 }
Eli Friedman7dfa6392008-06-01 15:31:44 +00001082
1083 llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E));
Chris Lattner7f215c12010-06-26 21:52:32 +00001084 if (C && C->getType()->isIntegerTy(1)) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00001085 llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
Owen Anderson3c4972d2009-07-29 18:54:39 +00001086 C = llvm::ConstantExpr::getZExt(C, BoolTy);
Eli Friedman7dfa6392008-06-01 15:31:44 +00001087 }
1088 return C;
Anders Carlsson3b1d57b2008-01-26 01:36:00 +00001089}
Eli Friedman0f593122009-04-13 21:47:26 +00001090
John McCall5808ce42011-02-03 08:15:49 +00001091static uint64_t getFieldOffset(ASTContext &C, const FieldDecl *field) {
1092 const ASTRecordLayout &layout = C.getASTRecordLayout(field->getParent());
1093 return layout.getFieldOffset(field->getFieldIndex());
1094}
1095
1096llvm::Constant *
1097CodeGenModule::getMemberPointerConstant(const UnaryOperator *uo) {
1098 // Member pointer constants always have a very particular form.
1099 const MemberPointerType *type = cast<MemberPointerType>(uo->getType());
1100 const ValueDecl *decl = cast<DeclRefExpr>(uo->getSubExpr())->getDecl();
1101
1102 // A member function pointer.
1103 if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(decl))
1104 return getCXXABI().EmitMemberPointer(method);
1105
1106 // Otherwise, a member data pointer.
1107 uint64_t fieldOffset;
1108 if (const FieldDecl *field = dyn_cast<FieldDecl>(decl))
1109 fieldOffset = getFieldOffset(getContext(), field);
1110 else {
1111 const IndirectFieldDecl *ifield = cast<IndirectFieldDecl>(decl);
1112
1113 fieldOffset = 0;
1114 for (IndirectFieldDecl::chain_iterator ci = ifield->chain_begin(),
1115 ce = ifield->chain_end(); ci != ce; ++ci)
1116 fieldOffset += getFieldOffset(getContext(), cast<FieldDecl>(*ci));
1117 }
1118
1119 CharUnits chars = getContext().toCharUnitsFromBits((int64_t) fieldOffset);
1120 return getCXXABI().EmitMemberDataPointer(type, chars);
1121}
1122
Anders Carlssona83fb4b2010-05-18 16:51:41 +00001123static void
1124FillInNullDataMemberPointers(CodeGenModule &CGM, QualType T,
1125 std::vector<llvm::Constant *> &Elements,
1126 uint64_t StartOffset) {
Ken Dyckc4d68db2011-03-22 00:24:31 +00001127 assert(StartOffset % CGM.getContext().getCharWidth() == 0 &&
1128 "StartOffset not byte aligned!");
Anders Carlssona83fb4b2010-05-18 16:51:41 +00001129
John McCallf16aa102010-08-22 21:01:12 +00001130 if (CGM.getTypes().isZeroInitializable(T))
Anders Carlssona83fb4b2010-05-18 16:51:41 +00001131 return;
1132
1133 if (const ConstantArrayType *CAT =
1134 CGM.getContext().getAsConstantArrayType(T)) {
1135 QualType ElementTy = CAT->getElementType();
1136 uint64_t ElementSize = CGM.getContext().getTypeSize(ElementTy);
1137
1138 for (uint64_t I = 0, E = CAT->getSize().getZExtValue(); I != E; ++I) {
1139 FillInNullDataMemberPointers(CGM, ElementTy, Elements,
1140 StartOffset + I * ElementSize);
1141 }
1142 } else if (const RecordType *RT = T->getAs<RecordType>()) {
1143 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1144 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1145
1146 // Go through all bases and fill in any null pointer to data members.
1147 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1148 E = RD->bases_end(); I != E; ++I) {
Anders Carlsson0a87b372010-05-27 18:51:01 +00001149 if (I->isVirtual()) {
Anders Carlssonada4b322010-11-22 18:42:14 +00001150 // Ignore virtual bases.
Anders Carlsson0a87b372010-05-27 18:51:01 +00001151 continue;
1152 }
Anders Carlssona83fb4b2010-05-18 16:51:41 +00001153
1154 const CXXRecordDecl *BaseDecl =
1155 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1156
1157 // Ignore empty bases.
1158 if (BaseDecl->isEmpty())
1159 continue;
1160
1161 // Ignore bases that don't have any pointer to data members.
John McCallf16aa102010-08-22 21:01:12 +00001162 if (CGM.getTypes().isZeroInitializable(BaseDecl))
Anders Carlssona83fb4b2010-05-18 16:51:41 +00001163 continue;
1164
Anders Carlssona14f5972010-10-31 23:22:37 +00001165 uint64_t BaseOffset = Layout.getBaseClassOffsetInBits(BaseDecl);
Anders Carlssona83fb4b2010-05-18 16:51:41 +00001166 FillInNullDataMemberPointers(CGM, I->getType(),
1167 Elements, StartOffset + BaseOffset);
1168 }
1169
1170 // Visit all fields.
1171 unsigned FieldNo = 0;
1172 for (RecordDecl::field_iterator I = RD->field_begin(),
1173 E = RD->field_end(); I != E; ++I, ++FieldNo) {
1174 QualType FieldType = I->getType();
1175
John McCallf16aa102010-08-22 21:01:12 +00001176 if (CGM.getTypes().isZeroInitializable(FieldType))
Anders Carlssona83fb4b2010-05-18 16:51:41 +00001177 continue;
1178
1179 uint64_t FieldOffset = StartOffset + Layout.getFieldOffset(FieldNo);
1180 FillInNullDataMemberPointers(CGM, FieldType, Elements, FieldOffset);
1181 }
1182 } else {
1183 assert(T->isMemberPointerType() && "Should only see member pointers here!");
1184 assert(!T->getAs<MemberPointerType>()->getPointeeType()->isFunctionType() &&
1185 "Should only see pointers to data members here!");
1186
Ken Dyckc4d68db2011-03-22 00:24:31 +00001187 CharUnits StartIndex = CGM.getContext().toCharUnitsFromBits(StartOffset);
1188 CharUnits EndIndex = StartIndex + CGM.getContext().getTypeSizeInChars(T);
Anders Carlssona83fb4b2010-05-18 16:51:41 +00001189
John McCall5808ce42011-02-03 08:15:49 +00001190 // FIXME: hardcodes Itanium member pointer representation!
Anders Carlssona83fb4b2010-05-18 16:51:41 +00001191 llvm::Constant *NegativeOne =
1192 llvm::ConstantInt::get(llvm::Type::getInt8Ty(CGM.getLLVMContext()),
John McCall5808ce42011-02-03 08:15:49 +00001193 -1ULL, /*isSigned*/true);
Anders Carlssona83fb4b2010-05-18 16:51:41 +00001194
1195 // Fill in the null data member pointer.
Ken Dyckc4d68db2011-03-22 00:24:31 +00001196 for (CharUnits I = StartIndex; I != EndIndex; ++I)
1197 Elements[I.getQuantity()] = NegativeOne;
Anders Carlssona83fb4b2010-05-18 16:51:41 +00001198 }
1199}
1200
John McCall9b7da1c2011-02-15 06:40:56 +00001201static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
Chris Lattner2acc6e32011-07-18 04:24:23 +00001202 llvm::Type *baseType,
John McCall9b7da1c2011-02-15 06:40:56 +00001203 const CXXRecordDecl *base);
1204
Anders Carlssonada4b322010-11-22 18:42:14 +00001205static llvm::Constant *EmitNullConstant(CodeGenModule &CGM,
John McCall9b7da1c2011-02-15 06:40:56 +00001206 const CXXRecordDecl *record,
1207 bool asCompleteObject) {
1208 const CGRecordLayout &layout = CGM.getTypes().getCGRecordLayout(record);
Chris Lattner2acc6e32011-07-18 04:24:23 +00001209 llvm::StructType *structure =
John McCall9b7da1c2011-02-15 06:40:56 +00001210 (asCompleteObject ? layout.getLLVMType()
1211 : layout.getBaseSubobjectLLVMType());
Anders Carlssonada4b322010-11-22 18:42:14 +00001212
John McCall9b7da1c2011-02-15 06:40:56 +00001213 unsigned numElements = structure->getNumElements();
1214 std::vector<llvm::Constant *> elements(numElements);
Anders Carlssonada4b322010-11-22 18:42:14 +00001215
John McCall9b7da1c2011-02-15 06:40:56 +00001216 // Fill in all the bases.
1217 for (CXXRecordDecl::base_class_const_iterator
1218 I = record->bases_begin(), E = record->bases_end(); I != E; ++I) {
Anders Carlssonada4b322010-11-22 18:42:14 +00001219 if (I->isVirtual()) {
John McCall9b7da1c2011-02-15 06:40:56 +00001220 // Ignore virtual bases; if we're laying out for a complete
1221 // object, we'll lay these out later.
Anders Carlssonada4b322010-11-22 18:42:14 +00001222 continue;
1223 }
1224
John McCall9b7da1c2011-02-15 06:40:56 +00001225 const CXXRecordDecl *base =
1226 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
Anders Carlssonada4b322010-11-22 18:42:14 +00001227
1228 // Ignore empty bases.
John McCall9b7da1c2011-02-15 06:40:56 +00001229 if (base->isEmpty())
Anders Carlssonada4b322010-11-22 18:42:14 +00001230 continue;
1231
John McCall9b7da1c2011-02-15 06:40:56 +00001232 unsigned fieldIndex = layout.getNonVirtualBaseLLVMFieldNo(base);
Chris Lattner2acc6e32011-07-18 04:24:23 +00001233 llvm::Type *baseType = structure->getElementType(fieldIndex);
John McCall9b7da1c2011-02-15 06:40:56 +00001234 elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
Anders Carlssonada4b322010-11-22 18:42:14 +00001235 }
1236
John McCall9b7da1c2011-02-15 06:40:56 +00001237 // Fill in all the fields.
1238 for (RecordDecl::field_iterator I = record->field_begin(),
1239 E = record->field_end(); I != E; ++I) {
1240 const FieldDecl *field = *I;
Anders Carlssonada4b322010-11-22 18:42:14 +00001241
1242 // Ignore bit fields.
John McCall9b7da1c2011-02-15 06:40:56 +00001243 if (field->isBitField())
Anders Carlssonada4b322010-11-22 18:42:14 +00001244 continue;
1245
John McCall9b7da1c2011-02-15 06:40:56 +00001246 unsigned fieldIndex = layout.getLLVMFieldNo(field);
1247 elements[fieldIndex] = CGM.EmitNullConstant(field->getType());
1248 }
1249
1250 // Fill in the virtual bases, if we're working with the complete object.
1251 if (asCompleteObject) {
1252 for (CXXRecordDecl::base_class_const_iterator
1253 I = record->vbases_begin(), E = record->vbases_end(); I != E; ++I) {
1254 const CXXRecordDecl *base =
1255 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
1256
1257 // Ignore empty bases.
1258 if (base->isEmpty())
1259 continue;
1260
1261 unsigned fieldIndex = layout.getVirtualBaseIndex(base);
1262
1263 // We might have already laid this field out.
1264 if (elements[fieldIndex]) continue;
1265
Chris Lattner2acc6e32011-07-18 04:24:23 +00001266 llvm::Type *baseType = structure->getElementType(fieldIndex);
John McCall9b7da1c2011-02-15 06:40:56 +00001267 elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
1268 }
Anders Carlssonada4b322010-11-22 18:42:14 +00001269 }
1270
1271 // Now go through all other fields and zero them out.
John McCall9b7da1c2011-02-15 06:40:56 +00001272 for (unsigned i = 0; i != numElements; ++i) {
1273 if (!elements[i])
1274 elements[i] = llvm::Constant::getNullValue(structure->getElementType(i));
Anders Carlssonada4b322010-11-22 18:42:14 +00001275 }
1276
John McCall9b7da1c2011-02-15 06:40:56 +00001277 return llvm::ConstantStruct::get(structure, elements);
1278}
1279
1280/// Emit the null constant for a base subobject.
1281static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
Chris Lattner2acc6e32011-07-18 04:24:23 +00001282 llvm::Type *baseType,
John McCall9b7da1c2011-02-15 06:40:56 +00001283 const CXXRecordDecl *base) {
1284 const CGRecordLayout &baseLayout = CGM.getTypes().getCGRecordLayout(base);
1285
1286 // Just zero out bases that don't have any pointer to data members.
1287 if (baseLayout.isZeroInitializableAsBase())
1288 return llvm::Constant::getNullValue(baseType);
1289
1290 // If the base type is a struct, we can just use its null constant.
1291 if (isa<llvm::StructType>(baseType)) {
1292 return EmitNullConstant(CGM, base, /*complete*/ false);
1293 }
1294
1295 // Otherwise, some bases are represented as arrays of i8 if the size
1296 // of the base is smaller than its corresponding LLVM type. Figure
1297 // out how many elements this base array has.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001298 llvm::ArrayType *baseArrayType = cast<llvm::ArrayType>(baseType);
John McCall9b7da1c2011-02-15 06:40:56 +00001299 unsigned numBaseElements = baseArrayType->getNumElements();
1300
1301 // Fill in null data member pointers.
1302 std::vector<llvm::Constant *> baseElements(numBaseElements);
1303 FillInNullDataMemberPointers(CGM, CGM.getContext().getTypeDeclType(base),
1304 baseElements, 0);
1305
1306 // Now go through all other elements and zero them out.
1307 if (numBaseElements) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00001308 llvm::Type *i8 = llvm::Type::getInt8Ty(CGM.getLLVMContext());
John McCall9b7da1c2011-02-15 06:40:56 +00001309 llvm::Constant *i8_zero = llvm::Constant::getNullValue(i8);
1310 for (unsigned i = 0; i != numBaseElements; ++i) {
1311 if (!baseElements[i])
1312 baseElements[i] = i8_zero;
1313 }
1314 }
1315
1316 return llvm::ConstantArray::get(baseArrayType, baseElements);
Anders Carlssonada4b322010-11-22 18:42:14 +00001317}
1318
Eli Friedman0f593122009-04-13 21:47:26 +00001319llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) {
John McCallf16aa102010-08-22 21:01:12 +00001320 if (getTypes().isZeroInitializable(T))
Anders Carlssonad3e7112009-08-24 17:16:23 +00001321 return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T));
Anders Carlsson2c12d032010-02-02 05:17:25 +00001322
Anders Carlsson237957c2009-08-09 18:26:27 +00001323 if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001324
Anders Carlsson237957c2009-08-09 18:26:27 +00001325 QualType ElementTy = CAT->getElementType();
1326
Anders Carlsson2c12d032010-02-02 05:17:25 +00001327 llvm::Constant *Element = EmitNullConstant(ElementTy);
1328 unsigned NumElements = CAT->getSize().getZExtValue();
1329 std::vector<llvm::Constant *> Array(NumElements);
1330 for (unsigned i = 0; i != NumElements; ++i)
1331 Array[i] = Element;
Mike Stump1eb44332009-09-09 15:08:12 +00001332
Chris Lattner2acc6e32011-07-18 04:24:23 +00001333 llvm::ArrayType *ATy =
Anders Carlsson2c12d032010-02-02 05:17:25 +00001334 cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T));
1335 return llvm::ConstantArray::get(ATy, Array);
Anders Carlsson237957c2009-08-09 18:26:27 +00001336 }
Anders Carlssonfc3eaa42009-08-23 01:25:01 +00001337
1338 if (const RecordType *RT = T->getAs<RecordType>()) {
Anders Carlsson2c12d032010-02-02 05:17:25 +00001339 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall9b7da1c2011-02-15 06:40:56 +00001340 return ::EmitNullConstant(*this, RD, /*complete object*/ true);
Anders Carlssonfc3eaa42009-08-23 01:25:01 +00001341 }
Mike Stump1eb44332009-09-09 15:08:12 +00001342
Anders Carlssona83fb4b2010-05-18 16:51:41 +00001343 assert(T->isMemberPointerType() && "Should only see member pointers here!");
Anders Carlsson2c12d032010-02-02 05:17:25 +00001344 assert(!T->getAs<MemberPointerType>()->getPointeeType()->isFunctionType() &&
1345 "Should only see pointers to data members here!");
1346
1347 // Itanium C++ ABI 2.3:
1348 // A NULL pointer is represented as -1.
John McCall5808ce42011-02-03 08:15:49 +00001349 return getCXXABI().EmitNullMemberPointer(T->castAs<MemberPointerType>());
Eli Friedman0f593122009-04-13 21:47:26 +00001350}
Eli Friedman2ed7cb62011-10-14 02:27:24 +00001351
1352llvm::Constant *
1353CodeGenModule::EmitNullConstantForBase(const CXXRecordDecl *Record) {
1354 return ::EmitNullConstant(*this, Record, false);
1355}