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;
44   SmallVector<llvm::Constant *, 32> Elements;
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:
ConstStructBuilder(CodeGenModule & CGM,CodeGenFunction * CGF)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 
getAlignment(const llvm::Constant * C) const82   CharUnits getAlignment(const llvm::Constant *C) const {
83     if (Packed)  return CharUnits::One();
84     return CharUnits::fromQuantity(
85         CGM.getDataLayout().getABITypeAlignment(C->getType()));
86   }
87 
getSizeInChars(const llvm::Constant * C) const88   CharUnits getSizeInChars(const llvm::Constant *C) const {
89     return CharUnits::fromQuantity(
90         CGM.getDataLayout().getTypeAllocSize(C->getType()));
91   }
92 };
93 
94 void ConstStructBuilder::
AppendField(const FieldDecl * Field,uint64_t FieldOffset,llvm::Constant * InitCst)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::
AppendBytes(CharUnits FieldOffsetInChars,llvm::Constant * InitCst)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 
AppendBitField(const FieldDecl * Field,uint64_t FieldOffset,llvm::ConstantInt * CI)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 
AppendPadding(CharUnits PadSize)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 
AppendTailPadding(CharUnits RecordSize)320 void ConstStructBuilder::AppendTailPadding(CharUnits RecordSize) {
321   assert(NextFieldOffsetInChars <= RecordSize &&
322          "Size mismatch!");
323 
324   AppendPadding(RecordSize - NextFieldOffsetInChars);
325 }
326 
ConvertStructToPacked()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 
Build(InitListExpr * ILE)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 {
BaseInfo__anon3adcc1930111::__anon3adcc1930211::BaseInfo414   BaseInfo(const CXXRecordDecl *Decl, CharUnits Offset, unsigned Index)
415     : Decl(Decl), Offset(Offset), Index(Index) {
416   }
417 
418   const CXXRecordDecl *Decl;
419   CharUnits Offset;
420   unsigned Index;
421 
operator <__anon3adcc1930111::__anon3adcc1930211::BaseInfo422   bool operator<(const BaseInfo &O) const { return Offset < O.Offset; }
423 };
424 }
425 
Build(const APValue & Val,const RecordDecl * RD,bool IsPrimaryBase,const CXXRecordDecl * VTableClass,CharUnits Offset)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.
443     SmallVector<BaseInfo, 8> Bases;
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 
Finalize(QualType Ty)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 
BuildStruct(CodeGenModule & CGM,CodeGenFunction * CGF,ConstExprEmitter * Emitter,llvm::ConstantStruct * Base,InitListExpr * Updater)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 
BuildStruct(CodeGenModule & CGM,CodeGenFunction * CGF,InitListExpr * ILE)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 
BuildStruct(CodeGenModule & CGM,CodeGenFunction * CGF,const APValue & Val,QualType ValTy)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:
ConstExprEmitter(CodeGenModule & cgm,CodeGenFunction * cgf)605   ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf)
606     : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()) {
607   }
608 
609   //===--------------------------------------------------------------------===//
610   //                            Visitor Methods
611   //===--------------------------------------------------------------------===//
612 
VisitStmt(Stmt * S)613   llvm::Constant *VisitStmt(Stmt *S) {
614     return nullptr;
615   }
616 
VisitParenExpr(ParenExpr * PE)617   llvm::Constant *VisitParenExpr(ParenExpr *PE) {
618     return Visit(PE->getSubExpr());
619   }
620 
621   llvm::Constant *
VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr * PE)622   VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) {
623     return Visit(PE->getReplacement());
624   }
625 
VisitGenericSelectionExpr(GenericSelectionExpr * GE)626   llvm::Constant *VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
627     return Visit(GE->getResultExpr());
628   }
629 
VisitChooseExpr(ChooseExpr * CE)630   llvm::Constant *VisitChooseExpr(ChooseExpr *CE) {
631     return Visit(CE->getChosenSubExpr());
632   }
633 
VisitCompoundLiteralExpr(CompoundLiteralExpr * E)634   llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
635     return Visit(E->getInitializer());
636   }
637 
VisitCastExpr(CastExpr * E)638   llvm::Constant *VisitCastExpr(CastExpr* E) {
639     if (const auto *ECE = dyn_cast<ExplicitCastExpr>(E))
640       CGM.EmitExplicitCastExprType(ECE, CGF);
641     Expr *subExpr = E->getSubExpr();
642     llvm::Constant *C = CGM.EmitConstantExpr(subExpr, subExpr->getType(), CGF);
643     if (!C) return nullptr;
644 
645     llvm::Type *destType = ConvertType(E->getType());
646 
647     switch (E->getCastKind()) {
648     case CK_ToUnion: {
649       // GCC cast to union extension
650       assert(E->getType()->isUnionType() &&
651              "Destination type is not union type!");
652 
653       // Build a struct with the union sub-element as the first member,
654       // and padded to the appropriate size
655       SmallVector<llvm::Constant*, 2> Elts;
656       SmallVector<llvm::Type*, 2> Types;
657       Elts.push_back(C);
658       Types.push_back(C->getType());
659       unsigned CurSize = CGM.getDataLayout().getTypeAllocSize(C->getType());
660       unsigned TotalSize = CGM.getDataLayout().getTypeAllocSize(destType);
661 
662       assert(CurSize <= TotalSize && "Union size mismatch!");
663       if (unsigned NumPadBytes = TotalSize - CurSize) {
664         llvm::Type *Ty = CGM.Int8Ty;
665         if (NumPadBytes > 1)
666           Ty = llvm::ArrayType::get(Ty, NumPadBytes);
667 
668         Elts.push_back(llvm::UndefValue::get(Ty));
669         Types.push_back(Ty);
670       }
671 
672       llvm::StructType* STy =
673         llvm::StructType::get(C->getType()->getContext(), Types, false);
674       return llvm::ConstantStruct::get(STy, Elts);
675     }
676 
677     case CK_AddressSpaceConversion:
678       return llvm::ConstantExpr::getAddrSpaceCast(C, destType);
679 
680     case CK_LValueToRValue:
681     case CK_AtomicToNonAtomic:
682     case CK_NonAtomicToAtomic:
683     case CK_NoOp:
684     case CK_ConstructorConversion:
685       return C;
686 
687     case CK_Dependent: llvm_unreachable("saw dependent cast!");
688 
689     case CK_BuiltinFnToFnPtr:
690       llvm_unreachable("builtin functions are handled elsewhere");
691 
692     case CK_ReinterpretMemberPointer:
693     case CK_DerivedToBaseMemberPointer:
694     case CK_BaseToDerivedMemberPointer:
695       return CGM.getCXXABI().EmitMemberPointerConversion(E, C);
696 
697     // These will never be supported.
698     case CK_ObjCObjectLValueCast:
699     case CK_ARCProduceObject:
700     case CK_ARCConsumeObject:
701     case CK_ARCReclaimReturnedObject:
702     case CK_ARCExtendBlockObject:
703     case CK_CopyAndAutoreleaseBlockObject:
704       return nullptr;
705 
706     // These don't need to be handled here because Evaluate knows how to
707     // evaluate them in the cases where they can be folded.
708     case CK_BitCast:
709     case CK_ToVoid:
710     case CK_Dynamic:
711     case CK_LValueBitCast:
712     case CK_NullToMemberPointer:
713     case CK_UserDefinedConversion:
714     case CK_CPointerToObjCPointerCast:
715     case CK_BlockPointerToObjCPointerCast:
716     case CK_AnyPointerToBlockPointerCast:
717     case CK_ArrayToPointerDecay:
718     case CK_FunctionToPointerDecay:
719     case CK_BaseToDerived:
720     case CK_DerivedToBase:
721     case CK_UncheckedDerivedToBase:
722     case CK_MemberPointerToBoolean:
723     case CK_VectorSplat:
724     case CK_FloatingRealToComplex:
725     case CK_FloatingComplexToReal:
726     case CK_FloatingComplexToBoolean:
727     case CK_FloatingComplexCast:
728     case CK_FloatingComplexToIntegralComplex:
729     case CK_IntegralRealToComplex:
730     case CK_IntegralComplexToReal:
731     case CK_IntegralComplexToBoolean:
732     case CK_IntegralComplexCast:
733     case CK_IntegralComplexToFloatingComplex:
734     case CK_PointerToIntegral:
735     case CK_PointerToBoolean:
736     case CK_NullToPointer:
737     case CK_IntegralCast:
738     case CK_IntegralToPointer:
739     case CK_IntegralToBoolean:
740     case CK_IntegralToFloating:
741     case CK_FloatingToIntegral:
742     case CK_FloatingToBoolean:
743     case CK_FloatingCast:
744     case CK_ZeroToOCLEvent:
745       return nullptr;
746     }
747     llvm_unreachable("Invalid CastKind");
748   }
749 
VisitCXXDefaultArgExpr(CXXDefaultArgExpr * DAE)750   llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
751     return Visit(DAE->getExpr());
752   }
753 
VisitCXXDefaultInitExpr(CXXDefaultInitExpr * DIE)754   llvm::Constant *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
755     // No need for a DefaultInitExprScope: we don't handle 'this' in a
756     // constant expression.
757     return Visit(DIE->getExpr());
758   }
759 
VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr * E)760   llvm::Constant *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
761     return Visit(E->GetTemporaryExpr());
762   }
763 
EmitArrayInitialization(InitListExpr * ILE)764   llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) {
765     if (ILE->isStringLiteralInit())
766       return Visit(ILE->getInit(0));
767 
768     llvm::ArrayType *AType =
769         cast<llvm::ArrayType>(ConvertType(ILE->getType()));
770     llvm::Type *ElemTy = AType->getElementType();
771     unsigned NumInitElements = ILE->getNumInits();
772     unsigned NumElements = AType->getNumElements();
773 
774     // Initialising an array requires us to automatically
775     // initialise any elements that have not been initialised explicitly
776     unsigned NumInitableElts = std::min(NumInitElements, NumElements);
777 
778     // Initialize remaining array elements.
779     // FIXME: This doesn't handle member pointers correctly!
780     llvm::Constant *fillC;
781     if (Expr *filler = ILE->getArrayFiller())
782       fillC = CGM.EmitConstantExpr(filler, filler->getType(), CGF);
783     else
784       fillC = llvm::Constant::getNullValue(ElemTy);
785     if (!fillC)
786       return nullptr;
787 
788     // Try to use a ConstantAggregateZero if we can.
789     if (fillC->isNullValue() && !NumInitableElts)
790       return llvm::ConstantAggregateZero::get(AType);
791 
792     // Copy initializer elements.
793     std::vector<llvm::Constant*> Elts;
794     Elts.reserve(NumInitableElts + NumElements);
795 
796     bool RewriteType = false;
797     for (unsigned i = 0; i < NumInitableElts; ++i) {
798       Expr *Init = ILE->getInit(i);
799       llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF);
800       if (!C)
801         return nullptr;
802       RewriteType |= (C->getType() != ElemTy);
803       Elts.push_back(C);
804     }
805 
806     RewriteType |= (fillC->getType() != ElemTy);
807     Elts.resize(NumElements, fillC);
808 
809     if (RewriteType) {
810       // FIXME: Try to avoid packing the array
811       std::vector<llvm::Type*> Types;
812       Types.reserve(NumInitableElts + NumElements);
813       for (unsigned i = 0, e = Elts.size(); i < e; ++i)
814         Types.push_back(Elts[i]->getType());
815       llvm::StructType *SType = llvm::StructType::get(AType->getContext(),
816                                                             Types, true);
817       return llvm::ConstantStruct::get(SType, Elts);
818     }
819 
820     return llvm::ConstantArray::get(AType, Elts);
821   }
822 
EmitRecordInitialization(InitListExpr * ILE)823   llvm::Constant *EmitRecordInitialization(InitListExpr *ILE) {
824     return ConstStructBuilder::BuildStruct(CGM, CGF, ILE);
825   }
826 
VisitImplicitValueInitExpr(ImplicitValueInitExpr * E)827   llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E) {
828     return CGM.EmitNullConstant(E->getType());
829   }
830 
VisitInitListExpr(InitListExpr * ILE)831   llvm::Constant *VisitInitListExpr(InitListExpr *ILE) {
832     if (ILE->getType()->isArrayType())
833       return EmitArrayInitialization(ILE);
834 
835     if (ILE->getType()->isRecordType())
836       return EmitRecordInitialization(ILE);
837 
838     return nullptr;
839   }
840 
EmitDesignatedInitUpdater(llvm::Constant * Base,InitListExpr * Updater)841   llvm::Constant *EmitDesignatedInitUpdater(llvm::Constant *Base,
842                                             InitListExpr *Updater) {
843     QualType ExprType = Updater->getType();
844 
845     if (ExprType->isArrayType()) {
846       llvm::ArrayType *AType = cast<llvm::ArrayType>(ConvertType(ExprType));
847       llvm::Type *ElemType = AType->getElementType();
848 
849       unsigned NumInitElements = Updater->getNumInits();
850       unsigned NumElements = AType->getNumElements();
851 
852       std::vector<llvm::Constant *> Elts;
853       Elts.reserve(NumElements);
854 
855       if (llvm::ConstantDataArray *DataArray =
856             dyn_cast<llvm::ConstantDataArray>(Base))
857         for (unsigned i = 0; i != NumElements; ++i)
858           Elts.push_back(DataArray->getElementAsConstant(i));
859       else if (llvm::ConstantArray *Array =
860                  dyn_cast<llvm::ConstantArray>(Base))
861         for (unsigned i = 0; i != NumElements; ++i)
862           Elts.push_back(Array->getOperand(i));
863       else
864         return nullptr; // FIXME: other array types not implemented
865 
866       llvm::Constant *fillC = nullptr;
867       if (Expr *filler = Updater->getArrayFiller())
868         if (!isa<NoInitExpr>(filler))
869           fillC = CGM.EmitConstantExpr(filler, filler->getType(), CGF);
870       bool RewriteType = (fillC && fillC->getType() != ElemType);
871 
872       for (unsigned i = 0; i != NumElements; ++i) {
873         Expr *Init = nullptr;
874         if (i < NumInitElements)
875           Init = Updater->getInit(i);
876 
877         if (!Init && fillC)
878           Elts[i] = fillC;
879         else if (!Init || isa<NoInitExpr>(Init))
880           ; // Do nothing.
881         else if (InitListExpr *ChildILE = dyn_cast<InitListExpr>(Init))
882           Elts[i] = EmitDesignatedInitUpdater(Elts[i], ChildILE);
883         else
884           Elts[i] = CGM.EmitConstantExpr(Init, Init->getType(), CGF);
885 
886        if (!Elts[i])
887           return nullptr;
888         RewriteType |= (Elts[i]->getType() != ElemType);
889       }
890 
891       if (RewriteType) {
892         std::vector<llvm::Type *> Types;
893         Types.reserve(NumElements);
894         for (unsigned i = 0; i != NumElements; ++i)
895           Types.push_back(Elts[i]->getType());
896         llvm::StructType *SType = llvm::StructType::get(AType->getContext(),
897                                                         Types, true);
898         return llvm::ConstantStruct::get(SType, Elts);
899       }
900 
901       return llvm::ConstantArray::get(AType, Elts);
902     }
903 
904     if (ExprType->isRecordType())
905       return ConstStructBuilder::BuildStruct(CGM, CGF, this,
906                  dyn_cast<llvm::ConstantStruct>(Base), Updater);
907 
908     return nullptr;
909   }
910 
VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr * E)911   llvm::Constant *VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr *E) {
912     return EmitDesignatedInitUpdater(
913                CGM.EmitConstantExpr(E->getBase(), E->getType(), CGF),
914                E->getUpdater());
915   }
916 
VisitCXXConstructExpr(CXXConstructExpr * E)917   llvm::Constant *VisitCXXConstructExpr(CXXConstructExpr *E) {
918     if (!E->getConstructor()->isTrivial())
919       return nullptr;
920 
921     QualType Ty = E->getType();
922 
923     // FIXME: We should not have to call getBaseElementType here.
924     const RecordType *RT =
925       CGM.getContext().getBaseElementType(Ty)->getAs<RecordType>();
926     const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
927 
928     // If the class doesn't have a trivial destructor, we can't emit it as a
929     // constant expr.
930     if (!RD->hasTrivialDestructor())
931       return nullptr;
932 
933     // Only copy and default constructors can be trivial.
934 
935 
936     if (E->getNumArgs()) {
937       assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument");
938       assert(E->getConstructor()->isCopyOrMoveConstructor() &&
939              "trivial ctor has argument but isn't a copy/move ctor");
940 
941       Expr *Arg = E->getArg(0);
942       assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) &&
943              "argument to copy ctor is of wrong type");
944 
945       return Visit(Arg);
946     }
947 
948     return CGM.EmitNullConstant(Ty);
949   }
950 
VisitStringLiteral(StringLiteral * E)951   llvm::Constant *VisitStringLiteral(StringLiteral *E) {
952     return CGM.GetConstantArrayFromStringLiteral(E);
953   }
954 
VisitObjCEncodeExpr(ObjCEncodeExpr * E)955   llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
956     // This must be an @encode initializing an array in a static initializer.
957     // Don't emit it as the address of the string, emit the string data itself
958     // as an inline array.
959     std::string Str;
960     CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str);
961     QualType T = E->getType();
962     if (T->getTypeClass() == Type::TypeOfExpr)
963       T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
964     const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
965 
966     // Resize the string to the right size, adding zeros at the end, or
967     // truncating as needed.
968     Str.resize(CAT->getSize().getZExtValue(), '\0');
969     return llvm::ConstantDataArray::getString(VMContext, Str, false);
970   }
971 
VisitUnaryExtension(const UnaryOperator * E)972   llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) {
973     return Visit(E->getSubExpr());
974   }
975 
976   // Utility methods
ConvertType(QualType T)977   llvm::Type *ConvertType(QualType T) {
978     return CGM.getTypes().ConvertType(T);
979   }
980 
981 public:
EmitLValue(APValue::LValueBase LVBase)982   ConstantAddress EmitLValue(APValue::LValueBase LVBase) {
983     if (const ValueDecl *Decl = LVBase.dyn_cast<const ValueDecl*>()) {
984       if (Decl->hasAttr<WeakRefAttr>())
985         return CGM.GetWeakRefReference(Decl);
986       if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl))
987         return ConstantAddress(CGM.GetAddrOfFunction(FD), CharUnits::One());
988       if (const VarDecl* VD = dyn_cast<VarDecl>(Decl)) {
989         // We can never refer to a variable with local storage.
990         if (!VD->hasLocalStorage()) {
991           CharUnits Align = CGM.getContext().getDeclAlign(VD);
992           if (VD->isFileVarDecl() || VD->hasExternalStorage())
993             return ConstantAddress(CGM.GetAddrOfGlobalVar(VD), Align);
994           else if (VD->isLocalVarDecl()) {
995             auto Ptr = CGM.getOrCreateStaticVarDecl(
996                 *VD, CGM.getLLVMLinkageVarDefinition(VD, /*isConstant=*/false));
997             return ConstantAddress(Ptr, Align);
998           }
999         }
1000       }
1001       return ConstantAddress::invalid();
1002     }
1003 
1004     Expr *E = const_cast<Expr*>(LVBase.get<const Expr*>());
1005     switch (E->getStmtClass()) {
1006     default: break;
1007     case Expr::CompoundLiteralExprClass: {
1008       // Note that due to the nature of compound literals, this is guaranteed
1009       // to be the only use of the variable, so we just generate it here.
1010       CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E);
1011       llvm::Constant* C = CGM.EmitConstantExpr(CLE->getInitializer(),
1012                                                CLE->getType(), CGF);
1013       // FIXME: "Leaked" on failure.
1014       if (!C) return ConstantAddress::invalid();
1015 
1016       CharUnits Align = CGM.getContext().getTypeAlignInChars(E->getType());
1017 
1018       auto GV = new llvm::GlobalVariable(CGM.getModule(), C->getType(),
1019                                      E->getType().isConstant(CGM.getContext()),
1020                                      llvm::GlobalValue::InternalLinkage,
1021                                      C, ".compoundliteral", nullptr,
1022                                      llvm::GlobalVariable::NotThreadLocal,
1023                           CGM.getContext().getTargetAddressSpace(E->getType()));
1024       GV->setAlignment(Align.getQuantity());
1025       return ConstantAddress(GV, Align);
1026     }
1027     case Expr::StringLiteralClass:
1028       return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E));
1029     case Expr::ObjCEncodeExprClass:
1030       return CGM.GetAddrOfConstantStringFromObjCEncode(cast<ObjCEncodeExpr>(E));
1031     case Expr::ObjCStringLiteralClass: {
1032       ObjCStringLiteral* SL = cast<ObjCStringLiteral>(E);
1033       ConstantAddress C =
1034           CGM.getObjCRuntime().GenerateConstantString(SL->getString());
1035       return C.getElementBitCast(ConvertType(E->getType()));
1036     }
1037     case Expr::PredefinedExprClass: {
1038       unsigned Type = cast<PredefinedExpr>(E)->getIdentType();
1039       if (CGF) {
1040         LValue Res = CGF->EmitPredefinedLValue(cast<PredefinedExpr>(E));
1041         return cast<ConstantAddress>(Res.getAddress());
1042       } else if (Type == PredefinedExpr::PrettyFunction) {
1043         return CGM.GetAddrOfConstantCString("top level", ".tmp");
1044       }
1045 
1046       return CGM.GetAddrOfConstantCString("", ".tmp");
1047     }
1048     case Expr::AddrLabelExprClass: {
1049       assert(CGF && "Invalid address of label expression outside function.");
1050       llvm::Constant *Ptr =
1051         CGF->GetAddrOfLabel(cast<AddrLabelExpr>(E)->getLabel());
1052       Ptr = llvm::ConstantExpr::getBitCast(Ptr, ConvertType(E->getType()));
1053       return ConstantAddress(Ptr, CharUnits::One());
1054     }
1055     case Expr::CallExprClass: {
1056       CallExpr* CE = cast<CallExpr>(E);
1057       unsigned builtin = CE->getBuiltinCallee();
1058       if (builtin !=
1059             Builtin::BI__builtin___CFStringMakeConstantString &&
1060           builtin !=
1061             Builtin::BI__builtin___NSStringMakeConstantString)
1062         break;
1063       const Expr *Arg = CE->getArg(0)->IgnoreParenCasts();
1064       const StringLiteral *Literal = cast<StringLiteral>(Arg);
1065       if (builtin ==
1066             Builtin::BI__builtin___NSStringMakeConstantString) {
1067         return CGM.getObjCRuntime().GenerateConstantString(Literal);
1068       }
1069       // FIXME: need to deal with UCN conversion issues.
1070       return CGM.GetAddrOfConstantCFString(Literal);
1071     }
1072     case Expr::BlockExprClass: {
1073       std::string FunctionName;
1074       if (CGF)
1075         FunctionName = CGF->CurFn->getName();
1076       else
1077         FunctionName = "global";
1078 
1079       // This is not really an l-value.
1080       llvm::Constant *Ptr =
1081         CGM.GetAddrOfGlobalBlock(cast<BlockExpr>(E), FunctionName.c_str());
1082       return ConstantAddress(Ptr, CGM.getPointerAlign());
1083     }
1084     case Expr::CXXTypeidExprClass: {
1085       CXXTypeidExpr *Typeid = cast<CXXTypeidExpr>(E);
1086       QualType T;
1087       if (Typeid->isTypeOperand())
1088         T = Typeid->getTypeOperand(CGM.getContext());
1089       else
1090         T = Typeid->getExprOperand()->getType();
1091       return ConstantAddress(CGM.GetAddrOfRTTIDescriptor(T),
1092                              CGM.getPointerAlign());
1093     }
1094     case Expr::CXXUuidofExprClass: {
1095       return CGM.GetAddrOfUuidDescriptor(cast<CXXUuidofExpr>(E));
1096     }
1097     case Expr::MaterializeTemporaryExprClass: {
1098       MaterializeTemporaryExpr *MTE = cast<MaterializeTemporaryExpr>(E);
1099       assert(MTE->getStorageDuration() == SD_Static);
1100       SmallVector<const Expr *, 2> CommaLHSs;
1101       SmallVector<SubobjectAdjustment, 2> Adjustments;
1102       const Expr *Inner = MTE->GetTemporaryExpr()
1103           ->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments);
1104       return CGM.GetAddrOfGlobalTemporary(MTE, Inner);
1105     }
1106     }
1107 
1108     return ConstantAddress::invalid();
1109   }
1110 };
1111 
1112 }  // end anonymous namespace.
1113 
Build(ConstExprEmitter * Emitter,llvm::ConstantStruct * Base,InitListExpr * Updater)1114 bool ConstStructBuilder::Build(ConstExprEmitter *Emitter,
1115                                llvm::ConstantStruct *Base,
1116                                InitListExpr *Updater) {
1117   assert(Base && "base expression should not be empty");
1118 
1119   QualType ExprType = Updater->getType();
1120   RecordDecl *RD = ExprType->getAs<RecordType>()->getDecl();
1121   const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1122   const llvm::StructLayout *BaseLayout = CGM.getDataLayout().getStructLayout(
1123                                            Base->getType());
1124   unsigned FieldNo = -1;
1125   unsigned ElementNo = 0;
1126 
1127   for (FieldDecl *Field : RD->fields()) {
1128     ++FieldNo;
1129 
1130     if (RD->isUnion() && Updater->getInitializedFieldInUnion() != Field)
1131       continue;
1132 
1133     // Skip anonymous bitfields.
1134     if (Field->isUnnamedBitfield())
1135       continue;
1136 
1137     llvm::Constant *EltInit = Base->getOperand(ElementNo);
1138 
1139     // Bail out if the type of the ConstantStruct does not have the same layout
1140     // as the type of the InitListExpr.
1141     if (CGM.getTypes().ConvertType(Field->getType()) != EltInit->getType() ||
1142         Layout.getFieldOffset(ElementNo) !=
1143           BaseLayout->getElementOffsetInBits(ElementNo))
1144       return false;
1145 
1146     // Get the initializer. If we encounter an empty field or a NoInitExpr,
1147     // we use values from the base expression.
1148     Expr *Init = nullptr;
1149     if (ElementNo < Updater->getNumInits())
1150       Init = Updater->getInit(ElementNo);
1151 
1152     if (!Init || isa<NoInitExpr>(Init))
1153       ; // Do nothing.
1154     else if (InitListExpr *ChildILE = dyn_cast<InitListExpr>(Init))
1155       EltInit = Emitter->EmitDesignatedInitUpdater(EltInit, ChildILE);
1156     else
1157       EltInit = CGM.EmitConstantExpr(Init, Field->getType(), CGF);
1158 
1159     ++ElementNo;
1160 
1161     if (!EltInit)
1162       return false;
1163 
1164     if (!Field->isBitField())
1165       AppendField(Field, Layout.getFieldOffset(FieldNo), EltInit);
1166     else if (llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(EltInit))
1167       AppendBitField(Field, Layout.getFieldOffset(FieldNo), CI);
1168     else
1169       // Initializing a bitfield with a non-trivial constant?
1170       return false;
1171   }
1172 
1173   return true;
1174 }
1175 
EmitConstantInit(const VarDecl & D,CodeGenFunction * CGF)1176 llvm::Constant *CodeGenModule::EmitConstantInit(const VarDecl &D,
1177                                                 CodeGenFunction *CGF) {
1178   // Make a quick check if variable can be default NULL initialized
1179   // and avoid going through rest of code which may do, for c++11,
1180   // initialization of memory to all NULLs.
1181   if (!D.hasLocalStorage()) {
1182     QualType Ty = D.getType();
1183     if (Ty->isArrayType())
1184       Ty = Context.getBaseElementType(Ty);
1185     if (Ty->isRecordType())
1186       if (const CXXConstructExpr *E =
1187           dyn_cast_or_null<CXXConstructExpr>(D.getInit())) {
1188         const CXXConstructorDecl *CD = E->getConstructor();
1189         if (CD->isTrivial() && CD->isDefaultConstructor())
1190           return EmitNullConstant(D.getType());
1191       }
1192   }
1193 
1194   if (const APValue *Value = D.evaluateValue())
1195     return EmitConstantValueForMemory(*Value, D.getType(), CGF);
1196 
1197   // FIXME: Implement C++11 [basic.start.init]p2: if the initializer of a
1198   // reference is a constant expression, and the reference binds to a temporary,
1199   // then constant initialization is performed. ConstExprEmitter will
1200   // incorrectly emit a prvalue constant in this case, and the calling code
1201   // interprets that as the (pointer) value of the reference, rather than the
1202   // desired value of the referee.
1203   if (D.getType()->isReferenceType())
1204     return nullptr;
1205 
1206   const Expr *E = D.getInit();
1207   assert(E && "No initializer to emit");
1208 
1209   llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E));
1210   if (C && C->getType()->isIntegerTy(1)) {
1211     llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
1212     C = llvm::ConstantExpr::getZExt(C, BoolTy);
1213   }
1214   return C;
1215 }
1216 
EmitConstantExpr(const Expr * E,QualType DestType,CodeGenFunction * CGF)1217 llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E,
1218                                                 QualType DestType,
1219                                                 CodeGenFunction *CGF) {
1220   Expr::EvalResult Result;
1221 
1222   bool Success = false;
1223 
1224   if (DestType->isReferenceType())
1225     Success = E->EvaluateAsLValue(Result, Context);
1226   else
1227     Success = E->EvaluateAsRValue(Result, Context);
1228 
1229   llvm::Constant *C = nullptr;
1230   if (Success && !Result.HasSideEffects)
1231     C = EmitConstantValue(Result.Val, DestType, CGF);
1232   else
1233     C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E));
1234 
1235   if (C && C->getType()->isIntegerTy(1)) {
1236     llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
1237     C = llvm::ConstantExpr::getZExt(C, BoolTy);
1238   }
1239   return C;
1240 }
1241 
EmitConstantValue(const APValue & Value,QualType DestType,CodeGenFunction * CGF)1242 llvm::Constant *CodeGenModule::EmitConstantValue(const APValue &Value,
1243                                                  QualType DestType,
1244                                                  CodeGenFunction *CGF) {
1245   // For an _Atomic-qualified constant, we may need to add tail padding.
1246   if (auto *AT = DestType->getAs<AtomicType>()) {
1247     QualType InnerType = AT->getValueType();
1248     auto *Inner = EmitConstantValue(Value, InnerType, CGF);
1249 
1250     uint64_t InnerSize = Context.getTypeSize(InnerType);
1251     uint64_t OuterSize = Context.getTypeSize(DestType);
1252     if (InnerSize == OuterSize)
1253       return Inner;
1254 
1255     assert(InnerSize < OuterSize && "emitted over-large constant for atomic");
1256     llvm::Constant *Elts[] = {
1257       Inner,
1258       llvm::ConstantAggregateZero::get(
1259           llvm::ArrayType::get(Int8Ty, (OuterSize - InnerSize) / 8))
1260     };
1261     return llvm::ConstantStruct::getAnon(Elts);
1262   }
1263 
1264   switch (Value.getKind()) {
1265   case APValue::Uninitialized:
1266     llvm_unreachable("Constant expressions should be initialized.");
1267   case APValue::LValue: {
1268     llvm::Type *DestTy = getTypes().ConvertTypeForMem(DestType);
1269     llvm::Constant *Offset =
1270       llvm::ConstantInt::get(Int64Ty, Value.getLValueOffset().getQuantity());
1271 
1272     llvm::Constant *C = nullptr;
1273     if (APValue::LValueBase LVBase = Value.getLValueBase()) {
1274       // An array can be represented as an lvalue referring to the base.
1275       if (isa<llvm::ArrayType>(DestTy)) {
1276         assert(Offset->isNullValue() && "offset on array initializer");
1277         return ConstExprEmitter(*this, CGF).Visit(
1278           const_cast<Expr*>(LVBase.get<const Expr*>()));
1279       }
1280 
1281       C = ConstExprEmitter(*this, CGF).EmitLValue(LVBase).getPointer();
1282 
1283       // Apply offset if necessary.
1284       if (!Offset->isNullValue()) {
1285         unsigned AS = C->getType()->getPointerAddressSpace();
1286         llvm::Type *CharPtrTy = Int8Ty->getPointerTo(AS);
1287         llvm::Constant *Casted = llvm::ConstantExpr::getBitCast(C, CharPtrTy);
1288         Casted = llvm::ConstantExpr::getGetElementPtr(Int8Ty, Casted, Offset);
1289         C = llvm::ConstantExpr::getPointerCast(Casted, C->getType());
1290       }
1291 
1292       // Convert to the appropriate type; this could be an lvalue for
1293       // an integer.
1294       if (isa<llvm::PointerType>(DestTy))
1295         return llvm::ConstantExpr::getPointerCast(C, DestTy);
1296 
1297       return llvm::ConstantExpr::getPtrToInt(C, DestTy);
1298     } else {
1299       C = Offset;
1300 
1301       // Convert to the appropriate type; this could be an lvalue for
1302       // an integer.
1303       if (isa<llvm::PointerType>(DestTy))
1304         return llvm::ConstantExpr::getIntToPtr(C, DestTy);
1305 
1306       // If the types don't match this should only be a truncate.
1307       if (C->getType() != DestTy)
1308         return llvm::ConstantExpr::getTrunc(C, DestTy);
1309 
1310       return C;
1311     }
1312   }
1313   case APValue::Int:
1314     return llvm::ConstantInt::get(VMContext, Value.getInt());
1315   case APValue::ComplexInt: {
1316     llvm::Constant *Complex[2];
1317 
1318     Complex[0] = llvm::ConstantInt::get(VMContext,
1319                                         Value.getComplexIntReal());
1320     Complex[1] = llvm::ConstantInt::get(VMContext,
1321                                         Value.getComplexIntImag());
1322 
1323     // FIXME: the target may want to specify that this is packed.
1324     llvm::StructType *STy = llvm::StructType::get(Complex[0]->getType(),
1325                                                   Complex[1]->getType(),
1326                                                   nullptr);
1327     return llvm::ConstantStruct::get(STy, Complex);
1328   }
1329   case APValue::Float: {
1330     const llvm::APFloat &Init = Value.getFloat();
1331     if (&Init.getSemantics() == &llvm::APFloat::IEEEhalf &&
1332         !Context.getLangOpts().NativeHalfType &&
1333         !Context.getLangOpts().HalfArgsAndReturns)
1334       return llvm::ConstantInt::get(VMContext, Init.bitcastToAPInt());
1335     else
1336       return llvm::ConstantFP::get(VMContext, Init);
1337   }
1338   case APValue::ComplexFloat: {
1339     llvm::Constant *Complex[2];
1340 
1341     Complex[0] = llvm::ConstantFP::get(VMContext,
1342                                        Value.getComplexFloatReal());
1343     Complex[1] = llvm::ConstantFP::get(VMContext,
1344                                        Value.getComplexFloatImag());
1345 
1346     // FIXME: the target may want to specify that this is packed.
1347     llvm::StructType *STy = llvm::StructType::get(Complex[0]->getType(),
1348                                                   Complex[1]->getType(),
1349                                                   nullptr);
1350     return llvm::ConstantStruct::get(STy, Complex);
1351   }
1352   case APValue::Vector: {
1353     unsigned NumElts = Value.getVectorLength();
1354     SmallVector<llvm::Constant *, 4> Inits(NumElts);
1355 
1356     for (unsigned I = 0; I != NumElts; ++I) {
1357       const APValue &Elt = Value.getVectorElt(I);
1358       if (Elt.isInt())
1359         Inits[I] = llvm::ConstantInt::get(VMContext, Elt.getInt());
1360       else if (Elt.isFloat())
1361         Inits[I] = llvm::ConstantFP::get(VMContext, Elt.getFloat());
1362       else
1363         llvm_unreachable("unsupported vector element type");
1364     }
1365     return llvm::ConstantVector::get(Inits);
1366   }
1367   case APValue::AddrLabelDiff: {
1368     const AddrLabelExpr *LHSExpr = Value.getAddrLabelDiffLHS();
1369     const AddrLabelExpr *RHSExpr = Value.getAddrLabelDiffRHS();
1370     llvm::Constant *LHS = EmitConstantExpr(LHSExpr, LHSExpr->getType(), CGF);
1371     llvm::Constant *RHS = EmitConstantExpr(RHSExpr, RHSExpr->getType(), CGF);
1372 
1373     // Compute difference
1374     llvm::Type *ResultType = getTypes().ConvertType(DestType);
1375     LHS = llvm::ConstantExpr::getPtrToInt(LHS, IntPtrTy);
1376     RHS = llvm::ConstantExpr::getPtrToInt(RHS, IntPtrTy);
1377     llvm::Constant *AddrLabelDiff = llvm::ConstantExpr::getSub(LHS, RHS);
1378 
1379     // LLVM is a bit sensitive about the exact format of the
1380     // address-of-label difference; make sure to truncate after
1381     // the subtraction.
1382     return llvm::ConstantExpr::getTruncOrBitCast(AddrLabelDiff, ResultType);
1383   }
1384   case APValue::Struct:
1385   case APValue::Union:
1386     return ConstStructBuilder::BuildStruct(*this, CGF, Value, DestType);
1387   case APValue::Array: {
1388     const ArrayType *CAT = Context.getAsArrayType(DestType);
1389     unsigned NumElements = Value.getArraySize();
1390     unsigned NumInitElts = Value.getArrayInitializedElts();
1391 
1392     // Emit array filler, if there is one.
1393     llvm::Constant *Filler = nullptr;
1394     if (Value.hasArrayFiller())
1395       Filler = EmitConstantValueForMemory(Value.getArrayFiller(),
1396                                           CAT->getElementType(), CGF);
1397 
1398     // Emit initializer elements.
1399     llvm::Type *CommonElementType =
1400         getTypes().ConvertType(CAT->getElementType());
1401 
1402     // Try to use a ConstantAggregateZero if we can.
1403     if (Filler && Filler->isNullValue() && !NumInitElts) {
1404       llvm::ArrayType *AType =
1405           llvm::ArrayType::get(CommonElementType, NumElements);
1406       return llvm::ConstantAggregateZero::get(AType);
1407     }
1408 
1409     std::vector<llvm::Constant*> Elts;
1410     Elts.reserve(NumElements);
1411     for (unsigned I = 0; I < NumElements; ++I) {
1412       llvm::Constant *C = Filler;
1413       if (I < NumInitElts)
1414         C = EmitConstantValueForMemory(Value.getArrayInitializedElt(I),
1415                                        CAT->getElementType(), CGF);
1416       else
1417         assert(Filler && "Missing filler for implicit elements of initializer");
1418       if (I == 0)
1419         CommonElementType = C->getType();
1420       else if (C->getType() != CommonElementType)
1421         CommonElementType = nullptr;
1422       Elts.push_back(C);
1423     }
1424 
1425     if (!CommonElementType) {
1426       // FIXME: Try to avoid packing the array
1427       std::vector<llvm::Type*> Types;
1428       Types.reserve(NumElements);
1429       for (unsigned i = 0, e = Elts.size(); i < e; ++i)
1430         Types.push_back(Elts[i]->getType());
1431       llvm::StructType *SType = llvm::StructType::get(VMContext, Types, true);
1432       return llvm::ConstantStruct::get(SType, Elts);
1433     }
1434 
1435     llvm::ArrayType *AType =
1436       llvm::ArrayType::get(CommonElementType, NumElements);
1437     return llvm::ConstantArray::get(AType, Elts);
1438   }
1439   case APValue::MemberPointer:
1440     return getCXXABI().EmitMemberPointer(Value, DestType);
1441   }
1442   llvm_unreachable("Unknown APValue kind");
1443 }
1444 
1445 llvm::Constant *
EmitConstantValueForMemory(const APValue & Value,QualType DestType,CodeGenFunction * CGF)1446 CodeGenModule::EmitConstantValueForMemory(const APValue &Value,
1447                                           QualType DestType,
1448                                           CodeGenFunction *CGF) {
1449   llvm::Constant *C = EmitConstantValue(Value, DestType, CGF);
1450   if (C->getType()->isIntegerTy(1)) {
1451     llvm::Type *BoolTy = getTypes().ConvertTypeForMem(DestType);
1452     C = llvm::ConstantExpr::getZExt(C, BoolTy);
1453   }
1454   return C;
1455 }
1456 
1457 ConstantAddress
GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr * E)1458 CodeGenModule::GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *E) {
1459   assert(E->isFileScope() && "not a file-scope compound literal expr");
1460   return ConstExprEmitter(*this, nullptr).EmitLValue(E);
1461 }
1462 
1463 llvm::Constant *
getMemberPointerConstant(const UnaryOperator * uo)1464 CodeGenModule::getMemberPointerConstant(const UnaryOperator *uo) {
1465   // Member pointer constants always have a very particular form.
1466   const MemberPointerType *type = cast<MemberPointerType>(uo->getType());
1467   const ValueDecl *decl = cast<DeclRefExpr>(uo->getSubExpr())->getDecl();
1468 
1469   // A member function pointer.
1470   if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(decl))
1471     return getCXXABI().EmitMemberFunctionPointer(method);
1472 
1473   // Otherwise, a member data pointer.
1474   uint64_t fieldOffset = getContext().getFieldOffset(decl);
1475   CharUnits chars = getContext().toCharUnitsFromBits((int64_t) fieldOffset);
1476   return getCXXABI().EmitMemberDataPointer(type, chars);
1477 }
1478 
1479 static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
1480                                                llvm::Type *baseType,
1481                                                const CXXRecordDecl *base);
1482 
EmitNullConstant(CodeGenModule & CGM,const CXXRecordDecl * record,bool asCompleteObject)1483 static llvm::Constant *EmitNullConstant(CodeGenModule &CGM,
1484                                         const CXXRecordDecl *record,
1485                                         bool asCompleteObject) {
1486   const CGRecordLayout &layout = CGM.getTypes().getCGRecordLayout(record);
1487   llvm::StructType *structure =
1488     (asCompleteObject ? layout.getLLVMType()
1489                       : layout.getBaseSubobjectLLVMType());
1490 
1491   unsigned numElements = structure->getNumElements();
1492   std::vector<llvm::Constant *> elements(numElements);
1493 
1494   // Fill in all the bases.
1495   for (const auto &I : record->bases()) {
1496     if (I.isVirtual()) {
1497       // Ignore virtual bases; if we're laying out for a complete
1498       // object, we'll lay these out later.
1499       continue;
1500     }
1501 
1502     const CXXRecordDecl *base =
1503       cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
1504 
1505     // Ignore empty bases.
1506     if (base->isEmpty())
1507       continue;
1508 
1509     unsigned fieldIndex = layout.getNonVirtualBaseLLVMFieldNo(base);
1510     llvm::Type *baseType = structure->getElementType(fieldIndex);
1511     elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
1512   }
1513 
1514   // Fill in all the fields.
1515   for (const auto *Field : record->fields()) {
1516     // Fill in non-bitfields. (Bitfields always use a zero pattern, which we
1517     // will fill in later.)
1518     if (!Field->isBitField()) {
1519       unsigned fieldIndex = layout.getLLVMFieldNo(Field);
1520       elements[fieldIndex] = CGM.EmitNullConstant(Field->getType());
1521     }
1522 
1523     // For unions, stop after the first named field.
1524     if (record->isUnion()) {
1525       if (Field->getIdentifier())
1526         break;
1527       if (const auto *FieldRD =
1528               dyn_cast_or_null<RecordDecl>(Field->getType()->getAsTagDecl()))
1529         if (FieldRD->findFirstNamedDataMember())
1530           break;
1531     }
1532   }
1533 
1534   // Fill in the virtual bases, if we're working with the complete object.
1535   if (asCompleteObject) {
1536     for (const auto &I : record->vbases()) {
1537       const CXXRecordDecl *base =
1538         cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
1539 
1540       // Ignore empty bases.
1541       if (base->isEmpty())
1542         continue;
1543 
1544       unsigned fieldIndex = layout.getVirtualBaseIndex(base);
1545 
1546       // We might have already laid this field out.
1547       if (elements[fieldIndex]) continue;
1548 
1549       llvm::Type *baseType = structure->getElementType(fieldIndex);
1550       elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
1551     }
1552   }
1553 
1554   // Now go through all other fields and zero them out.
1555   for (unsigned i = 0; i != numElements; ++i) {
1556     if (!elements[i])
1557       elements[i] = llvm::Constant::getNullValue(structure->getElementType(i));
1558   }
1559 
1560   return llvm::ConstantStruct::get(structure, elements);
1561 }
1562 
1563 /// Emit the null constant for a base subobject.
EmitNullConstantForBase(CodeGenModule & CGM,llvm::Type * baseType,const CXXRecordDecl * base)1564 static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
1565                                                llvm::Type *baseType,
1566                                                const CXXRecordDecl *base) {
1567   const CGRecordLayout &baseLayout = CGM.getTypes().getCGRecordLayout(base);
1568 
1569   // Just zero out bases that don't have any pointer to data members.
1570   if (baseLayout.isZeroInitializableAsBase())
1571     return llvm::Constant::getNullValue(baseType);
1572 
1573   // Otherwise, we can just use its null constant.
1574   return EmitNullConstant(CGM, base, /*asCompleteObject=*/false);
1575 }
1576 
EmitNullConstant(QualType T)1577 llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) {
1578   if (getTypes().isZeroInitializable(T))
1579     return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T));
1580 
1581   if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) {
1582     llvm::ArrayType *ATy =
1583       cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T));
1584 
1585     QualType ElementTy = CAT->getElementType();
1586 
1587     llvm::Constant *Element = EmitNullConstant(ElementTy);
1588     unsigned NumElements = CAT->getSize().getZExtValue();
1589     SmallVector<llvm::Constant *, 8> Array(NumElements, Element);
1590     return llvm::ConstantArray::get(ATy, Array);
1591   }
1592 
1593   if (const RecordType *RT = T->getAs<RecordType>()) {
1594     const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1595     return ::EmitNullConstant(*this, RD, /*complete object*/ true);
1596   }
1597 
1598   assert(T->isMemberDataPointerType() &&
1599          "Should only see pointers to data members here!");
1600 
1601   return getCXXABI().EmitNullMemberPointer(T->castAs<MemberPointerType>());
1602 }
1603 
1604 llvm::Constant *
EmitNullConstantForBase(const CXXRecordDecl * Record)1605 CodeGenModule::EmitNullConstantForBase(const CXXRecordDecl *Record) {
1606   return ::EmitNullConstant(*this, Record, false);
1607 }
1608