clang  3.7.0
CGExprConstant.cpp
Go to the documentation of this file.
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 "CGCXXABI.h"
16 #include "CGObjCRuntime.h"
17 #include "CGRecordLayout.h"
18 #include "CodeGenModule.h"
19 #include "clang/AST/APValue.h"
20 #include "clang/AST/ASTContext.h"
21 #include "clang/AST/RecordLayout.h"
22 #include "clang/AST/StmtVisitor.h"
23 #include "clang/Basic/Builtins.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/IR/GlobalVariable.h"
28 using namespace clang;
29 using namespace CodeGen;
30 
31 //===----------------------------------------------------------------------===//
32 // ConstStructBuilder
33 //===----------------------------------------------------------------------===//
34 
35 namespace {
36 class ConstExprEmitter;
37 class ConstStructBuilder {
38  CodeGenModule &CGM;
39  CodeGenFunction *CGF;
40 
41  bool Packed;
42  CharUnits NextFieldOffsetInChars;
43  CharUnits LLVMStructAlignment;
45 public:
46  static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CFG,
47  ConstExprEmitter *Emitter,
48  llvm::ConstantStruct *Base,
49  InitListExpr *Updater);
50  static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF,
51  InitListExpr *ILE);
52  static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF,
53  const APValue &Value, QualType ValTy);
54 
55 private:
56  ConstStructBuilder(CodeGenModule &CGM, CodeGenFunction *CGF)
57  : CGM(CGM), CGF(CGF), Packed(false),
58  NextFieldOffsetInChars(CharUnits::Zero()),
59  LLVMStructAlignment(CharUnits::One()) { }
60 
61  void AppendField(const FieldDecl *Field, uint64_t FieldOffset,
62  llvm::Constant *InitExpr);
63 
64  void AppendBytes(CharUnits FieldOffsetInChars, llvm::Constant *InitCst);
65 
66  void AppendBitField(const FieldDecl *Field, uint64_t FieldOffset,
67  llvm::ConstantInt *InitExpr);
68 
69  void AppendPadding(CharUnits PadSize);
70 
71  void AppendTailPadding(CharUnits RecordSize);
72 
73  void ConvertStructToPacked();
74 
75  bool Build(InitListExpr *ILE);
76  bool Build(ConstExprEmitter *Emitter, llvm::ConstantStruct *Base,
77  InitListExpr *Updater);
78  void Build(const APValue &Val, const RecordDecl *RD, bool IsPrimaryBase,
79  const CXXRecordDecl *VTableClass, CharUnits BaseOffset);
80  llvm::Constant *Finalize(QualType Ty);
81 
82  CharUnits getAlignment(const llvm::Constant *C) const {
83  if (Packed) return CharUnits::One();
85  CGM.getDataLayout().getABITypeAlignment(C->getType()));
86  }
87 
88  CharUnits getSizeInChars(const llvm::Constant *C) const {
90  CGM.getDataLayout().getTypeAllocSize(C->getType()));
91  }
92 };
93 
94 void ConstStructBuilder::
95 AppendField(const FieldDecl *Field, uint64_t FieldOffset,
96  llvm::Constant *InitCst) {
97  const ASTContext &Context = CGM.getContext();
98 
99  CharUnits FieldOffsetInChars = Context.toCharUnitsFromBits(FieldOffset);
100 
101  AppendBytes(FieldOffsetInChars, InitCst);
102 }
103 
104 void ConstStructBuilder::
105 AppendBytes(CharUnits FieldOffsetInChars, llvm::Constant *InitCst) {
106 
107  assert(NextFieldOffsetInChars <= FieldOffsetInChars
108  && "Field offset mismatch!");
109 
110  CharUnits FieldAlignment = getAlignment(InitCst);
111 
112  // Round up the field offset to the alignment of the field type.
113  CharUnits AlignedNextFieldOffsetInChars =
114  NextFieldOffsetInChars.RoundUpToAlignment(FieldAlignment);
115 
116  if (AlignedNextFieldOffsetInChars < FieldOffsetInChars) {
117  // We need to append padding.
118  AppendPadding(FieldOffsetInChars - NextFieldOffsetInChars);
119 
120  assert(NextFieldOffsetInChars == FieldOffsetInChars &&
121  "Did not add enough padding!");
122 
123  AlignedNextFieldOffsetInChars =
124  NextFieldOffsetInChars.RoundUpToAlignment(FieldAlignment);
125  }
126 
127  if (AlignedNextFieldOffsetInChars > FieldOffsetInChars) {
128  assert(!Packed && "Alignment is wrong even with a packed struct!");
129 
130  // Convert the struct to a packed struct.
131  ConvertStructToPacked();
132 
133  // After we pack the struct, we may need to insert padding.
134  if (NextFieldOffsetInChars < FieldOffsetInChars) {
135  // We need to append padding.
136  AppendPadding(FieldOffsetInChars - NextFieldOffsetInChars);
137 
138  assert(NextFieldOffsetInChars == FieldOffsetInChars &&
139  "Did not add enough padding!");
140  }
141  AlignedNextFieldOffsetInChars = NextFieldOffsetInChars;
142  }
143 
144  // Add the field.
145  Elements.push_back(InitCst);
146  NextFieldOffsetInChars = AlignedNextFieldOffsetInChars +
147  getSizeInChars(InitCst);
148 
149  if (Packed)
150  assert(LLVMStructAlignment == CharUnits::One() &&
151  "Packed struct not byte-aligned!");
152  else
153  LLVMStructAlignment = std::max(LLVMStructAlignment, FieldAlignment);
154 }
155 
156 void ConstStructBuilder::AppendBitField(const FieldDecl *Field,
157  uint64_t FieldOffset,
158  llvm::ConstantInt *CI) {
159  const ASTContext &Context = CGM.getContext();
160  const uint64_t CharWidth = Context.getCharWidth();
161  uint64_t NextFieldOffsetInBits = Context.toBits(NextFieldOffsetInChars);
162  if (FieldOffset > NextFieldOffsetInBits) {
163  // We need to add padding.
164  CharUnits PadSize = Context.toCharUnitsFromBits(
165  llvm::RoundUpToAlignment(FieldOffset - NextFieldOffsetInBits,
166  Context.getTargetInfo().getCharAlign()));
167 
168  AppendPadding(PadSize);
169  }
170 
171  uint64_t FieldSize = Field->getBitWidthValue(Context);
172 
173  llvm::APInt FieldValue = CI->getValue();
174 
175  // Promote the size of FieldValue if necessary
176  // FIXME: This should never occur, but currently it can because initializer
177  // constants are cast to bool, and because clang is not enforcing bitfield
178  // width limits.
179  if (FieldSize > FieldValue.getBitWidth())
180  FieldValue = FieldValue.zext(FieldSize);
181 
182  // Truncate the size of FieldValue to the bit field size.
183  if (FieldSize < FieldValue.getBitWidth())
184  FieldValue = FieldValue.trunc(FieldSize);
185 
186  NextFieldOffsetInBits = Context.toBits(NextFieldOffsetInChars);
187  if (FieldOffset < NextFieldOffsetInBits) {
188  // Either part of the field or the entire field can go into the previous
189  // byte.
190  assert(!Elements.empty() && "Elements can't be empty!");
191 
192  unsigned BitsInPreviousByte = NextFieldOffsetInBits - FieldOffset;
193 
194  bool FitsCompletelyInPreviousByte =
195  BitsInPreviousByte >= FieldValue.getBitWidth();
196 
197  llvm::APInt Tmp = FieldValue;
198 
199  if (!FitsCompletelyInPreviousByte) {
200  unsigned NewFieldWidth = FieldSize - BitsInPreviousByte;
201 
202  if (CGM.getDataLayout().isBigEndian()) {
203  Tmp = Tmp.lshr(NewFieldWidth);
204  Tmp = Tmp.trunc(BitsInPreviousByte);
205 
206  // We want the remaining high bits.
207  FieldValue = FieldValue.trunc(NewFieldWidth);
208  } else {
209  Tmp = Tmp.trunc(BitsInPreviousByte);
210 
211  // We want the remaining low bits.
212  FieldValue = FieldValue.lshr(BitsInPreviousByte);
213  FieldValue = FieldValue.trunc(NewFieldWidth);
214  }
215  }
216 
217  Tmp = Tmp.zext(CharWidth);
218  if (CGM.getDataLayout().isBigEndian()) {
219  if (FitsCompletelyInPreviousByte)
220  Tmp = Tmp.shl(BitsInPreviousByte - FieldValue.getBitWidth());
221  } else {
222  Tmp = Tmp.shl(CharWidth - BitsInPreviousByte);
223  }
224 
225  // 'or' in the bits that go into the previous byte.
226  llvm::Value *LastElt = Elements.back();
227  if (llvm::ConstantInt *Val = dyn_cast<llvm::ConstantInt>(LastElt))
228  Tmp |= Val->getValue();
229  else {
230  assert(isa<llvm::UndefValue>(LastElt));
231  // If there is an undef field that we're adding to, it can either be a
232  // scalar undef (in which case, we just replace it with our field) or it
233  // is an array. If it is an array, we have to pull one byte off the
234  // array so that the other undef bytes stay around.
235  if (!isa<llvm::IntegerType>(LastElt->getType())) {
236  // The undef padding will be a multibyte array, create a new smaller
237  // padding and then an hole for our i8 to get plopped into.
238  assert(isa<llvm::ArrayType>(LastElt->getType()) &&
239  "Expected array padding of undefs");
240  llvm::ArrayType *AT = cast<llvm::ArrayType>(LastElt->getType());
241  assert(AT->getElementType()->isIntegerTy(CharWidth) &&
242  AT->getNumElements() != 0 &&
243  "Expected non-empty array padding of undefs");
244 
245  // Remove the padding array.
246  NextFieldOffsetInChars -= CharUnits::fromQuantity(AT->getNumElements());
247  Elements.pop_back();
248 
249  // Add the padding back in two chunks.
250  AppendPadding(CharUnits::fromQuantity(AT->getNumElements()-1));
251  AppendPadding(CharUnits::One());
252  assert(isa<llvm::UndefValue>(Elements.back()) &&
253  Elements.back()->getType()->isIntegerTy(CharWidth) &&
254  "Padding addition didn't work right");
255  }
256  }
257 
258  Elements.back() = llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp);
259 
260  if (FitsCompletelyInPreviousByte)
261  return;
262  }
263 
264  while (FieldValue.getBitWidth() > CharWidth) {
265  llvm::APInt Tmp;
266 
267  if (CGM.getDataLayout().isBigEndian()) {
268  // We want the high bits.
269  Tmp =
270  FieldValue.lshr(FieldValue.getBitWidth() - CharWidth).trunc(CharWidth);
271  } else {
272  // We want the low bits.
273  Tmp = FieldValue.trunc(CharWidth);
274 
275  FieldValue = FieldValue.lshr(CharWidth);
276  }
277 
278  Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp));
279  ++NextFieldOffsetInChars;
280 
281  FieldValue = FieldValue.trunc(FieldValue.getBitWidth() - CharWidth);
282  }
283 
284  assert(FieldValue.getBitWidth() > 0 &&
285  "Should have at least one bit left!");
286  assert(FieldValue.getBitWidth() <= CharWidth &&
287  "Should not have more than a byte left!");
288 
289  if (FieldValue.getBitWidth() < CharWidth) {
290  if (CGM.getDataLayout().isBigEndian()) {
291  unsigned BitWidth = FieldValue.getBitWidth();
292 
293  FieldValue = FieldValue.zext(CharWidth) << (CharWidth - BitWidth);
294  } else
295  FieldValue = FieldValue.zext(CharWidth);
296  }
297 
298  // Append the last element.
299  Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(),
300  FieldValue));
301  ++NextFieldOffsetInChars;
302 }
303 
304 void ConstStructBuilder::AppendPadding(CharUnits PadSize) {
305  if (PadSize.isZero())
306  return;
307 
308  llvm::Type *Ty = CGM.Int8Ty;
309  if (PadSize > CharUnits::One())
310  Ty = llvm::ArrayType::get(Ty, PadSize.getQuantity());
311 
312  llvm::Constant *C = llvm::UndefValue::get(Ty);
313  Elements.push_back(C);
314  assert(getAlignment(C) == CharUnits::One() &&
315  "Padding must have 1 byte alignment!");
316 
317  NextFieldOffsetInChars += getSizeInChars(C);
318 }
319 
320 void ConstStructBuilder::AppendTailPadding(CharUnits RecordSize) {
321  assert(NextFieldOffsetInChars <= RecordSize &&
322  "Size mismatch!");
323 
324  AppendPadding(RecordSize - NextFieldOffsetInChars);
325 }
326 
327 void ConstStructBuilder::ConvertStructToPacked() {
328  SmallVector<llvm::Constant *, 16> PackedElements;
329  CharUnits ElementOffsetInChars = CharUnits::Zero();
330 
331  for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
332  llvm::Constant *C = Elements[i];
333 
334  CharUnits ElementAlign = CharUnits::fromQuantity(
335  CGM.getDataLayout().getABITypeAlignment(C->getType()));
336  CharUnits AlignedElementOffsetInChars =
337  ElementOffsetInChars.RoundUpToAlignment(ElementAlign);
338 
339  if (AlignedElementOffsetInChars > ElementOffsetInChars) {
340  // We need some padding.
341  CharUnits NumChars =
342  AlignedElementOffsetInChars - ElementOffsetInChars;
343 
344  llvm::Type *Ty = CGM.Int8Ty;
345  if (NumChars > CharUnits::One())
346  Ty = llvm::ArrayType::get(Ty, NumChars.getQuantity());
347 
348  llvm::Constant *Padding = llvm::UndefValue::get(Ty);
349  PackedElements.push_back(Padding);
350  ElementOffsetInChars += getSizeInChars(Padding);
351  }
352 
353  PackedElements.push_back(C);
354  ElementOffsetInChars += getSizeInChars(C);
355  }
356 
357  assert(ElementOffsetInChars == NextFieldOffsetInChars &&
358  "Packing the struct changed its size!");
359 
360  Elements.swap(PackedElements);
361  LLVMStructAlignment = CharUnits::One();
362  Packed = true;
363 }
364 
365 bool ConstStructBuilder::Build(InitListExpr *ILE) {
366  RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
367  const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
368 
369  unsigned FieldNo = 0;
370  unsigned ElementNo = 0;
371 
372  for (RecordDecl::field_iterator Field = RD->field_begin(),
373  FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
374  // If this is a union, skip all the fields that aren't being initialized.
375  if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
376  continue;
377 
378  // Don't emit anonymous bitfields, they just affect layout.
379  if (Field->isUnnamedBitfield())
380  continue;
381 
382  // Get the initializer. A struct can include fields without initializers,
383  // we just use explicit null values for them.
384  llvm::Constant *EltInit;
385  if (ElementNo < ILE->getNumInits())
386  EltInit = CGM.EmitConstantExpr(ILE->getInit(ElementNo++),
387  Field->getType(), CGF);
388  else
389  EltInit = CGM.EmitNullConstant(Field->getType());
390 
391  if (!EltInit)
392  return false;
393 
394  if (!Field->isBitField()) {
395  // Handle non-bitfield members.
396  AppendField(*Field, Layout.getFieldOffset(FieldNo), EltInit);
397  } else {
398  // Otherwise we have a bitfield.
399  if (auto *CI = dyn_cast<llvm::ConstantInt>(EltInit)) {
400  AppendBitField(*Field, Layout.getFieldOffset(FieldNo), CI);
401  } else {
402  // We are trying to initialize a bitfield with a non-trivial constant,
403  // this must require run-time code.
404  return false;
405  }
406  }
407  }
408 
409  return true;
410 }
411 
412 namespace {
413 struct BaseInfo {
414  BaseInfo(const CXXRecordDecl *Decl, CharUnits Offset, unsigned Index)
415  : Decl(Decl), Offset(Offset), Index(Index) {
416  }
417 
418  const CXXRecordDecl *Decl;
420  unsigned Index;
421 
422  bool operator<(const BaseInfo &O) const { return Offset < O.Offset; }
423 };
424 }
425 
426 void ConstStructBuilder::Build(const APValue &Val, const RecordDecl *RD,
427  bool IsPrimaryBase,
428  const CXXRecordDecl *VTableClass,
429  CharUnits Offset) {
430  const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
431 
432  if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) {
433  // Add a vtable pointer, if we need one and it hasn't already been added.
434  if (CD->isDynamicClass() && !IsPrimaryBase) {
435  llvm::Constant *VTableAddressPoint =
436  CGM.getCXXABI().getVTableAddressPointForConstExpr(
437  BaseSubobject(CD, Offset), VTableClass);
438  AppendBytes(Offset, VTableAddressPoint);
439  }
440 
441  // Accumulate and sort bases, in order to visit them in address order, which
442  // may not be the same as declaration order.
444  Bases.reserve(CD->getNumBases());
445  unsigned BaseNo = 0;
446  for (CXXRecordDecl::base_class_const_iterator Base = CD->bases_begin(),
447  BaseEnd = CD->bases_end(); Base != BaseEnd; ++Base, ++BaseNo) {
448  assert(!Base->isVirtual() && "should not have virtual bases here");
449  const CXXRecordDecl *BD = Base->getType()->getAsCXXRecordDecl();
450  CharUnits BaseOffset = Layout.getBaseClassOffset(BD);
451  Bases.push_back(BaseInfo(BD, BaseOffset, BaseNo));
452  }
453  std::stable_sort(Bases.begin(), Bases.end());
454 
455  for (unsigned I = 0, N = Bases.size(); I != N; ++I) {
456  BaseInfo &Base = Bases[I];
457 
458  bool IsPrimaryBase = Layout.getPrimaryBase() == Base.Decl;
459  Build(Val.getStructBase(Base.Index), Base.Decl, IsPrimaryBase,
460  VTableClass, Offset + Base.Offset);
461  }
462  }
463 
464  unsigned FieldNo = 0;
465  uint64_t OffsetBits = CGM.getContext().toBits(Offset);
466 
467  for (RecordDecl::field_iterator Field = RD->field_begin(),
468  FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
469  // If this is a union, skip all the fields that aren't being initialized.
470  if (RD->isUnion() && Val.getUnionField() != *Field)
471  continue;
472 
473  // Don't emit anonymous bitfields, they just affect layout.
474  if (Field->isUnnamedBitfield())
475  continue;
476 
477  // Emit the value of the initializer.
478  const APValue &FieldValue =
479  RD->isUnion() ? Val.getUnionValue() : Val.getStructField(FieldNo);
480  llvm::Constant *EltInit =
481  CGM.EmitConstantValueForMemory(FieldValue, Field->getType(), CGF);
482  assert(EltInit && "EmitConstantValue can't fail");
483 
484  if (!Field->isBitField()) {
485  // Handle non-bitfield members.
486  AppendField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits, EltInit);
487  } else {
488  // Otherwise we have a bitfield.
489  AppendBitField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits,
490  cast<llvm::ConstantInt>(EltInit));
491  }
492  }
493 }
494 
495 llvm::Constant *ConstStructBuilder::Finalize(QualType Ty) {
496  RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
497  const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
498 
499  CharUnits LayoutSizeInChars = Layout.getSize();
500 
501  if (NextFieldOffsetInChars > LayoutSizeInChars) {
502  // If the struct is bigger than the size of the record type,
503  // we must have a flexible array member at the end.
504  assert(RD->hasFlexibleArrayMember() &&
505  "Must have flexible array member if struct is bigger than type!");
506 
507  // No tail padding is necessary.
508  } else {
509  // Append tail padding if necessary.
510  CharUnits LLVMSizeInChars =
511  NextFieldOffsetInChars.RoundUpToAlignment(LLVMStructAlignment);
512 
513  if (LLVMSizeInChars != LayoutSizeInChars)
514  AppendTailPadding(LayoutSizeInChars);
515 
516  LLVMSizeInChars =
517  NextFieldOffsetInChars.RoundUpToAlignment(LLVMStructAlignment);
518 
519  // Check if we need to convert the struct to a packed struct.
520  if (NextFieldOffsetInChars <= LayoutSizeInChars &&
521  LLVMSizeInChars > LayoutSizeInChars) {
522  assert(!Packed && "Size mismatch!");
523 
524  ConvertStructToPacked();
525  assert(NextFieldOffsetInChars <= LayoutSizeInChars &&
526  "Converting to packed did not help!");
527  }
528 
529  LLVMSizeInChars =
530  NextFieldOffsetInChars.RoundUpToAlignment(LLVMStructAlignment);
531 
532  assert(LayoutSizeInChars == LLVMSizeInChars &&
533  "Tail padding mismatch!");
534  }
535 
536  // Pick the type to use. If the type is layout identical to the ConvertType
537  // type then use it, otherwise use whatever the builder produced for us.
538  llvm::StructType *STy =
539  llvm::ConstantStruct::getTypeForElements(CGM.getLLVMContext(),
540  Elements, Packed);
541  llvm::Type *ValTy = CGM.getTypes().ConvertType(Ty);
542  if (llvm::StructType *ValSTy = dyn_cast<llvm::StructType>(ValTy)) {
543  if (ValSTy->isLayoutIdentical(STy))
544  STy = ValSTy;
545  }
546 
547  llvm::Constant *Result = llvm::ConstantStruct::get(STy, Elements);
548 
549  assert(NextFieldOffsetInChars.RoundUpToAlignment(getAlignment(Result)) ==
550  getSizeInChars(Result) && "Size mismatch!");
551 
552  return Result;
553 }
554 
555 llvm::Constant *ConstStructBuilder::BuildStruct(CodeGenModule &CGM,
556  CodeGenFunction *CGF,
557  ConstExprEmitter *Emitter,
558  llvm::ConstantStruct *Base,
559  InitListExpr *Updater) {
560  ConstStructBuilder Builder(CGM, CGF);
561  if (!Builder.Build(Emitter, Base, Updater))
562  return nullptr;
563  return Builder.Finalize(Updater->getType());
564 }
565 
566 llvm::Constant *ConstStructBuilder::BuildStruct(CodeGenModule &CGM,
567  CodeGenFunction *CGF,
568  InitListExpr *ILE) {
569  ConstStructBuilder Builder(CGM, CGF);
570 
571  if (!Builder.Build(ILE))
572  return nullptr;
573 
574  return Builder.Finalize(ILE->getType());
575 }
576 
577 llvm::Constant *ConstStructBuilder::BuildStruct(CodeGenModule &CGM,
578  CodeGenFunction *CGF,
579  const APValue &Val,
580  QualType ValTy) {
581  ConstStructBuilder Builder(CGM, CGF);
582 
583  const RecordDecl *RD = ValTy->castAs<RecordType>()->getDecl();
584  const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD);
585  Builder.Build(Val, RD, false, CD, CharUnits::Zero());
586 
587  return Builder.Finalize(ValTy);
588 }
589 
590 
591 //===----------------------------------------------------------------------===//
592 // ConstExprEmitter
593 //===----------------------------------------------------------------------===//
594 
595 /// This class only needs to handle two cases:
596 /// 1) Literals (this is used by APValue emission to emit literals).
597 /// 2) Arrays, structs and unions (outside C++11 mode, we don't currently
598 /// constant fold these types).
599 class ConstExprEmitter :
600  public StmtVisitor<ConstExprEmitter, llvm::Constant*> {
601  CodeGenModule &CGM;
602  CodeGenFunction *CGF;
603  llvm::LLVMContext &VMContext;
604 public:
605  ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf)
606  : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()) {
607  }
608 
609  //===--------------------------------------------------------------------===//
610  // Visitor Methods
611  //===--------------------------------------------------------------------===//
612 
613  llvm::Constant *VisitStmt(Stmt *S) {
614  return nullptr;
615  }
616 
617  llvm::Constant *VisitParenExpr(ParenExpr *PE) {
618  return Visit(PE->getSubExpr());
619  }
620 
621  llvm::Constant *
622  VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) {
623  return Visit(PE->getReplacement());
624  }
625 
626  llvm::Constant *VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
627  return Visit(GE->getResultExpr());
628  }
629 
630  llvm::Constant *VisitChooseExpr(ChooseExpr *CE) {
631  return Visit(CE->getChosenSubExpr());
632  }
633 
634  llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
635  return Visit(E->getInitializer());
636  }
637 
638  llvm::Constant *VisitCastExpr(CastExpr* E) {
639  Expr *subExpr = E->getSubExpr();
640  llvm::Constant *C = CGM.EmitConstantExpr(subExpr, subExpr->getType(), CGF);
641  if (!C) return nullptr;
642 
643  llvm::Type *destType = ConvertType(E->getType());
644 
645  switch (E->getCastKind()) {
646  case CK_ToUnion: {
647  // GCC cast to union extension
648  assert(E->getType()->isUnionType() &&
649  "Destination type is not union type!");
650 
651  // Build a struct with the union sub-element as the first member,
652  // and padded to the appropriate size
655  Elts.push_back(C);
656  Types.push_back(C->getType());
657  unsigned CurSize = CGM.getDataLayout().getTypeAllocSize(C->getType());
658  unsigned TotalSize = CGM.getDataLayout().getTypeAllocSize(destType);
659 
660  assert(CurSize <= TotalSize && "Union size mismatch!");
661  if (unsigned NumPadBytes = TotalSize - CurSize) {
662  llvm::Type *Ty = CGM.Int8Ty;
663  if (NumPadBytes > 1)
664  Ty = llvm::ArrayType::get(Ty, NumPadBytes);
665 
666  Elts.push_back(llvm::UndefValue::get(Ty));
667  Types.push_back(Ty);
668  }
669 
670  llvm::StructType* STy =
671  llvm::StructType::get(C->getType()->getContext(), Types, false);
672  return llvm::ConstantStruct::get(STy, Elts);
673  }
674 
676  return llvm::ConstantExpr::getAddrSpaceCast(C, destType);
677 
678  case CK_LValueToRValue:
681  case CK_NoOp:
683  return C;
684 
685  case CK_Dependent: llvm_unreachable("saw dependent cast!");
686 
687  case CK_BuiltinFnToFnPtr:
688  llvm_unreachable("builtin functions are handled elsewhere");
689 
693  return CGM.getCXXABI().EmitMemberPointerConversion(E, C);
694 
695  // These will never be supported.
697  case CK_ARCProduceObject:
698  case CK_ARCConsumeObject:
702  return nullptr;
703 
704  // These don't need to be handled here because Evaluate knows how to
705  // evaluate them in the cases where they can be folded.
706  case CK_BitCast:
707  case CK_ToVoid:
708  case CK_Dynamic:
709  case CK_LValueBitCast:
717  case CK_BaseToDerived:
718  case CK_DerivedToBase:
721  case CK_VectorSplat:
733  case CK_PointerToBoolean:
734  case CK_NullToPointer:
735  case CK_IntegralCast:
741  case CK_FloatingCast:
742  case CK_ZeroToOCLEvent:
743  return nullptr;
744  }
745  llvm_unreachable("Invalid CastKind");
746  }
747 
748  llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
749  return Visit(DAE->getExpr());
750  }
751 
752  llvm::Constant *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
753  // No need for a DefaultInitExprScope: we don't handle 'this' in a
754  // constant expression.
755  return Visit(DIE->getExpr());
756  }
757 
758  llvm::Constant *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
759  return Visit(E->GetTemporaryExpr());
760  }
761 
762  llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) {
763  if (ILE->isStringLiteralInit())
764  return Visit(ILE->getInit(0));
765 
766  llvm::ArrayType *AType =
767  cast<llvm::ArrayType>(ConvertType(ILE->getType()));
768  llvm::Type *ElemTy = AType->getElementType();
769  unsigned NumInitElements = ILE->getNumInits();
770  unsigned NumElements = AType->getNumElements();
771 
772  // Initialising an array requires us to automatically
773  // initialise any elements that have not been initialised explicitly
774  unsigned NumInitableElts = std::min(NumInitElements, NumElements);
775 
776  // Initialize remaining array elements.
777  // FIXME: This doesn't handle member pointers correctly!
778  llvm::Constant *fillC;
779  if (Expr *filler = ILE->getArrayFiller())
780  fillC = CGM.EmitConstantExpr(filler, filler->getType(), CGF);
781  else
782  fillC = llvm::Constant::getNullValue(ElemTy);
783  if (!fillC)
784  return nullptr;
785 
786  // Try to use a ConstantAggregateZero if we can.
787  if (fillC->isNullValue() && !NumInitableElts)
788  return llvm::ConstantAggregateZero::get(AType);
789 
790  // Copy initializer elements.
791  std::vector<llvm::Constant*> Elts;
792  Elts.reserve(NumInitableElts + NumElements);
793 
794  bool RewriteType = false;
795  for (unsigned i = 0; i < NumInitableElts; ++i) {
796  Expr *Init = ILE->getInit(i);
797  llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF);
798  if (!C)
799  return nullptr;
800  RewriteType |= (C->getType() != ElemTy);
801  Elts.push_back(C);
802  }
803 
804  RewriteType |= (fillC->getType() != ElemTy);
805  Elts.resize(NumElements, fillC);
806 
807  if (RewriteType) {
808  // FIXME: Try to avoid packing the array
809  std::vector<llvm::Type*> Types;
810  Types.reserve(NumInitableElts + NumElements);
811  for (unsigned i = 0, e = Elts.size(); i < e; ++i)
812  Types.push_back(Elts[i]->getType());
813  llvm::StructType *SType = llvm::StructType::get(AType->getContext(),
814  Types, true);
815  return llvm::ConstantStruct::get(SType, Elts);
816  }
817 
818  return llvm::ConstantArray::get(AType, Elts);
819  }
820 
821  llvm::Constant *EmitRecordInitialization(InitListExpr *ILE) {
822  return ConstStructBuilder::BuildStruct(CGM, CGF, ILE);
823  }
824 
825  llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E) {
826  return CGM.EmitNullConstant(E->getType());
827  }
828 
829  llvm::Constant *VisitInitListExpr(InitListExpr *ILE) {
830  if (ILE->getType()->isArrayType())
831  return EmitArrayInitialization(ILE);
832 
833  if (ILE->getType()->isRecordType())
834  return EmitRecordInitialization(ILE);
835 
836  return nullptr;
837  }
838 
839  llvm::Constant *EmitDesignatedInitUpdater(llvm::Constant *Base,
840  InitListExpr *Updater) {
841  QualType ExprType = Updater->getType();
842 
843  if (ExprType->isArrayType()) {
844  llvm::ArrayType *AType = cast<llvm::ArrayType>(ConvertType(ExprType));
845  llvm::Type *ElemType = AType->getElementType();
846 
847  unsigned NumInitElements = Updater->getNumInits();
848  unsigned NumElements = AType->getNumElements();
849 
850  std::vector<llvm::Constant *> Elts;
851  Elts.reserve(NumElements);
852 
853  if (llvm::ConstantDataArray *DataArray =
854  dyn_cast<llvm::ConstantDataArray>(Base))
855  for (unsigned i = 0; i != NumElements; ++i)
856  Elts.push_back(DataArray->getElementAsConstant(i));
857  else if (llvm::ConstantArray *Array =
858  dyn_cast<llvm::ConstantArray>(Base))
859  for (unsigned i = 0; i != NumElements; ++i)
860  Elts.push_back(Array->getOperand(i));
861  else
862  return nullptr; // FIXME: other array types not implemented
863 
864  llvm::Constant *fillC = nullptr;
865  if (Expr *filler = Updater->getArrayFiller())
866  if (!isa<NoInitExpr>(filler))
867  fillC = CGM.EmitConstantExpr(filler, filler->getType(), CGF);
868  bool RewriteType = (fillC && fillC->getType() != ElemType);
869 
870  for (unsigned i = 0; i != NumElements; ++i) {
871  Expr *Init = nullptr;
872  if (i < NumInitElements)
873  Init = Updater->getInit(i);
874 
875  if (!Init && fillC)
876  Elts[i] = fillC;
877  else if (!Init || isa<NoInitExpr>(Init))
878  ; // Do nothing.
879  else if (InitListExpr *ChildILE = dyn_cast<InitListExpr>(Init))
880  Elts[i] = EmitDesignatedInitUpdater(Elts[i], ChildILE);
881  else
882  Elts[i] = CGM.EmitConstantExpr(Init, Init->getType(), CGF);
883 
884  if (!Elts[i])
885  return nullptr;
886  RewriteType |= (Elts[i]->getType() != ElemType);
887  }
888 
889  if (RewriteType) {
890  std::vector<llvm::Type *> Types;
891  Types.reserve(NumElements);
892  for (unsigned i = 0; i != NumElements; ++i)
893  Types.push_back(Elts[i]->getType());
894  llvm::StructType *SType = llvm::StructType::get(AType->getContext(),
895  Types, true);
896  return llvm::ConstantStruct::get(SType, Elts);
897  }
898 
899  return llvm::ConstantArray::get(AType, Elts);
900  }
901 
902  if (ExprType->isRecordType())
903  return ConstStructBuilder::BuildStruct(CGM, CGF, this,
904  dyn_cast<llvm::ConstantStruct>(Base), Updater);
905 
906  return nullptr;
907  }
908 
909  llvm::Constant *VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr *E) {
910  return EmitDesignatedInitUpdater(
911  CGM.EmitConstantExpr(E->getBase(), E->getType(), CGF),
912  E->getUpdater());
913  }
914 
915  llvm::Constant *VisitCXXConstructExpr(CXXConstructExpr *E) {
916  if (!E->getConstructor()->isTrivial())
917  return nullptr;
918 
919  QualType Ty = E->getType();
920 
921  // FIXME: We should not have to call getBaseElementType here.
922  const RecordType *RT =
924  const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
925 
926  // If the class doesn't have a trivial destructor, we can't emit it as a
927  // constant expr.
928  if (!RD->hasTrivialDestructor())
929  return nullptr;
930 
931  // Only copy and default constructors can be trivial.
932 
933 
934  if (E->getNumArgs()) {
935  assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument");
936  assert(E->getConstructor()->isCopyOrMoveConstructor() &&
937  "trivial ctor has argument but isn't a copy/move ctor");
938 
939  Expr *Arg = E->getArg(0);
940  assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) &&
941  "argument to copy ctor is of wrong type");
942 
943  return Visit(Arg);
944  }
945 
946  return CGM.EmitNullConstant(Ty);
947  }
948 
949  llvm::Constant *VisitStringLiteral(StringLiteral *E) {
950  return CGM.GetConstantArrayFromStringLiteral(E);
951  }
952 
953  llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
954  // This must be an @encode initializing an array in a static initializer.
955  // Don't emit it as the address of the string, emit the string data itself
956  // as an inline array.
957  std::string Str;
959  QualType T = E->getType();
960  if (T->getTypeClass() == Type::TypeOfExpr)
961  T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
962  const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
963 
964  // Resize the string to the right size, adding zeros at the end, or
965  // truncating as needed.
966  Str.resize(CAT->getSize().getZExtValue(), '\0');
967  return llvm::ConstantDataArray::getString(VMContext, Str, false);
968  }
969 
970  llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) {
971  return Visit(E->getSubExpr());
972  }
973 
974  // Utility methods
975  llvm::Type *ConvertType(QualType T) {
976  return CGM.getTypes().ConvertType(T);
977  }
978 
979 public:
980  llvm::Constant *EmitLValue(APValue::LValueBase LVBase) {
981  if (const ValueDecl *Decl = LVBase.dyn_cast<const ValueDecl*>()) {
982  if (Decl->hasAttr<WeakRefAttr>())
983  return CGM.GetWeakRefReference(Decl);
984  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl))
985  return CGM.GetAddrOfFunction(FD);
986  if (const VarDecl* VD = dyn_cast<VarDecl>(Decl)) {
987  // We can never refer to a variable with local storage.
988  if (!VD->hasLocalStorage()) {
989  if (VD->isFileVarDecl() || VD->hasExternalStorage())
990  return CGM.GetAddrOfGlobalVar(VD);
991  else if (VD->isLocalVarDecl())
992  return CGM.getOrCreateStaticVarDecl(
993  *VD, CGM.getLLVMLinkageVarDefinition(VD, /*isConstant=*/false));
994  }
995  }
996  return nullptr;
997  }
998 
999  Expr *E = const_cast<Expr*>(LVBase.get<const Expr*>());
1000  switch (E->getStmtClass()) {
1001  default: break;
1002  case Expr::CompoundLiteralExprClass: {
1003  // Note that due to the nature of compound literals, this is guaranteed
1004  // to be the only use of the variable, so we just generate it here.
1005  CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E);
1006  llvm::Constant* C = CGM.EmitConstantExpr(CLE->getInitializer(),
1007  CLE->getType(), CGF);
1008  // FIXME: "Leaked" on failure.
1009  if (C)
1010  C = new llvm::GlobalVariable(CGM.getModule(), C->getType(),
1011  E->getType().isConstant(CGM.getContext()),
1013  C, ".compoundliteral", nullptr,
1014  llvm::GlobalVariable::NotThreadLocal,
1016  return C;
1017  }
1018  case Expr::StringLiteralClass:
1019  return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E));
1020  case Expr::ObjCEncodeExprClass:
1021  return CGM.GetAddrOfConstantStringFromObjCEncode(cast<ObjCEncodeExpr>(E));
1022  case Expr::ObjCStringLiteralClass: {
1023  ObjCStringLiteral* SL = cast<ObjCStringLiteral>(E);
1024  llvm::Constant *C =
1026  return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
1027  }
1028  case Expr::PredefinedExprClass: {
1029  unsigned Type = cast<PredefinedExpr>(E)->getIdentType();
1030  if (CGF) {
1031  LValue Res = CGF->EmitPredefinedLValue(cast<PredefinedExpr>(E));
1032  return cast<llvm::Constant>(Res.getAddress());
1033  } else if (Type == PredefinedExpr::PrettyFunction) {
1034  return CGM.GetAddrOfConstantCString("top level", ".tmp");
1035  }
1036 
1037  return CGM.GetAddrOfConstantCString("", ".tmp");
1038  }
1039  case Expr::AddrLabelExprClass: {
1040  assert(CGF && "Invalid address of label expression outside function.");
1041  llvm::Constant *Ptr =
1042  CGF->GetAddrOfLabel(cast<AddrLabelExpr>(E)->getLabel());
1043  return llvm::ConstantExpr::getBitCast(Ptr, ConvertType(E->getType()));
1044  }
1045  case Expr::CallExprClass: {
1046  CallExpr* CE = cast<CallExpr>(E);
1047  unsigned builtin = CE->getBuiltinCallee();
1048  if (builtin !=
1049  Builtin::BI__builtin___CFStringMakeConstantString &&
1050  builtin !=
1051  Builtin::BI__builtin___NSStringMakeConstantString)
1052  break;
1053  const Expr *Arg = CE->getArg(0)->IgnoreParenCasts();
1054  const StringLiteral *Literal = cast<StringLiteral>(Arg);
1055  if (builtin ==
1056  Builtin::BI__builtin___NSStringMakeConstantString) {
1057  return CGM.getObjCRuntime().GenerateConstantString(Literal);
1058  }
1059  // FIXME: need to deal with UCN conversion issues.
1060  return CGM.GetAddrOfConstantCFString(Literal);
1061  }
1062  case Expr::BlockExprClass: {
1063  std::string FunctionName;
1064  if (CGF)
1065  FunctionName = CGF->CurFn->getName();
1066  else
1067  FunctionName = "global";
1068 
1069  return CGM.GetAddrOfGlobalBlock(cast<BlockExpr>(E), FunctionName.c_str());
1070  }
1071  case Expr::CXXTypeidExprClass: {
1072  CXXTypeidExpr *Typeid = cast<CXXTypeidExpr>(E);
1073  QualType T;
1074  if (Typeid->isTypeOperand())
1075  T = Typeid->getTypeOperand(CGM.getContext());
1076  else
1077  T = Typeid->getExprOperand()->getType();
1078  return CGM.GetAddrOfRTTIDescriptor(T);
1079  }
1080  case Expr::CXXUuidofExprClass: {
1081  return CGM.GetAddrOfUuidDescriptor(cast<CXXUuidofExpr>(E));
1082  }
1083  case Expr::MaterializeTemporaryExprClass: {
1084  MaterializeTemporaryExpr *MTE = cast<MaterializeTemporaryExpr>(E);
1085  assert(MTE->getStorageDuration() == SD_Static);
1086  SmallVector<const Expr *, 2> CommaLHSs;
1088  const Expr *Inner = MTE->GetTemporaryExpr()
1089  ->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments);
1090  return CGM.GetAddrOfGlobalTemporary(MTE, Inner);
1091  }
1092  }
1093 
1094  return nullptr;
1095  }
1096 };
1097 
1098 } // end anonymous namespace.
1099 
1100 bool ConstStructBuilder::Build(ConstExprEmitter *Emitter,
1101  llvm::ConstantStruct *Base,
1102  InitListExpr *Updater) {
1103  assert(Base && "base expression should not be empty");
1104 
1105  QualType ExprType = Updater->getType();
1106  RecordDecl *RD = ExprType->getAs<RecordType>()->getDecl();
1107  const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1108  const llvm::StructLayout *BaseLayout = CGM.getDataLayout().getStructLayout(
1109  Base->getType());
1110  unsigned FieldNo = -1;
1111  unsigned ElementNo = 0;
1112 
1113  for (FieldDecl *Field : RD->fields()) {
1114  ++FieldNo;
1115 
1116  if (RD->isUnion() && Updater->getInitializedFieldInUnion() != Field)
1117  continue;
1118 
1119  // Skip anonymous bitfields.
1120  if (Field->isUnnamedBitfield())
1121  continue;
1122 
1123  llvm::Constant *EltInit = Base->getOperand(ElementNo);
1124 
1125  // Bail out if the type of the ConstantStruct does not have the same layout
1126  // as the type of the InitListExpr.
1127  if (CGM.getTypes().ConvertType(Field->getType()) != EltInit->getType() ||
1128  Layout.getFieldOffset(ElementNo) !=
1129  BaseLayout->getElementOffsetInBits(ElementNo))
1130  return false;
1131 
1132  // Get the initializer. If we encounter an empty field or a NoInitExpr,
1133  // we use values from the base expression.
1134  Expr *Init = nullptr;
1135  if (ElementNo < Updater->getNumInits())
1136  Init = Updater->getInit(ElementNo);
1137 
1138  if (!Init || isa<NoInitExpr>(Init))
1139  ; // Do nothing.
1140  else if (InitListExpr *ChildILE = dyn_cast<InitListExpr>(Init))
1141  EltInit = Emitter->EmitDesignatedInitUpdater(EltInit, ChildILE);
1142  else
1143  EltInit = CGM.EmitConstantExpr(Init, Field->getType(), CGF);
1144 
1145  ++ElementNo;
1146 
1147  if (!EltInit)
1148  return false;
1149 
1150  if (!Field->isBitField())
1151  AppendField(Field, Layout.getFieldOffset(FieldNo), EltInit);
1152  else if (llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(EltInit))
1153  AppendBitField(Field, Layout.getFieldOffset(FieldNo), CI);
1154  else
1155  // Initializing a bitfield with a non-trivial constant?
1156  return false;
1157  }
1158 
1159  return true;
1160 }
1161 
1162 llvm::Constant *CodeGenModule::EmitConstantInit(const VarDecl &D,
1163  CodeGenFunction *CGF) {
1164  // Make a quick check if variable can be default NULL initialized
1165  // and avoid going through rest of code which may do, for c++11,
1166  // initialization of memory to all NULLs.
1167  if (!D.hasLocalStorage()) {
1168  QualType Ty = D.getType();
1169  if (Ty->isArrayType())
1170  Ty = Context.getBaseElementType(Ty);
1171  if (Ty->isRecordType())
1172  if (const CXXConstructExpr *E =
1173  dyn_cast_or_null<CXXConstructExpr>(D.getInit())) {
1174  const CXXConstructorDecl *CD = E->getConstructor();
1175  if (CD->isTrivial() && CD->isDefaultConstructor())
1176  return EmitNullConstant(D.getType());
1177  }
1178  }
1179 
1180  if (const APValue *Value = D.evaluateValue())
1181  return EmitConstantValueForMemory(*Value, D.getType(), CGF);
1182 
1183  // FIXME: Implement C++11 [basic.start.init]p2: if the initializer of a
1184  // reference is a constant expression, and the reference binds to a temporary,
1185  // then constant initialization is performed. ConstExprEmitter will
1186  // incorrectly emit a prvalue constant in this case, and the calling code
1187  // interprets that as the (pointer) value of the reference, rather than the
1188  // desired value of the referee.
1189  if (D.getType()->isReferenceType())
1190  return nullptr;
1191 
1192  const Expr *E = D.getInit();
1193  assert(E && "No initializer to emit");
1194 
1195  llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E));
1196  if (C && C->getType()->isIntegerTy(1)) {
1197  llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
1198  C = llvm::ConstantExpr::getZExt(C, BoolTy);
1199  }
1200  return C;
1201 }
1202 
1203 llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E,
1204  QualType DestType,
1205  CodeGenFunction *CGF) {
1206  Expr::EvalResult Result;
1207 
1208  bool Success = false;
1209 
1210  if (DestType->isReferenceType())
1211  Success = E->EvaluateAsLValue(Result, Context);
1212  else
1213  Success = E->EvaluateAsRValue(Result, Context);
1214 
1215  llvm::Constant *C = nullptr;
1216  if (Success && !Result.HasSideEffects)
1217  C = EmitConstantValue(Result.Val, DestType, CGF);
1218  else
1219  C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E));
1220 
1221  if (C && C->getType()->isIntegerTy(1)) {
1222  llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
1223  C = llvm::ConstantExpr::getZExt(C, BoolTy);
1224  }
1225  return C;
1226 }
1227 
1229  QualType DestType,
1230  CodeGenFunction *CGF) {
1231  // For an _Atomic-qualified constant, we may need to add tail padding.
1232  if (auto *AT = DestType->getAs<AtomicType>()) {
1233  QualType InnerType = AT->getValueType();
1234  auto *Inner = EmitConstantValue(Value, InnerType, CGF);
1235 
1236  uint64_t InnerSize = Context.getTypeSize(InnerType);
1237  uint64_t OuterSize = Context.getTypeSize(DestType);
1238  if (InnerSize == OuterSize)
1239  return Inner;
1240 
1241  assert(InnerSize < OuterSize && "emitted over-large constant for atomic");
1242  llvm::Constant *Elts[] = {
1243  Inner,
1244  llvm::ConstantAggregateZero::get(
1245  llvm::ArrayType::get(Int8Ty, (OuterSize - InnerSize) / 8))
1246  };
1247  return llvm::ConstantStruct::getAnon(Elts);
1248  }
1249 
1250  switch (Value.getKind()) {
1252  llvm_unreachable("Constant expressions should be initialized.");
1253  case APValue::LValue: {
1254  llvm::Type *DestTy = getTypes().ConvertTypeForMem(DestType);
1255  llvm::Constant *Offset =
1256  llvm::ConstantInt::get(Int64Ty, Value.getLValueOffset().getQuantity());
1257 
1258  llvm::Constant *C;
1259  if (APValue::LValueBase LVBase = Value.getLValueBase()) {
1260  // An array can be represented as an lvalue referring to the base.
1261  if (isa<llvm::ArrayType>(DestTy)) {
1262  assert(Offset->isNullValue() && "offset on array initializer");
1263  return ConstExprEmitter(*this, CGF).Visit(
1264  const_cast<Expr*>(LVBase.get<const Expr*>()));
1265  }
1266 
1267  C = ConstExprEmitter(*this, CGF).EmitLValue(LVBase);
1268 
1269  // Apply offset if necessary.
1270  if (!Offset->isNullValue()) {
1271  unsigned AS = C->getType()->getPointerAddressSpace();
1272  llvm::Type *CharPtrTy = Int8Ty->getPointerTo(AS);
1273  llvm::Constant *Casted = llvm::ConstantExpr::getBitCast(C, CharPtrTy);
1274  Casted = llvm::ConstantExpr::getGetElementPtr(Int8Ty, Casted, Offset);
1275  C = llvm::ConstantExpr::getPointerCast(Casted, C->getType());
1276  }
1277 
1278  // Convert to the appropriate type; this could be an lvalue for
1279  // an integer.
1280  if (isa<llvm::PointerType>(DestTy))
1281  return llvm::ConstantExpr::getPointerCast(C, DestTy);
1282 
1283  return llvm::ConstantExpr::getPtrToInt(C, DestTy);
1284  } else {
1285  C = Offset;
1286 
1287  // Convert to the appropriate type; this could be an lvalue for
1288  // an integer.
1289  if (isa<llvm::PointerType>(DestTy))
1290  return llvm::ConstantExpr::getIntToPtr(C, DestTy);
1291 
1292  // If the types don't match this should only be a truncate.
1293  if (C->getType() != DestTy)
1294  return llvm::ConstantExpr::getTrunc(C, DestTy);
1295 
1296  return C;
1297  }
1298  }
1299  case APValue::Int:
1300  return llvm::ConstantInt::get(VMContext, Value.getInt());
1301  case APValue::ComplexInt: {
1302  llvm::Constant *Complex[2];
1303 
1304  Complex[0] = llvm::ConstantInt::get(VMContext,
1305  Value.getComplexIntReal());
1306  Complex[1] = llvm::ConstantInt::get(VMContext,
1307  Value.getComplexIntImag());
1308 
1309  // FIXME: the target may want to specify that this is packed.
1310  llvm::StructType *STy = llvm::StructType::get(Complex[0]->getType(),
1311  Complex[1]->getType(),
1312  nullptr);
1313  return llvm::ConstantStruct::get(STy, Complex);
1314  }
1315  case APValue::Float: {
1316  const llvm::APFloat &Init = Value.getFloat();
1317  if (&Init.getSemantics() == &llvm::APFloat::IEEEhalf &&
1318  !Context.getLangOpts().NativeHalfType &&
1319  !Context.getLangOpts().HalfArgsAndReturns)
1320  return llvm::ConstantInt::get(VMContext, Init.bitcastToAPInt());
1321  else
1322  return llvm::ConstantFP::get(VMContext, Init);
1323  }
1324  case APValue::ComplexFloat: {
1325  llvm::Constant *Complex[2];
1326 
1327  Complex[0] = llvm::ConstantFP::get(VMContext,
1328  Value.getComplexFloatReal());
1329  Complex[1] = llvm::ConstantFP::get(VMContext,
1330  Value.getComplexFloatImag());
1331 
1332  // FIXME: the target may want to specify that this is packed.
1333  llvm::StructType *STy = llvm::StructType::get(Complex[0]->getType(),
1334  Complex[1]->getType(),
1335  nullptr);
1336  return llvm::ConstantStruct::get(STy, Complex);
1337  }
1338  case APValue::Vector: {
1340  unsigned NumElts = Value.getVectorLength();
1341 
1342  for (unsigned i = 0; i != NumElts; ++i) {
1343  const APValue &Elt = Value.getVectorElt(i);
1344  if (Elt.isInt())
1345  Inits.push_back(llvm::ConstantInt::get(VMContext, Elt.getInt()));
1346  else
1347  Inits.push_back(llvm::ConstantFP::get(VMContext, Elt.getFloat()));
1348  }
1349  return llvm::ConstantVector::get(Inits);
1350  }
1351  case APValue::AddrLabelDiff: {
1352  const AddrLabelExpr *LHSExpr = Value.getAddrLabelDiffLHS();
1353  const AddrLabelExpr *RHSExpr = Value.getAddrLabelDiffRHS();
1354  llvm::Constant *LHS = EmitConstantExpr(LHSExpr, LHSExpr->getType(), CGF);
1355  llvm::Constant *RHS = EmitConstantExpr(RHSExpr, RHSExpr->getType(), CGF);
1356 
1357  // Compute difference
1358  llvm::Type *ResultType = getTypes().ConvertType(DestType);
1359  LHS = llvm::ConstantExpr::getPtrToInt(LHS, IntPtrTy);
1360  RHS = llvm::ConstantExpr::getPtrToInt(RHS, IntPtrTy);
1361  llvm::Constant *AddrLabelDiff = llvm::ConstantExpr::getSub(LHS, RHS);
1362 
1363  // LLVM is a bit sensitive about the exact format of the
1364  // address-of-label difference; make sure to truncate after
1365  // the subtraction.
1366  return llvm::ConstantExpr::getTruncOrBitCast(AddrLabelDiff, ResultType);
1367  }
1368  case APValue::Struct:
1369  case APValue::Union:
1370  return ConstStructBuilder::BuildStruct(*this, CGF, Value, DestType);
1371  case APValue::Array: {
1372  const ArrayType *CAT = Context.getAsArrayType(DestType);
1373  unsigned NumElements = Value.getArraySize();
1374  unsigned NumInitElts = Value.getArrayInitializedElts();
1375 
1376  // Emit array filler, if there is one.
1377  llvm::Constant *Filler = nullptr;
1378  if (Value.hasArrayFiller())
1379  Filler = EmitConstantValueForMemory(Value.getArrayFiller(),
1380  CAT->getElementType(), CGF);
1381 
1382  // Emit initializer elements.
1383  llvm::Type *CommonElementType =
1385 
1386  // Try to use a ConstantAggregateZero if we can.
1387  if (Filler && Filler->isNullValue() && !NumInitElts) {
1388  llvm::ArrayType *AType =
1389  llvm::ArrayType::get(CommonElementType, NumElements);
1390  return llvm::ConstantAggregateZero::get(AType);
1391  }
1392 
1393  std::vector<llvm::Constant*> Elts;
1394  Elts.reserve(NumElements);
1395  for (unsigned I = 0; I < NumElements; ++I) {
1396  llvm::Constant *C = Filler;
1397  if (I < NumInitElts)
1399  CAT->getElementType(), CGF);
1400  else
1401  assert(Filler && "Missing filler for implicit elements of initializer");
1402  if (I == 0)
1403  CommonElementType = C->getType();
1404  else if (C->getType() != CommonElementType)
1405  CommonElementType = nullptr;
1406  Elts.push_back(C);
1407  }
1408 
1409  if (!CommonElementType) {
1410  // FIXME: Try to avoid packing the array
1411  std::vector<llvm::Type*> Types;
1412  Types.reserve(NumElements);
1413  for (unsigned i = 0, e = Elts.size(); i < e; ++i)
1414  Types.push_back(Elts[i]->getType());
1415  llvm::StructType *SType = llvm::StructType::get(VMContext, Types, true);
1416  return llvm::ConstantStruct::get(SType, Elts);
1417  }
1418 
1419  llvm::ArrayType *AType =
1420  llvm::ArrayType::get(CommonElementType, NumElements);
1421  return llvm::ConstantArray::get(AType, Elts);
1422  }
1424  return getCXXABI().EmitMemberPointer(Value, DestType);
1425  }
1426  llvm_unreachable("Unknown APValue kind");
1427 }
1428 
1429 llvm::Constant *
1431  QualType DestType,
1432  CodeGenFunction *CGF) {
1433  llvm::Constant *C = EmitConstantValue(Value, DestType, CGF);
1434  if (C->getType()->isIntegerTy(1)) {
1435  llvm::Type *BoolTy = getTypes().ConvertTypeForMem(DestType);
1436  C = llvm::ConstantExpr::getZExt(C, BoolTy);
1437  }
1438  return C;
1439 }
1440 
1441 llvm::Constant *
1443  assert(E->isFileScope() && "not a file-scope compound literal expr");
1444  return ConstExprEmitter(*this, nullptr).EmitLValue(E);
1445 }
1446 
1447 llvm::Constant *
1449  // Member pointer constants always have a very particular form.
1450  const MemberPointerType *type = cast<MemberPointerType>(uo->getType());
1451  const ValueDecl *decl = cast<DeclRefExpr>(uo->getSubExpr())->getDecl();
1452 
1453  // A member function pointer.
1454  if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(decl))
1455  return getCXXABI().EmitMemberFunctionPointer(method);
1456 
1457  // Otherwise, a member data pointer.
1458  uint64_t fieldOffset = getContext().getFieldOffset(decl);
1459  CharUnits chars = getContext().toCharUnitsFromBits((int64_t) fieldOffset);
1460  return getCXXABI().EmitMemberDataPointer(type, chars);
1461 }
1462 
1463 static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
1464  llvm::Type *baseType,
1465  const CXXRecordDecl *base);
1466 
1467 static llvm::Constant *EmitNullConstant(CodeGenModule &CGM,
1468  const CXXRecordDecl *record,
1469  bool asCompleteObject) {
1470  const CGRecordLayout &layout = CGM.getTypes().getCGRecordLayout(record);
1471  llvm::StructType *structure =
1472  (asCompleteObject ? layout.getLLVMType()
1473  : layout.getBaseSubobjectLLVMType());
1474 
1475  unsigned numElements = structure->getNumElements();
1476  std::vector<llvm::Constant *> elements(numElements);
1477 
1478  // Fill in all the bases.
1479  for (const auto &I : record->bases()) {
1480  if (I.isVirtual()) {
1481  // Ignore virtual bases; if we're laying out for a complete
1482  // object, we'll lay these out later.
1483  continue;
1484  }
1485 
1486  const CXXRecordDecl *base =
1487  cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
1488 
1489  // Ignore empty bases.
1490  if (base->isEmpty())
1491  continue;
1492 
1493  unsigned fieldIndex = layout.getNonVirtualBaseLLVMFieldNo(base);
1494  llvm::Type *baseType = structure->getElementType(fieldIndex);
1495  elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
1496  }
1497 
1498  // Fill in all the fields.
1499  for (const auto *Field : record->fields()) {
1500  // Fill in non-bitfields. (Bitfields always use a zero pattern, which we
1501  // will fill in later.)
1502  if (!Field->isBitField()) {
1503  unsigned fieldIndex = layout.getLLVMFieldNo(Field);
1504  elements[fieldIndex] = CGM.EmitNullConstant(Field->getType());
1505  }
1506 
1507  // For unions, stop after the first named field.
1508  if (record->isUnion()) {
1509  if (Field->getIdentifier())
1510  break;
1511  if (const auto *FieldRD =
1512  dyn_cast_or_null<RecordDecl>(Field->getType()->getAsTagDecl()))
1513  if (FieldRD->findFirstNamedDataMember())
1514  break;
1515  }
1516  }
1517 
1518  // Fill in the virtual bases, if we're working with the complete object.
1519  if (asCompleteObject) {
1520  for (const auto &I : record->vbases()) {
1521  const CXXRecordDecl *base =
1522  cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
1523 
1524  // Ignore empty bases.
1525  if (base->isEmpty())
1526  continue;
1527 
1528  unsigned fieldIndex = layout.getVirtualBaseIndex(base);
1529 
1530  // We might have already laid this field out.
1531  if (elements[fieldIndex]) continue;
1532 
1533  llvm::Type *baseType = structure->getElementType(fieldIndex);
1534  elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
1535  }
1536  }
1537 
1538  // Now go through all other fields and zero them out.
1539  for (unsigned i = 0; i != numElements; ++i) {
1540  if (!elements[i])
1541  elements[i] = llvm::Constant::getNullValue(structure->getElementType(i));
1542  }
1543 
1544  return llvm::ConstantStruct::get(structure, elements);
1545 }
1546 
1547 /// Emit the null constant for a base subobject.
1548 static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
1549  llvm::Type *baseType,
1550  const CXXRecordDecl *base) {
1551  const CGRecordLayout &baseLayout = CGM.getTypes().getCGRecordLayout(base);
1552 
1553  // Just zero out bases that don't have any pointer to data members.
1554  if (baseLayout.isZeroInitializableAsBase())
1555  return llvm::Constant::getNullValue(baseType);
1556 
1557  // Otherwise, we can just use its null constant.
1558  return EmitNullConstant(CGM, base, /*asCompleteObject=*/false);
1559 }
1560 
1562  if (getTypes().isZeroInitializable(T))
1563  return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T));
1564 
1565  if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) {
1566  llvm::ArrayType *ATy =
1567  cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T));
1568 
1569  QualType ElementTy = CAT->getElementType();
1570 
1571  llvm::Constant *Element = EmitNullConstant(ElementTy);
1572  unsigned NumElements = CAT->getSize().getZExtValue();
1573  SmallVector<llvm::Constant *, 8> Array(NumElements, Element);
1574  return llvm::ConstantArray::get(ATy, Array);
1575  }
1576 
1577  if (const RecordType *RT = T->getAs<RecordType>()) {
1578  const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1579  return ::EmitNullConstant(*this, RD, /*complete object*/ true);
1580  }
1581 
1582  assert(T->isMemberDataPointerType() &&
1583  "Should only see pointers to data members here!");
1584 
1586 }
1587 
1588 llvm::Constant *
1590  return ::EmitNullConstant(*this, Record, false);
1591 }
llvm::Constant * GetAddrOfConstantCFString(const StringLiteral *Literal)
Return a pointer to a constant CFString object for the given string.
Defines the clang::ASTContext interface.
unsigned getNumInits() const
Definition: Expr.h:3789
CastKind getCastKind() const
Definition: Expr.h:2709
virtual llvm::Constant * EmitMemberPointer(const APValue &MP, QualType MPT)
Create a member pointer for the given member pointer constant.
Definition: CGCXXABI.cpp:143
const FieldDecl * getUnionField() const
Definition: APValue.h:322
bool isFileScope() const
Definition: Expr.h:2621
APValue * evaluateValue() const
Attempt to evaluate the value of the initializer attached to this declaration, and produce notes expl...
Definition: Decl.cpp:2101
Static storage duration.
Definition: Specifiers.h:242
base_class_range bases()
Definition: DeclCXX.h:713
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition: Expr.h:2216
bool isBitField() const
Determines whether this field is a bitfield.
Definition: Decl.h:2330
llvm::Module & getModule() const
llvm::Constant * getMemberPointerConstant(const UnaryOperator *e)
IdentifierInfo * getIdentifier() const
Definition: Decl.h:163
Expr * GetTemporaryExpr() const
Retrieve the temporary-generating subexpression whose value will be materialized into a glvalue...
Definition: ExprCXX.h:3787
#define trunc(__x)
Definition: tgmath.h:1223
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:163
bool isRecordType() const
Definition: Type.h:5289
bool hasFlexibleArrayMember() const
Definition: Decl.h:3279
const llvm::DataLayout & getDataLayout() const
const Expr * getResultExpr() const
Definition: Expr.h:4514
const Expr * getInit() const
Definition: Decl.h:1068
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1075
[ARC] Consumes a retainable object pointer that has just been produced, e.g. as the return value of a...
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2147
Represents a prvalue temporary that is written into memory so that a reference can bind to it...
Definition: ExprCXX.h:3746
llvm::Constant * EmitConstantValue(const APValue &Value, QualType DestType, CodeGenFunction *CGF=nullptr)
llvm::Value * getAddress() const
Definition: CGValue.h:265
const llvm::APInt & getSize() const
Definition: Type.h:2472
unsigned getLLVMFieldNo(const FieldDecl *FD) const
Return llvm::StructType element number that corresponds to the field FD.
llvm::StructType * getLLVMType() const
Return the "complete object" LLVM type associated with this record.
APFloat & getComplexFloatReal()
Definition: APValue.h:232
field_iterator field_begin() const
Definition: Decl.cpp:3629
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
Definition: ASTContext.h:1701
llvm::Constant * GetAddrOfGlobalVar(const VarDecl *D, llvm::Type *Ty=nullptr)
CK_Dynamic - A C++ dynamic_cast.
bool isCopyOrMoveConstructor(unsigned &TypeQuals) const
Determine whether this is a copy or move constructor.
Definition: DeclCXX.cpp:1797
bool isMemberDataPointerType() const
Definition: Type.h:5265
llvm::Type * ConvertTypeForMem(QualType T)
llvm::GlobalVariable * GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *)
Return a pointer to a constant array for the given ObjCEncodeExpr node.
bool isUnionType() const
Definition: Type.cpp:390
llvm::GlobalVariable * GetAddrOfConstantStringFromLiteral(const StringLiteral *S, StringRef Name=".str")
Return a pointer to a constant array for the given string literal.
llvm::Constant * EmitConstantValueForMemory(const APValue &Value, QualType DestType, CodeGenFunction *CGF=nullptr)
bool isEmpty() const
Determine whether this is an empty class in the sense of (C++11 [meta.unary.prop]).
Definition: DeclCXX.h:1144
TagDecl * getAsTagDecl() const
Retrieves the TagDecl that this type refers to, either because the type is a TagType or because it is...
Definition: Type.cpp:1509
bool hasAttr() const
Definition: DeclBase.h:487
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:89
Converts between different integral complex types. _Complex char -> _Complex long long _Complex unsig...
bool isReferenceType() const
Definition: Type.h:5241
StringLiteral * getString()
Definition: ExprObjC.h:40
Converting between two Objective-C object types, which can occur when performing reference binding to...
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
Definition: ASTMatchers.h:258
[ARC] Causes a value of block type to be copied to the heap, if it is not already there...
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition: CharUnits.h:53
Expr * getSubExpr()
Definition: Expr.h:2713
bool hasSameUnqualifiedType(QualType T1, QualType T2) const
Determine whether the given types are equivalent after cvr-qualifiers have been removed.
Definition: ASTContext.h:1896
llvm::Constant * GetConstantArrayFromStringLiteral(const StringLiteral *E)
Return a constant array for the given string.
Converts a floating point complex to bool by comparing against 0+0i.
unsigned getVirtualBaseIndex(const CXXRecordDecl *base) const
Return the LLVM field index corresponding to the given virtual base. Only valid when operating on the...
Describes an C or C++ initializer list.
Definition: Expr.h:3759
Expr * getChosenSubExpr() const
Definition: Expr.h:3605
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:518
const LangOptions & getLangOpts() const
Definition: ASTContext.h:533
APValue Val
Val - This is the value the expression can be folded to.
Definition: Expr.h:557
uint32_t Offset
Definition: CacheTokens.cpp:43
bool isZeroInitializableAsBase() const
Check whether this struct can be C++ zero-initialized with a zeroinitializer when considered as a bas...
llvm::GlobalVariable * GetAddrOfConstantCString(const std::string &Str, const char *GlobalName=nullptr, unsigned Alignment=0)
Expr * getExprOperand() const
Definition: ExprCXX.h:589
field_range fields() const
Definition: Decl.h:3349
const ArrayType * getAsArrayType(QualType T) const
const Expr * skipRValueSubobjectAdjustments(SmallVectorImpl< const Expr * > &CommaLHS, SmallVectorImpl< SubobjectAdjustment > &Adjustments) const
Definition: Expr.cpp:54
RecordDecl * getDecl() const
Definition: Type.h:3527
Expr * IgnoreParenCasts() LLVM_READONLY
Definition: Expr.cpp:2439
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D...
uint64_t getFieldOffset(unsigned FieldNo) const
Definition: RecordLayout.h:181
TypeClass getTypeClass() const
Definition: Type.h:1486
unsigned getBuiltinCallee() const
Definition: Expr.cpp:1219
APSInt & getComplexIntReal()
Definition: APValue.h:216
A default argument (C++ [dcl.fct.default]).
Definition: ExprCXX.h:862
QualType getType() const
Definition: Decl.h:538
bool isUnnamedBitfield() const
Determines whether this is an unnamed bitfield.
Definition: Decl.h:2336
field_iterator field_end() const
Definition: Decl.h:3352
APValue & getVectorElt(unsigned I)
Definition: APValue.h:258
bool isUnion() const
Definition: Decl.h:2906
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition: CharUnits.h:58
Causes a block literal to by copied to the heap and then autoreleased.
bool EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const
APValue & getArrayFiller()
Definition: APValue.h:282
void getObjCEncodingForType(QualType T, std::string &S, const FieldDecl *Field=nullptr, QualType *NotEncodedT=nullptr) const
Emit the Objective-CC type encoding for the given type T into S.
ASTContext * Context
const Expr * getExpr() const
Get the initialization expression that will be used.
Definition: ExprCXX.h:977
Converts between different floating point complex types. _Complex float -> _Complex double...
unsigned getArrayInitializedElts() const
Definition: APValue.h:290
CGObjCRuntime & getObjCRuntime()
Return a reference to the configured Objective-C runtime.
Converts an integral complex to an integral real of the source's element type by discarding the imagi...
CGCXXABI & getCXXABI() const
StorageDuration getStorageDuration() const
Retrieve the storage duration for the materialized temporary.
Definition: ExprCXX.h:3790
Inits[]
Gets the list of initial values for linear variables.
Definition: OpenMPClause.h:303
ASTContext & getContext() const
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
Definition: RecordLayout.h:224
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition: CharUnits.h:63
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
llvm::GlobalValue::LinkageTypes getLLVMLinkageVarDefinition(const VarDecl *VD, bool IsConstant)
Returns LLVM linkage for a declarator.
Expr * getSubExpr() const
Definition: Expr.h:1699
APValue & getStructField(unsigned i)
Definition: APValue.h:311
virtual llvm::Constant * EmitMemberFunctionPointer(const CXXMethodDecl *MD)
Create a member pointer for the given method.
Definition: CGCXXABI.cpp:133
Converts from an integral complex to a floating complex. _Complex unsigned -> _Complex float...
Represents a reference to a non-type template parameter that has been substituted with a template arg...
Definition: ExprCXX.h:3558
llvm::Constant * GetAddrOfUuidDescriptor(const CXXUuidofExpr *E)
Get the address of a uuid descriptor .
APSInt & getComplexIntImag()
Definition: APValue.h:224
The result type of a method or function.
InitListExpr * getUpdater() const
Definition: Expr.h:4332
uint64_t getFieldOffset(const ValueDecl *FD) const
Get the offset of a FieldDecl or IndirectFieldDecl, in bits.
APValue & getStructBase(unsigned i)
Definition: APValue.h:307
llvm::Constant * GetWeakRefReference(const ValueDecl *VD)
Get a reference to the target of VD.
llvm::Constant * getOrCreateStaticVarDecl(const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage)
Definition: CGDecl.cpp:175
APValue & getArrayInitializedElt(unsigned I)
Definition: APValue.h:271
virtual llvm::Value * EmitMemberPointerConversion(CodeGenFunction &CGF, const CastExpr *E, llvm::Value *Src)
Definition: CGCXXABI.cpp:98
#define false
Definition: stdbool.h:33
llvm::Constant * GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *E)
llvm::Constant * EmitConstantExpr(const Expr *E, QualType DestType, CodeGenFunction *CGF=nullptr)
CharUnits getSize() const
getSize - Get the record size in characters.
Definition: RecordLayout.h:174
unsigned getBitWidthValue(const ASTContext &Ctx) const
Definition: Decl.cpp:3344
const CXXRecordDecl * getPrimaryBase() const
getPrimaryBase - Get the primary base for this record.
Definition: RecordLayout.h:209
const AddrLabelExpr * getAddrLabelDiffLHS() const
Definition: APValue.h:338
APValue & getUnionValue()
Definition: APValue.h:326
bool isConstant(ASTContext &Ctx) const
Definition: Type.h:703
ValueKind getKind() const
Definition: APValue.h:180
APFloat & getFloat()
Definition: APValue.h:208
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1717
llvm::Constant * EmitNullConstantForBase(const CXXRecordDecl *Record)
const ConstantArrayType * getAsConstantArrayType(QualType T) const
Definition: ASTContext.h:2003
llvm::Constant * GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH=false)
Get the address of the RTTI descriptor for the given type.
Converts from an integral real to an integral complex whose element type matches the source...
const T * castAs() const
Definition: Type.h:5586
const AddrLabelExpr * getAddrLabelDiffRHS() const
Definition: APValue.h:342
bool operator<(DeclarationName LHS, DeclarationName RHS)
llvm::Constant * GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E, const Expr *Inner)
Returns a pointer to a global variable representing a temporary with static or thread storage duratio...
virtual llvm::Constant * EmitNullMemberPointer(const MemberPointerType *MPT)
Create a null member pointer of the given type.
Definition: CGCXXABI.cpp:129
Expr * getArrayFiller()
If this initializer list initializes an array with more elements than there are initializers in the l...
Definition: Expr.h:3840
Represents a C11 generic selection.
Definition: Expr.h:4446
AddrLabelExpr - The GNU address of label extension, representing &&label.
Definition: Expr.h:3357
QualType getType() const
Definition: Expr.h:125
Converts a floating point complex to floating point real of the source's element type. Just discards the imaginary component. _Complex long double -> long double.
const Expr * getExpr() const
Definition: ExprCXX.h:911
QualType getTypeOperand(ASTContext &Context) const
Retrieves the type operand of this typeid() expression after various required adjustments (removing r...
Definition: ExprCXX.cpp:43
EvalResult is a struct with detailed info about an evaluated expression.
Definition: Expr.h:555
llvm::Constant * GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty=0, bool ForVTable=false, bool DontDefer=false)
Converts an integral complex to bool by comparing against 0+0i.
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition: CharUnits.h:116
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
Definition: ASTMatchers.h:1639
unsigned getNonVirtualBaseLLVMFieldNo(const CXXRecordDecl *RD) const
static llvm::Constant * EmitNullConstantForBase(CodeGenModule &CGM, llvm::Type *baseType, const CXXRecordDecl *base)
Emit the null constant for a base subobject.
llvm::Constant * EmitNullConstant(QualType T)
A conversion of a floating point real to a floating point complex of the original type...
unsigned getNumArgs() const
Definition: ExprCXX.h:1198
[ARC] Reclaim a retainable object pointer object that may have been produced and autoreleased as part...
const T * getAs() const
Definition: Type.h:5555
llvm::PointerUnion< const ValueDecl *, const Expr * > LValueBase
Definition: APValue.h:56
[ARC] Produces a retainable object pointer so that it may be consumed, e.g. by being passed to a cons...
virtual llvm::Constant * EmitMemberDataPointer(const MemberPointerType *MPT, CharUnits offset)
Create a member pointer for the given field.
Definition: CGCXXABI.cpp:138
Converts from T to _Atomic(T).
Expr * getArg(unsigned Arg)
Return the specified argument.
Definition: ExprCXX.h:1201
CXXConstructorDecl * getConstructor() const
Definition: ExprCXX.h:1137
llvm::Constant * GetAddrOfGlobalBlock(const BlockExpr *BE, const char *)
Gets the address of a block which requires no captures.
Definition: CGBlocks.cpp:1041
bool isTrivial() const
Definition: Decl.h:1800
Converts from a floating complex to an integral complex. _Complex float -> _Complex int...
uint64_t getCharWidth() const
Return the size of the character type, in bits.
Definition: ASTContext.h:1705
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
Definition: Linkage.h:33
Represents a base class of a C++ class.
Definition: DeclCXX.h:157
const Expr * getInitializer() const
Definition: Expr.h:2617
llvm::Constant * EmitConstantInit(const VarDecl &D, CodeGenFunction *CGF=nullptr)
virtual llvm::Constant * GenerateConstantString(const StringLiteral *)=0
Generate a constant string object.
bool isDefaultConstructor() const
Definition: DeclCXX.cpp:1777
A use of a default initializer in a constructor or in aggregate initialization.
Definition: ExprCXX.h:952
const Expr * getSubExpr() const
Definition: Expr.h:1638
APFloat & getComplexFloatImag()
Definition: APValue.h:240
const LValueBase getLValueBase() const
Definition: APValue.cpp:553
Represents a C++ struct/union/class.
Definition: DeclCXX.h:285
BoundNodesTreeBuilder *const Builder
QualType getEncodedType() const
Definition: ExprObjC.h:377
Converts from _Atomic(T) to T.
bool isArrayType() const
Definition: Type.h:5271
LValue EmitPredefinedLValue(const PredefinedExpr *E)
Definition: CGExpr.cpp:2109
bool isInt() const
Definition: APValue.h:182
bool isStringLiteralInit() const
Definition: Expr.cpp:1935
Expr * getBase() const
Definition: Expr.h:4329
unsigned getTargetAddressSpace(QualType T) const
Definition: ASTContext.h:2089
QualType getElementType() const
Definition: Type.h:2434
const Expr * getInit(unsigned Init) const
Definition: Expr.h:3794
FieldDecl * getInitializedFieldInUnion()
If this initializes a union, specifies which field in the union to initialize.
Definition: Expr.h:3858
int64_t toBits(CharUnits CharSize) const
Convert a size in characters to a size in bits.
static llvm::Constant * EmitNullConstant(CodeGenModule &CGM, const CXXRecordDecl *record, bool asCompleteObject)
llvm::BlockAddress * GetAddrOfLabel(const LabelDecl *L)
APSInt & getInt()
Definition: APValue.h:200
unsigned getArraySize() const
Definition: APValue.h:294
bool EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const
bool isTypeOperand() const
Definition: ExprCXX.h:572
llvm::StructType * getBaseSubobjectLLVMType() const
Return the "base subobject" LLVM type associated with this record.
CharUnits RoundUpToAlignment(const CharUnits &Align) const
Definition: CharUnits.h:168
const CGRecordLayout & getCGRecordLayout(const RecordDecl *)
getCGRecordLayout - Return record layout info for the given record decl.
base_class_range vbases()
Definition: DeclCXX.h:730
unsigned getVectorLength() const
Definition: APValue.h:266
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
Defines enum values for all the target-independent builtin functions.
Represents an implicitly-generated value initialization of an object of a given type.
Definition: Expr.h:4352
bool hasLocalStorage() const
Definition: Decl.h:887
bool hasArrayFiller() const
Definition: APValue.h:279
CharUnits & getLValueOffset()
Definition: APValue.cpp:563