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