clang 8.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 "ConstantEmitter.h"
20 #include "TargetInfo.h"
21 #include "clang/AST/APValue.h"
22 #include "clang/AST/ASTContext.h"
23 #include "clang/AST/RecordLayout.h"
24 #include "clang/AST/StmtVisitor.h"
25 #include "clang/Basic/Builtins.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/DataLayout.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/GlobalVariable.h"
30 using namespace clang;
31 using namespace CodeGen;
32
33 //===----------------------------------------------------------------------===//
34 // ConstStructBuilder
35 //===----------------------------------------------------------------------===//
36
37 namespace {
38 class ConstExprEmitter;
39 class ConstStructBuilder {
40 CodeGenModule &CGM;
41 ConstantEmitter &Emitter;
42
43 bool Packed;
44 CharUnits NextFieldOffsetInChars;
45 CharUnits LLVMStructAlignment;
46 SmallVector<llvm::Constant *, 32> Elements;
47 public:
48 static llvm::Constant *BuildStruct(ConstantEmitter &Emitter,
49 ConstExprEmitter *ExprEmitter,
50 llvm::Constant *Base,
51 InitListExpr *Updater,
52 QualType ValTy);
53 static llvm::Constant *BuildStruct(ConstantEmitter &Emitter,
54 InitListExpr *ILE, QualType StructTy);
55 static llvm::Constant *BuildStruct(ConstantEmitter &Emitter,
56 const APValue &Value, QualType ValTy);
57
58 private:
59 ConstStructBuilder(ConstantEmitter &emitter)
60 : CGM(emitter.CGM), Emitter(emitter), Packed(false),
61 NextFieldOffsetInChars(CharUnits::Zero()),
62 LLVMStructAlignment(CharUnits::One()) { }
63
64 void AppendField(const FieldDecl *Field, uint64_t FieldOffset,
65 llvm::Constant *InitExpr);
66
67 void AppendBytes(CharUnits FieldOffsetInChars, llvm::Constant *InitCst);
68
69 void AppendBitField(const FieldDecl *Field, uint64_t FieldOffset,
70 llvm::ConstantInt *InitExpr);
71
72 void AppendPadding(CharUnits PadSize);
73
74 void AppendTailPadding(CharUnits RecordSize);
75
76 void ConvertStructToPacked();
77
78 bool Build(InitListExpr *ILE);
79 bool Build(ConstExprEmitter *Emitter, llvm::Constant *Base,
80 InitListExpr *Updater);
81 bool Build(const APValue &Val, const RecordDecl *RD, bool IsPrimaryBase,
82 const CXXRecordDecl *VTableClass, CharUnits BaseOffset);
83 llvm::Constant *Finalize(QualType Ty);
84
85 CharUnits getAlignment(const llvm::Constant *C) const {
86 if (Packed) return CharUnits::One();
87 return CharUnits::fromQuantity(
88 CGM.getDataLayout().getABITypeAlignment(C->getType()));
89 }
90
91 CharUnits getSizeInChars(const llvm::Constant *C) const {
92 return CharUnits::fromQuantity(
93 CGM.getDataLayout().getTypeAllocSize(C->getType()));
94 }
95 };
96
97 void ConstStructBuilder::
98 AppendField(const FieldDecl *Field, uint64_t FieldOffset,
99 llvm::Constant *InitCst) {
100 const ASTContext &Context = CGM.getContext();
101
102 CharUnits FieldOffsetInChars = Context.toCharUnitsFromBits(FieldOffset);
103
104 AppendBytes(FieldOffsetInChars, InitCst);
105 }
106
107 void ConstStructBuilder::
108 AppendBytes(CharUnits FieldOffsetInChars, llvm::Constant *InitCst) {
109
110 assert(NextFieldOffsetInChars <= FieldOffsetInChars
111 && "Field offset mismatch!");
112
113 CharUnits FieldAlignment = getAlignment(InitCst);
114
115 // Round up the field offset to the alignment of the field type.
116 CharUnits AlignedNextFieldOffsetInChars =
117 NextFieldOffsetInChars.alignTo(FieldAlignment);
118
119 if (AlignedNextFieldOffsetInChars < FieldOffsetInChars) {
120 // We need to append padding.
121 AppendPadding(FieldOffsetInChars - NextFieldOffsetInChars);
122
123 assert(NextFieldOffsetInChars == FieldOffsetInChars &&
124 "Did not add enough padding!");
125
126 AlignedNextFieldOffsetInChars =
127 NextFieldOffsetInChars.alignTo(FieldAlignment);
128 }
129
130 if (AlignedNextFieldOffsetInChars > FieldOffsetInChars) {
131 assert(!Packed && "Alignment is wrong even with a packed struct!");
132
133 // Convert the struct to a packed struct.
134 ConvertStructToPacked();
135
136 // After we pack the struct, we may need to insert padding.
137 if (NextFieldOffsetInChars < FieldOffsetInChars) {
138 // We need to append padding.
139 AppendPadding(FieldOffsetInChars - NextFieldOffsetInChars);
140
141 assert(NextFieldOffsetInChars == FieldOffsetInChars &&
142 "Did not add enough padding!");
143 }
144 AlignedNextFieldOffsetInChars = NextFieldOffsetInChars;
145 }
146
147 // Add the field.
148 Elements.push_back(InitCst);
149 NextFieldOffsetInChars = AlignedNextFieldOffsetInChars +
150 getSizeInChars(InitCst);
151
152 if (Packed)
153 assert(LLVMStructAlignment == CharUnits::One() &&
154 "Packed struct not byte-aligned!");
155 else
156 LLVMStructAlignment = std::max(LLVMStructAlignment, FieldAlignment);
157 }
158
159 void ConstStructBuilder::AppendBitField(const FieldDecl *Field,
160 uint64_t FieldOffset,
161 llvm::ConstantInt *CI) {
162 const ASTContext &Context = CGM.getContext();
163 const uint64_t CharWidth = Context.getCharWidth();
164 uint64_t NextFieldOffsetInBits = Context.toBits(NextFieldOffsetInChars);
165 if (FieldOffset > NextFieldOffsetInBits) {
166 // We need to add padding.
167 CharUnits PadSize = Context.toCharUnitsFromBits(
168 llvm::alignTo(FieldOffset - NextFieldOffsetInBits,
169 Context.getTargetInfo().getCharAlign()));
170
171 AppendPadding(PadSize);
172 }
173
174 uint64_t FieldSize = Field->getBitWidthValue(Context);
175
176 llvm::APInt FieldValue = CI->getValue();
177
178 // Promote the size of FieldValue if necessary
179 // FIXME: This should never occur, but currently it can because initializer
180 // constants are cast to bool, and because clang is not enforcing bitfield
181 // width limits.
182 if (FieldSize > FieldValue.getBitWidth())
183 FieldValue = FieldValue.zext(FieldSize);
184
185 // Truncate the size of FieldValue to the bit field size.
186 if (FieldSize < FieldValue.getBitWidth())
187 FieldValue = FieldValue.trunc(FieldSize);
188
189 NextFieldOffsetInBits = Context.toBits(NextFieldOffsetInChars);
190 if (FieldOffset < NextFieldOffsetInBits) {
191 // Either part of the field or the entire field can go into the previous
192 // byte.
193 assert(!Elements.empty() && "Elements can't be empty!");
194
195 unsigned BitsInPreviousByte = NextFieldOffsetInBits - FieldOffset;
196
197 bool FitsCompletelyInPreviousByte =
198 BitsInPreviousByte >= FieldValue.getBitWidth();
199
200 llvm::APInt Tmp = FieldValue;
201
202 if (!FitsCompletelyInPreviousByte) {
203 unsigned NewFieldWidth = FieldSize - BitsInPreviousByte;
204
205 if (CGM.getDataLayout().isBigEndian()) {
206 Tmp.lshrInPlace(NewFieldWidth);
207 Tmp = Tmp.trunc(BitsInPreviousByte);
208
209 // We want the remaining high bits.
210 FieldValue = FieldValue.trunc(NewFieldWidth);
211 } else {
212 Tmp = Tmp.trunc(BitsInPreviousByte);
213
214 // We want the remaining low bits.
215 FieldValue.lshrInPlace(BitsInPreviousByte);
216 FieldValue = FieldValue.trunc(NewFieldWidth);
217 }
218 }
219
220 Tmp = Tmp.zext(CharWidth);
221 if (CGM.getDataLayout().isBigEndian()) {
222 if (FitsCompletelyInPreviousByte)
223 Tmp = Tmp.shl(BitsInPreviousByte - FieldValue.getBitWidth());
224 } else {
225 Tmp = Tmp.shl(CharWidth - BitsInPreviousByte);
226 }
227
228 // 'or' in the bits that go into the previous byte.
229 llvm::Value *LastElt = Elements.back();
230 if (llvm::ConstantInt *Val = dyn_cast<llvm::ConstantInt>(LastElt))
231 Tmp |= Val->getValue();
232 else {
233 assert(isa<llvm::UndefValue>(LastElt));
234 // If there is an undef field that we're adding to, it can either be a
235 // scalar undef (in which case, we just replace it with our field) or it
236 // is an array. If it is an array, we have to pull one byte off the
237 // array so that the other undef bytes stay around.
238 if (!isa<llvm::IntegerType>(LastElt->getType())) {
239 // The undef padding will be a multibyte array, create a new smaller
240 // padding and then an hole for our i8 to get plopped into.
241 assert(isa<llvm::ArrayType>(LastElt->getType()) &&
242 "Expected array padding of undefs");
243 llvm::ArrayType *AT = cast<llvm::ArrayType>(LastElt->getType());
244 assert(AT->getElementType()->isIntegerTy(CharWidth) &&
245 AT->getNumElements() != 0 &&
246 "Expected non-empty array padding of undefs");
247
248 // Remove the padding array.
249 NextFieldOffsetInChars -= CharUnits::fromQuantity(AT->getNumElements());
250 Elements.pop_back();
251
252 // Add the padding back in two chunks.
253 AppendPadding(CharUnits::fromQuantity(AT->getNumElements()-1));
254 AppendPadding(CharUnits::One());
255 assert(isa<llvm::UndefValue>(Elements.back()) &&
256 Elements.back()->getType()->isIntegerTy(CharWidth) &&
257 "Padding addition didn't work right");
258 }
259 }
260
261 Elements.back() = llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp);
262
263 if (FitsCompletelyInPreviousByte)
264 return;
265 }
266
267 while (FieldValue.getBitWidth() > CharWidth) {
268 llvm::APInt Tmp;
269
270 if (CGM.getDataLayout().isBigEndian()) {
271 // We want the high bits.
272 Tmp =
273 FieldValue.lshr(FieldValue.getBitWidth() - CharWidth).trunc(CharWidth);
274 } else {
275 // We want the low bits.
276 Tmp = FieldValue.trunc(CharWidth);
277
278 FieldValue.lshrInPlace(CharWidth);
279 }
280
281 Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp));
282 ++NextFieldOffsetInChars;
283
284 FieldValue = FieldValue.trunc(FieldValue.getBitWidth() - CharWidth);
285 }
286
287 assert(FieldValue.getBitWidth() > 0 &&
288 "Should have at least one bit left!");
289 assert(FieldValue.getBitWidth() <= CharWidth &&
290 "Should not have more than a byte left!");
291
292 if (FieldValue.getBitWidth() < CharWidth) {
293 if (CGM.getDataLayout().isBigEndian()) {
294 unsigned BitWidth = FieldValue.getBitWidth();
295
296 FieldValue = FieldValue.zext(CharWidth) << (CharWidth - BitWidth);
297 } else
298 FieldValue = FieldValue.zext(CharWidth);
299 }
300
301 // Append the last element.
302 Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(),
303 FieldValue));
304 ++NextFieldOffsetInChars;
305 }
306
307 void ConstStructBuilder::AppendPadding(CharUnits PadSize) {
308 if (PadSize.isZero())
309 return;
310
311 llvm::Type *Ty = CGM.Int8Ty;
312 if (PadSize > CharUnits::One())
313 Ty = llvm::ArrayType::get(Ty, PadSize.getQuantity());
314
315 llvm::Constant *C = llvm::UndefValue::get(Ty);
316 Elements.push_back(C);
317 assert(getAlignment(C) == CharUnits::One() &&
318 "Padding must have 1 byte alignment!");
319
320 NextFieldOffsetInChars += getSizeInChars(C);
321 }
322
323 void ConstStructBuilder::AppendTailPadding(CharUnits RecordSize) {
324 assert(NextFieldOffsetInChars <= RecordSize &&
325 "Size mismatch!");
326
327 AppendPadding(RecordSize - NextFieldOffsetInChars);
328 }
329
330 void ConstStructBuilder::ConvertStructToPacked() {
331 SmallVector<llvm::Constant *, 16> PackedElements;
332 CharUnits ElementOffsetInChars = CharUnits::Zero();
333
334 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
335 llvm::Constant *C = Elements[i];
336
337 CharUnits ElementAlign = CharUnits::fromQuantity(
338 CGM.getDataLayout().getABITypeAlignment(C->getType()));
339 CharUnits AlignedElementOffsetInChars =
340 ElementOffsetInChars.alignTo(ElementAlign);
341
342 if (AlignedElementOffsetInChars > ElementOffsetInChars) {
343 // We need some padding.
344 CharUnits NumChars =
345 AlignedElementOffsetInChars - ElementOffsetInChars;
346
347 llvm::Type *Ty = CGM.Int8Ty;
348 if (NumChars > CharUnits::One())
349 Ty = llvm::ArrayType::get(Ty, NumChars.getQuantity());
350
351 llvm::Constant *Padding = llvm::UndefValue::get(Ty);
352 PackedElements.push_back(Padding);
353 ElementOffsetInChars += getSizeInChars(Padding);
354 }
355
356 PackedElements.push_back(C);
357 ElementOffsetInChars += getSizeInChars(C);
358 }
359
360 assert(ElementOffsetInChars == NextFieldOffsetInChars &&
361 "Packing the struct changed its size!");
362
363 Elements.swap(PackedElements);
364 LLVMStructAlignment = CharUnits::One();
365 Packed = true;
366 }
367
368 bool ConstStructBuilder::Build(InitListExpr *ILE) {
369 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
370 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
371
372 unsigned FieldNo = 0;
373 unsigned ElementNo = 0;
374
375 // Bail out if we have base classes. We could support these, but they only
376 // arise in C++1z where we will have already constant folded most interesting
377 // cases. FIXME: There are still a few more cases we can handle this way.
378 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
379 if (CXXRD->getNumBases())
380 return false;
381
382 for (RecordDecl::field_iterator Field = RD->field_begin(),
383 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
384 // If this is a union, skip all the fields that aren't being initialized.
385 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
386 continue;
387
388 // Don't emit anonymous bitfields, they just affect layout.
389 if (Field->isUnnamedBitfield())
390 continue;
391
392 // Get the initializer. A struct can include fields without initializers,
393 // we just use explicit null values for them.
394 llvm::Constant *EltInit;
395 if (ElementNo < ILE->getNumInits())
396 EltInit = Emitter.tryEmitPrivateForMemory(ILE->getInit(ElementNo++),
397 Field->getType());
398 else
399 EltInit = Emitter.emitNullForMemory(Field->getType());
400
401 if (!EltInit)
402 return false;
403
404 if (!Field->isBitField()) {
405 // Handle non-bitfield members.
406 AppendField(*Field, Layout.getFieldOffset(FieldNo), EltInit);
407 } else {
408 // Otherwise we have a bitfield.
409 if (auto *CI = dyn_cast<llvm::ConstantInt>(EltInit)) {
410 AppendBitField(*Field, Layout.getFieldOffset(FieldNo), CI);
411 } else {
412 // We are trying to initialize a bitfield with a non-trivial constant,
413 // this must require run-time code.
414 return false;
415 }
416 }
417 }
418
419 return true;
420 }
421
422 namespace {
423 struct BaseInfo {
424 BaseInfo(const CXXRecordDecl *Decl, CharUnits Offset, unsigned Index)
425 : Decl(Decl), Offset(Offset), Index(Index) {
426 }
427
428 const CXXRecordDecl *Decl;
429 CharUnits Offset;
430 unsigned Index;
431
432 bool operator<(const BaseInfo &O) const { return Offset < O.Offset; }
433 };
434 }
435
436 bool ConstStructBuilder::Build(const APValue &Val, const RecordDecl *RD,
437 bool IsPrimaryBase,
438 const CXXRecordDecl *VTableClass,
439 CharUnits Offset) {
440 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
441
442 if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) {
443 // Add a vtable pointer, if we need one and it hasn't already been added.
444 if (CD->isDynamicClass() && !IsPrimaryBase) {
445 llvm::Constant *VTableAddressPoint =
446 CGM.getCXXABI().getVTableAddressPointForConstExpr(
447 BaseSubobject(CD, Offset), VTableClass);
448 AppendBytes(Offset, VTableAddressPoint);
449 }
450
451 // Accumulate and sort bases, in order to visit them in address order, which
452 // may not be the same as declaration order.
453 SmallVector<BaseInfo, 8> Bases;
454 Bases.reserve(CD->getNumBases());
455 unsigned BaseNo = 0;
456 for (CXXRecordDecl::base_class_const_iterator Base = CD->bases_begin(),
457 BaseEnd = CD->bases_end(); Base != BaseEnd; ++Base, ++BaseNo) {
458 assert(!Base->isVirtual() && "should not have virtual bases here");
459 const CXXRecordDecl *BD = Base->getType()->getAsCXXRecordDecl();
460 CharUnits BaseOffset = Layout.getBaseClassOffset(BD);
461 Bases.push_back(BaseInfo(BD, BaseOffset, BaseNo));
462 }
463 std::stable_sort(Bases.begin(), Bases.end());
464
465 for (unsigned I = 0, N = Bases.size(); I != N; ++I) {
466 BaseInfo &Base = Bases[I];
467
468 bool IsPrimaryBase = Layout.getPrimaryBase() == Base.Decl;
469 Build(Val.getStructBase(Base.Index), Base.Decl, IsPrimaryBase,
470 VTableClass, Offset + Base.Offset);
471 }
472 }
473
474 unsigned FieldNo = 0;
475 uint64_t OffsetBits = CGM.getContext().toBits(Offset);
476
477 for (RecordDecl::field_iterator Field = RD->field_begin(),
478 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
479 // If this is a union, skip all the fields that aren't being initialized.
480 if (RD->isUnion() && Val.getUnionField() != *Field)
481 continue;
482
483 // Don't emit anonymous bitfields, they just affect layout.
484 if (Field->isUnnamedBitfield())
485 continue;
486
487 // Emit the value of the initializer.
488 const APValue &FieldValue =
489 RD->isUnion() ? Val.getUnionValue() : Val.getStructField(FieldNo);
490 llvm::Constant *EltInit =
491 Emitter.tryEmitPrivateForMemory(FieldValue, Field->getType());
492 if (!EltInit)
493 return false;
494
495 if (!Field->isBitField()) {
496 // Handle non-bitfield members.
497 AppendField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits, EltInit);
498 } else {
499 // Otherwise we have a bitfield.
500 AppendBitField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits,
501 cast<llvm::ConstantInt>(EltInit));
502 }
503 }
504
505 return true;
506 }
507
508 llvm::Constant *ConstStructBuilder::Finalize(QualType Ty) {
509 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
510 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
511
512 CharUnits LayoutSizeInChars = Layout.getSize();
513
514 if (NextFieldOffsetInChars > LayoutSizeInChars) {
515 // If the struct is bigger than the size of the record type,
516 // we must have a flexible array member at the end.
517 assert(RD->hasFlexibleArrayMember() &&
518 "Must have flexible array member if struct is bigger than type!");
519
520 // No tail padding is necessary.
521 } else {
522 // Append tail padding if necessary.
523 CharUnits LLVMSizeInChars =
524 NextFieldOffsetInChars.alignTo(LLVMStructAlignment);
525
526 if (LLVMSizeInChars != LayoutSizeInChars)
527 AppendTailPadding(LayoutSizeInChars);
528
529 LLVMSizeInChars = NextFieldOffsetInChars.alignTo(LLVMStructAlignment);
530
531 // Check if we need to convert the struct to a packed struct.
532 if (NextFieldOffsetInChars <= LayoutSizeInChars &&
533 LLVMSizeInChars > LayoutSizeInChars) {
534 assert(!Packed && "Size mismatch!");
535
536 ConvertStructToPacked();
537 assert(NextFieldOffsetInChars <= LayoutSizeInChars &&
538 "Converting to packed did not help!");
539 }
540
541 LLVMSizeInChars = NextFieldOffsetInChars.alignTo(LLVMStructAlignment);
542
543 assert(LayoutSizeInChars == LLVMSizeInChars &&
544 "Tail padding mismatch!");
545 }
546
547 // Pick the type to use. If the type is layout identical to the ConvertType
548 // type then use it, otherwise use whatever the builder produced for us.
549 llvm::StructType *STy =
550 llvm::ConstantStruct::getTypeForElements(CGM.getLLVMContext(),
551 Elements, Packed);
552 llvm::Type *ValTy = CGM.getTypes().ConvertType(Ty);
553 if (llvm::StructType *ValSTy = dyn_cast<llvm::StructType>(ValTy)) {
554 if (ValSTy->isLayoutIdentical(STy))
555 STy = ValSTy;
556 }
557
558 llvm::Constant *Result = llvm::ConstantStruct::get(STy, Elements);
559
560 assert(NextFieldOffsetInChars.alignTo(getAlignment(Result)) ==
561 getSizeInChars(Result) &&
562 "Size mismatch!");
563
564 return Result;
565 }
566
567 llvm::Constant *ConstStructBuilder::BuildStruct(ConstantEmitter &Emitter,
568 ConstExprEmitter *ExprEmitter,
569 llvm::Constant *Base,
570 InitListExpr *Updater,
571 QualType ValTy) {
572 ConstStructBuilder Builder(Emitter);
573 if (!Builder.Build(ExprEmitter, Base, Updater))
574 return nullptr;
575 return Builder.Finalize(ValTy);
576 }
577
578 llvm::Constant *ConstStructBuilder::BuildStruct(ConstantEmitter &Emitter,
579 InitListExpr *ILE,
580 QualType ValTy) {
581 ConstStructBuilder Builder(Emitter);
582
583 if (!Builder.Build(ILE))
584 return nullptr;
585
586 return Builder.Finalize(ValTy);
587 }
588
589 llvm::Constant *ConstStructBuilder::BuildStruct(ConstantEmitter &Emitter,
590 const APValue &Val,
591 QualType ValTy) {
592 ConstStructBuilder Builder(Emitter);
593
594 const RecordDecl *RD = ValTy->castAs<RecordType>()->getDecl();
595 const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD);
596 if (!Builder.Build(Val, RD, false, CD, CharUnits::Zero()))
597 return nullptr;
598
599 return Builder.Finalize(ValTy);
600 }
601
602
603 //===----------------------------------------------------------------------===//
604 // ConstExprEmitter
605 //===----------------------------------------------------------------------===//
606
607 static ConstantAddress tryEmitGlobalCompoundLiteral(CodeGenModule &CGM,
608 CodeGenFunction *CGF,
609 const CompoundLiteralExpr *E) {
610 CharUnits Align = CGM.getContext().getTypeAlignInChars(E->getType());
611 if (llvm::GlobalVariable *Addr =
612 CGM.getAddrOfConstantCompoundLiteralIfEmitted(E))
613 return ConstantAddress(Addr, Align);
614
615 LangAS addressSpace = E->getType().getAddressSpace();
616
617 ConstantEmitter emitter(CGM, CGF);
618 llvm::Constant *C = emitter.tryEmitForInitializer(E->getInitializer(),
619 addressSpace, E->getType());
620 if (!C) {
621 assert(!E->isFileScope() &&
622 "file-scope compound literal did not have constant initializer!");
623 return ConstantAddress::invalid();
624 }
625
626 auto GV = new llvm::GlobalVariable(CGM.getModule(), C->getType(),
627 CGM.isTypeConstant(E->getType(), true),
628 llvm::GlobalValue::InternalLinkage,
629 C, ".compoundliteral", nullptr,
630 llvm::GlobalVariable::NotThreadLocal,
631 CGM.getContext().getTargetAddressSpace(addressSpace));
632 emitter.finalize(GV);
633 GV->setAlignment(Align.getQuantity());
634 CGM.setAddrOfConstantCompoundLiteral(E, GV);
635 return ConstantAddress(GV, Align);
636 }
637
638 static llvm::Constant *
639 EmitArrayConstant(CodeGenModule &CGM, const ConstantArrayType *DestType,
640 llvm::Type *CommonElementType, unsigned ArrayBound,
641 SmallVectorImpl<llvm::Constant *> &Elements,
642 llvm::Constant *Filler) {
643 // Figure out how long the initial prefix of non-zero elements is.
644 unsigned NonzeroLength = ArrayBound;
645 if (Elements.size() < NonzeroLength && Filler->isNullValue())
646 NonzeroLength = Elements.size();
647 if (NonzeroLength == Elements.size()) {
648 while (NonzeroLength > 0 && Elements[NonzeroLength - 1]->isNullValue())
649 --NonzeroLength;
650 }
651
652 if (NonzeroLength == 0) {
653 return llvm::ConstantAggregateZero::get(
654 CGM.getTypes().ConvertType(QualType(DestType, 0)));
655 }
656
657 // Add a zeroinitializer array filler if we have lots of trailing zeroes.
658 unsigned TrailingZeroes = ArrayBound - NonzeroLength;
659 if (TrailingZeroes >= 8) {
660 assert(Elements.size() >= NonzeroLength &&
661 "missing initializer for non-zero element");
662
663 // If all the elements had the same type up to the trailing zeroes, emit a
664 // struct of two arrays (the nonzero data and the zeroinitializer).
665 if (CommonElementType && NonzeroLength >= 8) {
666 llvm::Constant *Initial = llvm::ConstantArray::get(
667 llvm::ArrayType::get(CommonElementType, NonzeroLength),
668 makeArrayRef(Elements).take_front(NonzeroLength));
669 Elements.resize(2);
670 Elements[0] = Initial;
671 } else {
672 Elements.resize(NonzeroLength + 1);
673 }
674
675 auto *FillerType =
676 CommonElementType
677 ? CommonElementType
678 : CGM.getTypes().ConvertType(DestType->getElementType());
679 FillerType = llvm::ArrayType::get(FillerType, TrailingZeroes);
680 Elements.back() = llvm::ConstantAggregateZero::get(FillerType);
681 CommonElementType = nullptr;
682 } else if (Elements.size() != ArrayBound) {
683 // Otherwise pad to the right size with the filler if necessary.
684 Elements.resize(ArrayBound, Filler);
685 if (Filler->getType() != CommonElementType)
686 CommonElementType = nullptr;
687 }
688
689 // If all elements have the same type, just emit an array constant.
690 if (CommonElementType)
691 return llvm::ConstantArray::get(
692 llvm::ArrayType::get(CommonElementType, ArrayBound), Elements);
693
694 // We have mixed types. Use a packed struct.
695 llvm::SmallVector<llvm::Type *, 16> Types;
696 Types.reserve(Elements.size());
697 for (llvm::Constant *Elt : Elements)
698 Types.push_back(Elt->getType());
699 llvm::StructType *SType =
700 llvm::StructType::get(CGM.getLLVMContext(), Types, true);
701 return llvm::ConstantStruct::get(SType, Elements);
702 }
703
704 /// This class only needs to handle two cases:
705 /// 1) Literals (this is used by APValue emission to emit literals).
706 /// 2) Arrays, structs and unions (outside C++11 mode, we don't currently
707 /// constant fold these types).
708 class ConstExprEmitter :
709 public StmtVisitor<ConstExprEmitter, llvm::Constant*, QualType> {
710 CodeGenModule &CGM;
711 ConstantEmitter &Emitter;
712 llvm::LLVMContext &VMContext;
713 public:
714 ConstExprEmitter(ConstantEmitter &emitter)
715 : CGM(emitter.CGM), Emitter(emitter), VMContext(CGM.getLLVMContext()) {
716 }
717
718 //===--------------------------------------------------------------------===//
719 // Visitor Methods
720 //===--------------------------------------------------------------------===//
721
722 llvm::Constant *VisitStmt(Stmt *S, QualType T) {
723 return nullptr;
724 }
725
726 llvm::Constant *VisitConstantExpr(ConstantExpr *CE, QualType T) {
727 return Visit(CE->getSubExpr(), T);
728 }
729
730 llvm::Constant *VisitParenExpr(ParenExpr *PE, QualType T) {
731 return Visit(PE->getSubExpr(), T);
732 }
733
734 llvm::Constant *
735 VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE,
736 QualType T) {
737 return Visit(PE->getReplacement(), T);
738 }
739
740 llvm::Constant *VisitGenericSelectionExpr(GenericSelectionExpr *GE,
741 QualType T) {
742 return Visit(GE->getResultExpr(), T);
743 }
744
745 llvm::Constant *VisitChooseExpr(ChooseExpr *CE, QualType T) {
746 return Visit(CE->getChosenSubExpr(), T);
747 }
748
749 llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E, QualType T) {
750 return Visit(E->getInitializer(), T);
751 }
752
753 llvm::Constant *VisitCastExpr(CastExpr *E, QualType destType) {
754 if (const auto *ECE = dyn_cast<ExplicitCastExpr>(E))
755 CGM.EmitExplicitCastExprType(ECE, Emitter.CGF);
756 Expr *subExpr = E->getSubExpr();
757
758 switch (E->getCastKind()) {
759 case CK_ToUnion: {
760 // GCC cast to union extension
761 assert(E->getType()->isUnionType() &&
762 "Destination type is not union type!");
763
764 auto field = E->getTargetUnionField();
765
766 auto C = Emitter.tryEmitPrivateForMemory(subExpr, field->getType());
767 if (!C) return nullptr;
768
769 auto destTy = ConvertType(destType);
770 if (C->getType() == destTy) return C;
771
772 // Build a struct with the union sub-element as the first member,
773 // and padded to the appropriate size.
774 SmallVector<llvm::Constant*, 2> Elts;
775 SmallVector<llvm::Type*, 2> Types;
776 Elts.push_back(C);
777 Types.push_back(C->getType());
778 unsigned CurSize = CGM.getDataLayout().getTypeAllocSize(C->getType());
779 unsigned TotalSize = CGM.getDataLayout().getTypeAllocSize(destTy);
780
781 assert(CurSize <= TotalSize && "Union size mismatch!");
782 if (unsigned NumPadBytes = TotalSize - CurSize) {
783 llvm::Type *Ty = CGM.Int8Ty;
784 if (NumPadBytes > 1)
785 Ty = llvm::ArrayType::get(Ty, NumPadBytes);
786
787 Elts.push_back(llvm::UndefValue::get(Ty));
788 Types.push_back(Ty);
789 }
790
791 llvm::StructType *STy = llvm::StructType::get(VMContext, Types, false);
792 return llvm::ConstantStruct::get(STy, Elts);
793 }
794
795 case CK_AddressSpaceConversion: {
796 auto C = Emitter.tryEmitPrivate(subExpr, subExpr->getType());
797 if (!C) return nullptr;
798 LangAS destAS = E->getType()->getPointeeType().getAddressSpace();
799 LangAS srcAS = subExpr->getType()->getPointeeType().getAddressSpace();
800 llvm::Type *destTy = ConvertType(E->getType());
801 return CGM.getTargetCodeGenInfo().performAddrSpaceCast(CGM, C, srcAS,
802 destAS, destTy);
803 }
804
805 case CK_LValueToRValue:
806 case CK_AtomicToNonAtomic:
807 case CK_NonAtomicToAtomic:
808 case CK_NoOp:
809 case CK_ConstructorConversion:
810 return Visit(subExpr, destType);
811
812 case CK_IntToOCLSampler:
813 llvm_unreachable("global sampler variables are not generated");
814
815 case CK_Dependent: llvm_unreachable("saw dependent cast!");
816
817 case CK_BuiltinFnToFnPtr:
818 llvm_unreachable("builtin functions are handled elsewhere");
819
820 case CK_ReinterpretMemberPointer:
821 case CK_DerivedToBaseMemberPointer:
822 case CK_BaseToDerivedMemberPointer: {
823 auto C = Emitter.tryEmitPrivate(subExpr, subExpr->getType());
824 if (!C) return nullptr;
825 return CGM.getCXXABI().EmitMemberPointerConversion(E, C);
826 }
827
828 // These will never be supported.
829 case CK_ObjCObjectLValueCast:
830 case CK_ARCProduceObject:
831 case CK_ARCConsumeObject:
832 case CK_ARCReclaimReturnedObject:
833 case CK_ARCExtendBlockObject:
834 case CK_CopyAndAutoreleaseBlockObject:
835 return nullptr;
836
837 // These don't need to be handled here because Evaluate knows how to
838 // evaluate them in the cases where they can be folded.
839 case CK_BitCast:
840 case CK_ToVoid:
841 case CK_Dynamic:
842 case CK_LValueBitCast:
843 case CK_NullToMemberPointer:
844 case CK_UserDefinedConversion:
845 case CK_CPointerToObjCPointerCast:
846 case CK_BlockPointerToObjCPointerCast:
847 case CK_AnyPointerToBlockPointerCast:
848 case CK_ArrayToPointerDecay:
849 case CK_FunctionToPointerDecay:
850 case CK_BaseToDerived:
851 case CK_DerivedToBase:
852 case CK_UncheckedDerivedToBase:
853 case CK_MemberPointerToBoolean:
854 case CK_VectorSplat:
855 case CK_FloatingRealToComplex:
856 case CK_FloatingComplexToReal:
857 case CK_FloatingComplexToBoolean:
858 case CK_FloatingComplexCast:
859 case CK_FloatingComplexToIntegralComplex:
860 case CK_IntegralRealToComplex:
861 case CK_IntegralComplexToReal:
862 case CK_IntegralComplexToBoolean:
863 case CK_IntegralComplexCast:
864 case CK_IntegralComplexToFloatingComplex:
865 case CK_PointerToIntegral:
866 case CK_PointerToBoolean:
867 case CK_NullToPointer:
868 case CK_IntegralCast:
869 case CK_BooleanToSignedIntegral:
870 case CK_IntegralToPointer:
871 case CK_IntegralToBoolean:
872 case CK_IntegralToFloating:
873 case CK_FloatingToIntegral:
874 case CK_FloatingToBoolean:
875 case CK_FloatingCast:
876 case CK_FixedPointCast:
877 case CK_FixedPointToBoolean:
878 case CK_ZeroToOCLOpaqueType:
879 return nullptr;
880 }
881 llvm_unreachable("Invalid CastKind");
882 }
883
884 llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE, QualType T) {
885 return Visit(DAE->getExpr(), T);
886 }
887
888 llvm::Constant *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE, QualType T) {
889 // No need for a DefaultInitExprScope: we don't handle 'this' in a
890 // constant expression.
891 return Visit(DIE->getExpr(), T);
892 }
893
894 llvm::Constant *VisitExprWithCleanups(ExprWithCleanups *E, QualType T) {
895 if (!E->cleanupsHaveSideEffects())
896 return Visit(E->getSubExpr(), T);
897 return nullptr;
898 }
899
900 llvm::Constant *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E,
901 QualType T) {
902 return Visit(E->GetTemporaryExpr(), T);
903 }
904
905 llvm::Constant *EmitArrayInitialization(InitListExpr *ILE, QualType T) {
906 auto *CAT = CGM.getContext().getAsConstantArrayType(ILE->getType());
907 assert(CAT && "can't emit array init for non-constant-bound array");
908 unsigned NumInitElements = ILE->getNumInits();
909 unsigned NumElements = CAT->getSize().getZExtValue();
910
911 // Initialising an array requires us to automatically
912 // initialise any elements that have not been initialised explicitly
913 unsigned NumInitableElts = std::min(NumInitElements, NumElements);
914
915 QualType EltType = CAT->getElementType();
916
917 // Initialize remaining array elements.
918 llvm::Constant *fillC = nullptr;
919 if (Expr *filler = ILE->getArrayFiller()) {
920 fillC = Emitter.tryEmitAbstractForMemory(filler, EltType);
921 if (!fillC)
922 return nullptr;
923 }
924
925 // Copy initializer elements.
926 SmallVector<llvm::Constant*, 16> Elts;
927 if (fillC && fillC->isNullValue())
928 Elts.reserve(NumInitableElts + 1);
929 else
930 Elts.reserve(NumElements);
931
932 llvm::Type *CommonElementType = nullptr;
933 for (unsigned i = 0; i < NumInitableElts; ++i) {
934 Expr *Init = ILE->getInit(i);
935 llvm::Constant *C = Emitter.tryEmitPrivateForMemory(Init, EltType);
936 if (!C)
937 return nullptr;
938 if (i == 0)
939 CommonElementType = C->getType();
940 else if (C->getType() != CommonElementType)
941 CommonElementType = nullptr;
942 Elts.push_back(C);
943 }
944
945 return EmitArrayConstant(CGM, CAT, CommonElementType, NumElements, Elts,
946 fillC);
947 }
948
949 llvm::Constant *EmitRecordInitialization(InitListExpr *ILE, QualType T) {
950 return ConstStructBuilder::BuildStruct(Emitter, ILE, T);
951 }
952
953 llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E,
954 QualType T) {
955 return CGM.EmitNullConstant(T);
956 }
957
958 llvm::Constant *VisitInitListExpr(InitListExpr *ILE, QualType T) {
959 if (ILE->isTransparent())
960 return Visit(ILE->getInit(0), T);
961
962 if (ILE->getType()->isArrayType())
963 return EmitArrayInitialization(ILE, T);
964
965 if (ILE->getType()->isRecordType())
966 return EmitRecordInitialization(ILE, T);
967
968 return nullptr;
969 }
970
971 llvm::Constant *EmitDesignatedInitUpdater(llvm::Constant *Base,
972 InitListExpr *Updater,
973 QualType destType) {
974 if (auto destAT = CGM.getContext().getAsArrayType(destType)) {
975 llvm::ArrayType *AType = cast<llvm::ArrayType>(ConvertType(destType));
976 llvm::Type *ElemType = AType->getElementType();
977
978 unsigned NumInitElements = Updater->getNumInits();
979 unsigned NumElements = AType->getNumElements();
980
981 std::vector<llvm::Constant *> Elts;
982 Elts.reserve(NumElements);
983
984 QualType destElemType = destAT->getElementType();
985
986 if (auto DataArray = dyn_cast<llvm::ConstantDataArray>(Base))
987 for (unsigned i = 0; i != NumElements; ++i)
988 Elts.push_back(DataArray->getElementAsConstant(i));
989 else if (auto Array = dyn_cast<llvm::ConstantArray>(Base))
990 for (unsigned i = 0; i != NumElements; ++i)
991 Elts.push_back(Array->getOperand(i));
992 else
993 return nullptr; // FIXME: other array types not implemented
994
995 llvm::Constant *fillC = nullptr;
996 if (Expr *filler = Updater->getArrayFiller())
997 if (!isa<NoInitExpr>(filler))
998 fillC = Emitter.tryEmitAbstractForMemory(filler, destElemType);
999 bool RewriteType = (fillC && fillC->getType() != ElemType);
1000
1001 for (unsigned i = 0; i != NumElements; ++i) {
1002 Expr *Init = nullptr;
1003 if (i < NumInitElements)
1004 Init = Updater->getInit(i);
1005
1006 if (!Init && fillC)
1007 Elts[i] = fillC;
1008 else if (!Init || isa<NoInitExpr>(Init))
1009 ; // Do nothing.
1010 else if (InitListExpr *ChildILE = dyn_cast<InitListExpr>(Init))
1011 Elts[i] = EmitDesignatedInitUpdater(Elts[i], ChildILE, destElemType);
1012 else
1013 Elts[i] = Emitter.tryEmitPrivateForMemory(Init, destElemType);
1014
1015 if (!Elts[i])
1016 return nullptr;
1017 RewriteType |= (Elts[i]->getType() != ElemType);
1018 }
1019
1020 if (RewriteType) {
1021 std::vector<llvm::Type *> Types;
1022 Types.reserve(NumElements);
1023 for (unsigned i = 0; i != NumElements; ++i)
1024 Types.push_back(Elts[i]->getType());
1025 llvm::StructType *SType = llvm::StructType::get(AType->getContext(),
1026 Types, true);
1027 return llvm::ConstantStruct::get(SType, Elts);
1028 }
1029
1030 return llvm::ConstantArray::get(AType, Elts);
1031 }
1032
1033 if (destType->isRecordType())
1034 return ConstStructBuilder::BuildStruct(Emitter, this, Base, Updater,
1035 destType);
1036
1037 return nullptr;
1038 }
1039
1040 llvm::Constant *VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr *E,
1041 QualType destType) {
1042 auto C = Visit(E->getBase(), destType);
1043 if (!C) return nullptr;
1044 return EmitDesignatedInitUpdater(C, E->getUpdater(), destType);
1045 }
1046
1047 llvm::Constant *VisitCXXConstructExpr(CXXConstructExpr *E, QualType Ty) {
1048 if (!E->getConstructor()->isTrivial())
1049 return nullptr;
1050
1051 // FIXME: We should not have to call getBaseElementType here.
1052 const RecordType *RT =
1053 CGM.getContext().getBaseElementType(Ty)->getAs<RecordType>();
1054 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1055
1056 // If the class doesn't have a trivial destructor, we can't emit it as a
1057 // constant expr.
1058 if (!RD->hasTrivialDestructor())
1059 return nullptr;
1060
1061 // Only copy and default constructors can be trivial.
1062
1063
1064 if (E->getNumArgs()) {
1065 assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument");
1066 assert(E->getConstructor()->isCopyOrMoveConstructor() &&
1067 "trivial ctor has argument but isn't a copy/move ctor");
1068
1069 Expr *Arg = E->getArg(0);
1070 assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) &&
1071 "argument to copy ctor is of wrong type");
1072
1073 return Visit(Arg, Ty);
1074 }
1075
1076 return CGM.EmitNullConstant(Ty);
1077 }
1078
1079 llvm::Constant *VisitStringLiteral(StringLiteral *E, QualType T) {
1080 return CGM.GetConstantArrayFromStringLiteral(E);
1081 }
1082
1083 llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E, QualType T) {
1084 // This must be an @encode initializing an array in a static initializer.
1085 // Don't emit it as the address of the string, emit the string data itself
1086 // as an inline array.
1087 std::string Str;
1088 CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str);
1089 const ConstantArrayType *CAT = CGM.getContext().getAsConstantArrayType(T);
1090
1091 // Resize the string to the right size, adding zeros at the end, or
1092 // truncating as needed.
1093 Str.resize(CAT->getSize().getZExtValue(), '\0');
1094 return llvm::ConstantDataArray::getString(VMContext, Str, false);
1095 }
1096
1097 llvm::Constant *VisitUnaryExtension(const UnaryOperator *E, QualType T) {
1098 return Visit(E->getSubExpr(), T);
1099 }
1100
1101 // Utility methods
1102 llvm::Type *ConvertType(QualType T) {
1103 return CGM.getTypes().ConvertType(T);
1104 }
1105 };
1106
1107 } // end anonymous namespace.
1108
1109 bool ConstStructBuilder::Build(ConstExprEmitter *ExprEmitter,
1110 llvm::Constant *Base,
1111 InitListExpr *Updater) {
1112 assert(Base && "base expression should not be empty");
1113
1114 QualType ExprType = Updater->getType();
1115 RecordDecl *RD = ExprType->getAs<RecordType>()->getDecl();
1116 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1117 const llvm::StructLayout *BaseLayout = CGM.getDataLayout().getStructLayout(
1118 cast<llvm::StructType>(Base->getType()));
1119 unsigned FieldNo = -1;
1120 unsigned ElementNo = 0;
1121
1122 // Bail out if we have base classes. We could support these, but they only
1123 // arise in C++1z where we will have already constant folded most interesting
1124 // cases. FIXME: There are still a few more cases we can handle this way.
1125 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1126 if (CXXRD->getNumBases())
1127 return false;
1128
1129 for (FieldDecl *Field : RD->fields()) {
1130 ++FieldNo;
1131
1132 if (RD->isUnion() && Updater->getInitializedFieldInUnion() != Field)
1133 continue;
1134
1135 // Skip anonymous bitfields.
1136 if (Field->isUnnamedBitfield())
1137 continue;
1138
1139 llvm::Constant *EltInit = Base->getAggregateElement(ElementNo);
1140
1141 // Bail out if the type of the ConstantStruct does not have the same layout
1142 // as the type of the InitListExpr.
1143 if (CGM.getTypes().ConvertType(Field->getType()) != EltInit->getType() ||
1144 Layout.getFieldOffset(ElementNo) !=
1145 BaseLayout->getElementOffsetInBits(ElementNo))
1146 return false;
1147
1148 // Get the initializer. If we encounter an empty field or a NoInitExpr,
1149 // we use values from the base expression.
1150 Expr *Init = nullptr;
1151 if (ElementNo < Updater->getNumInits())
1152 Init = Updater->getInit(ElementNo);
1153
1154 if (!Init || isa<NoInitExpr>(Init))
1155 ; // Do nothing.
1156 else if (InitListExpr *ChildILE = dyn_cast<InitListExpr>(Init))
1157 EltInit = ExprEmitter->EmitDesignatedInitUpdater(EltInit, ChildILE,
1158 Field->getType());
1159 else
1160 EltInit = Emitter.tryEmitPrivateForMemory(Init, Field->getType());
1161
1162 ++ElementNo;
1163
1164 if (!EltInit)
1165 return false;
1166
1167 if (!Field->isBitField())
1168 AppendField(Field, Layout.getFieldOffset(FieldNo), EltInit);
1169 else if (llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(EltInit))
1170 AppendBitField(Field, Layout.getFieldOffset(FieldNo), CI);
1171 else
1172 // Initializing a bitfield with a non-trivial constant?
1173 return false;
1174 }
1175
1176 return true;
1177 }
1178
1179 llvm::Constant *ConstantEmitter::validateAndPopAbstract(llvm::Constant *C,
1180 AbstractState saved) {
1181 Abstract = saved.OldValue;
1182
1183 assert(saved.OldPlaceholdersSize == PlaceholderAddresses.size() &&
1184 "created a placeholder while doing an abstract emission?");
1185
1186 // No validation necessary for now.
1187 // No cleanup to do for now.
1188 return C;
1189 }
1190
1191 llvm::Constant *
1192 ConstantEmitter::tryEmitAbstractForInitializer(const VarDecl &D) {
1193 auto state = pushAbstract();
1194 auto C = tryEmitPrivateForVarInit(D);
1195 return validateAndPopAbstract(C, state);
1196 }
1197
1198 llvm::Constant *
1199 ConstantEmitter::tryEmitAbstract(const Expr *E, QualType destType) {
1200 auto state = pushAbstract();
1201 auto C = tryEmitPrivate(E, destType);
1202 return validateAndPopAbstract(C, state);
1203 }
1204
1205 llvm::Constant *
1206 ConstantEmitter::tryEmitAbstract(const APValue &value, QualType destType) {
1207 auto state = pushAbstract();
1208 auto C = tryEmitPrivate(value, destType);
1209 return validateAndPopAbstract(C, state);
1210 }
1211
1212 llvm::Constant *
1213 ConstantEmitter::emitAbstract(const Expr *E, QualType destType) {
1214 auto state = pushAbstract();
1215 auto C = tryEmitPrivate(E, destType);
1216 C = validateAndPopAbstract(C, state);
1217 if (!C) {
1218 CGM.Error(E->getExprLoc(),
1219 "internal error: could not emit constant value \"abstractly\"");
1220 C = CGM.EmitNullConstant(destType);
1221 }
1222 return C;
1223 }
1224
1225 llvm::Constant *
1226 ConstantEmitter::emitAbstract(SourceLocation loc, const APValue &value,
1227 QualType destType) {
1228 auto state = pushAbstract();
1229 auto C = tryEmitPrivate(value, destType);
1230 C = validateAndPopAbstract(C, state);
1231 if (!C) {
1232 CGM.Error(loc,
1233 "internal error: could not emit constant value \"abstractly\"");
1234 C = CGM.EmitNullConstant(destType);
1235 }
1236 return C;
1237 }
1238
1239 llvm::Constant *ConstantEmitter::tryEmitForInitializer(const VarDecl &D) {
1240 initializeNonAbstract(D.getType().getAddressSpace());
1241 return markIfFailed(tryEmitPrivateForVarInit(D));
1242 }
1243
1244 llvm::Constant *ConstantEmitter::tryEmitForInitializer(const Expr *E,
1245 LangAS destAddrSpace,
1246 QualType destType) {
1247 initializeNonAbstract(destAddrSpace);
1248 return markIfFailed(tryEmitPrivateForMemory(E, destType));
1249 }
1250
1251 llvm::Constant *ConstantEmitter::emitForInitializer(const APValue &value,
1252 LangAS destAddrSpace,
1253 QualType destType) {
1254 initializeNonAbstract(destAddrSpace);
1255 auto C = tryEmitPrivateForMemory(value, destType);
1256 assert(C && "couldn't emit constant value non-abstractly?");
1257 return C;
1258 }
1259
1260 llvm::GlobalValue *ConstantEmitter::getCurrentAddrPrivate() {
1261 assert(!Abstract && "cannot get current address for abstract constant");
1262
1263
1264
1265 // Make an obviously ill-formed global that should blow up compilation
1266 // if it survives.
1267 auto global = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8Ty, true,
1268 llvm::GlobalValue::PrivateLinkage,
1269 /*init*/ nullptr,
1270 /*name*/ "",
1271 /*before*/ nullptr,
1272 llvm::GlobalVariable::NotThreadLocal,
1273 CGM.getContext().getTargetAddressSpace(DestAddressSpace));
1274
1275 PlaceholderAddresses.push_back(std::make_pair(nullptr, global));
1276
1277 return global;
1278 }
1279
1280 void ConstantEmitter::registerCurrentAddrPrivate(llvm::Constant *signal,
1281 llvm::GlobalValue *placeholder) {
1282 assert(!PlaceholderAddresses.empty());
1283 assert(PlaceholderAddresses.back().first == nullptr);
1284 assert(PlaceholderAddresses.back().second == placeholder);
1285 PlaceholderAddresses.back().first = signal;
1286 }
1287
1288 namespace {
1289 struct ReplacePlaceholders {
1290 CodeGenModule &CGM;
1291
1292 /// The base address of the global.
1293 llvm::Constant *Base;
1294 llvm::Type *BaseValueTy = nullptr;
1295
1296 /// The placeholder addresses that were registered during emission.
1297 llvm::DenseMap<llvm::Constant*, llvm::GlobalVariable*> PlaceholderAddresses;
1298
1299 /// The locations of the placeholder signals.
1300 llvm::DenseMap<llvm::GlobalVariable*, llvm::Constant*> Locations;
1301
1302 /// The current index stack. We use a simple unsigned stack because
1303 /// we assume that placeholders will be relatively sparse in the
1304 /// initializer, but we cache the index values we find just in case.
1305 llvm::SmallVector<unsigned, 8> Indices;
1306 llvm::SmallVector<llvm::Constant*, 8> IndexValues;
1307
1308 ReplacePlaceholders(CodeGenModule &CGM, llvm::Constant *base,
1309 ArrayRef<std::pair<llvm::Constant*,
1310 llvm::GlobalVariable*>> addresses)
1311 : CGM(CGM), Base(base),
1312 PlaceholderAddresses(addresses.begin(), addresses.end()) {
1313 }
1314
1315 void replaceInInitializer(llvm::Constant *init) {
1316 // Remember the type of the top-most initializer.
1317 BaseValueTy = init->getType();
1318
1319 // Initialize the stack.
1320 Indices.push_back(0);
1321 IndexValues.push_back(nullptr);
1322
1323 // Recurse into the initializer.
1324 findLocations(init);
1325
1326 // Check invariants.
1327 assert(IndexValues.size() == Indices.size() && "mismatch");
1328 assert(Indices.size() == 1 && "didn't pop all indices");
1329
1330 // Do the replacement; this basically invalidates 'init'.
1331 assert(Locations.size() == PlaceholderAddresses.size() &&
1332 "missed a placeholder?");
1333
1334 // We're iterating over a hashtable, so this would be a source of
1335 // non-determinism in compiler output *except* that we're just
1336 // messing around with llvm::Constant structures, which never itself
1337 // does anything that should be visible in compiler output.
1338 for (auto &entry : Locations) {
1339 assert(entry.first->getParent() == nullptr && "not a placeholder!");
1340 entry.first->replaceAllUsesWith(entry.second);
1341 entry.first->eraseFromParent();
1342 }
1343 }
1344
1345 private:
1346 void findLocations(llvm::Constant *init) {
1347 // Recurse into aggregates.
1348 if (auto agg = dyn_cast<llvm::ConstantAggregate>(init)) {
1349 for (unsigned i = 0, e = agg->getNumOperands(); i != e; ++i) {
1350 Indices.push_back(i);
1351 IndexValues.push_back(nullptr);
1352
1353 findLocations(agg->getOperand(i));
1354
1355 IndexValues.pop_back();
1356 Indices.pop_back();
1357 }
1358 return;
1359 }
1360
1361 // Otherwise, check for registered constants.
1362 while (true) {
1363 auto it = PlaceholderAddresses.find(init);
1364 if (it != PlaceholderAddresses.end()) {
1365 setLocation(it->second);
1366 break;
1367 }
1368
1369 // Look through bitcasts or other expressions.
1370 if (auto expr = dyn_cast<llvm::ConstantExpr>(init)) {
1371 init = expr->getOperand(0);
1372 } else {
1373 break;
1374 }
1375 }
1376 }
1377
1378 void setLocation(llvm::GlobalVariable *placeholder) {
1379 assert(Locations.find(placeholder) == Locations.end() &&
1380 "already found location for placeholder!");
1381
1382 // Lazily fill in IndexValues with the values from Indices.
1383 // We do this in reverse because we should always have a strict
1384 // prefix of indices from the start.
1385 assert(Indices.size() == IndexValues.size());
1386 for (size_t i = Indices.size() - 1; i != size_t(-1); --i) {
1387 if (IndexValues[i]) {
1388 #ifndef NDEBUG
1389 for (size_t j = 0; j != i + 1; ++j) {
1390 assert(IndexValues[j] &&
1391 isa<llvm::ConstantInt>(IndexValues[j]) &&
1392 cast<llvm::ConstantInt>(IndexValues[j])->getZExtValue()
1393 == Indices[j]);
1394 }
1395 #endif
1396 break;
1397 }
1398
1399 IndexValues[i] = llvm::ConstantInt::get(CGM.Int32Ty, Indices[i]);
1400 }
1401
1402 // Form a GEP and then bitcast to the placeholder type so that the
1403 // replacement will succeed.
1404 llvm::Constant *location =
1405 llvm::ConstantExpr::getInBoundsGetElementPtr(BaseValueTy,
1406 Base, IndexValues);
1407 location = llvm::ConstantExpr::getBitCast(location,
1408 placeholder->getType());
1409
1410 Locations.insert({placeholder, location});
1411 }
1412 };
1413 }
1414
1415 void ConstantEmitter::finalize(llvm::GlobalVariable *global) {
1416 assert(InitializedNonAbstract &&
1417 "finalizing emitter that was used for abstract emission?");
1418 assert(!Finalized && "finalizing emitter multiple times");
1419 assert(global->getInitializer());
1420
1421 // Note that we might also be Failed.
1422 Finalized = true;
1423
1424 if (!PlaceholderAddresses.empty()) {
1425 ReplacePlaceholders(CGM, global, PlaceholderAddresses)
1426 .replaceInInitializer(global->getInitializer());
1427 PlaceholderAddresses.clear(); // satisfy
1428 }
1429 }
1430
1431 ConstantEmitter::~ConstantEmitter() {
1432 assert((!InitializedNonAbstract || Finalized || Failed) &&
1433 "not finalized after being initialized for non-abstract emission");
1434 assert(PlaceholderAddresses.empty() && "unhandled placeholders");
1435 }
1436
1437 static QualType getNonMemoryType(CodeGenModule &CGM, QualType type) {
1438 if (auto AT = type->getAs<AtomicType>()) {
1439 return CGM.getContext().getQualifiedType(AT->getValueType(),
1440 type.getQualifiers());
1441 }
1442 return type;
1443 }
1444
1445 llvm::Constant *ConstantEmitter::tryEmitPrivateForVarInit(const VarDecl &D) {
1446 // Make a quick check if variable can be default NULL initialized
1447 // and avoid going through rest of code which may do, for c++11,
1448 // initialization of memory to all NULLs.
1449 if (!D.hasLocalStorage()) {
1450 QualType Ty = CGM.getContext().getBaseElementType(D.getType());
1451 if (Ty->isRecordType())
1452 if (const CXXConstructExpr *E =
1453 dyn_cast_or_null<CXXConstructExpr>(D.getInit())) {
1454 const CXXConstructorDecl *CD = E->getConstructor();
1455 if (CD->isTrivial() && CD->isDefaultConstructor())
1456 return CGM.EmitNullConstant(D.getType());
1457 }
1458 InConstantContext = true;
1459 }
1460
1461 QualType destType = D.getType();
1462
1463 // Try to emit the initializer. Note that this can allow some things that
1464 // are not allowed by tryEmitPrivateForMemory alone.
1465 if (auto value = D.evaluateValue()) {
1466 return tryEmitPrivateForMemory(*value, destType);
1467 }
1468
1469 // FIXME: Implement C++11 [basic.start.init]p2: if the initializer of a
1470 // reference is a constant expression, and the reference binds to a temporary,
1471 // then constant initialization is performed. ConstExprEmitter will
1472 // incorrectly emit a prvalue constant in this case, and the calling code
1473 // interprets that as the (pointer) value of the reference, rather than the
1474 // desired value of the referee.
1475 if (destType->isReferenceType())
1476 return nullptr;
1477
1478 const Expr *E = D.getInit();
1479 assert(E && "No initializer to emit");
1480
1481 auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1482 auto C =
1483 ConstExprEmitter(*this).Visit(const_cast<Expr*>(E), nonMemoryDestType);
1484 return (C ? emitForMemory(C, destType) : nullptr);
1485 }
1486
1487 llvm::Constant *
1488 ConstantEmitter::tryEmitAbstractForMemory(const Expr *E, QualType destType) {
1489 auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1490 auto C = tryEmitAbstract(E, nonMemoryDestType);
1491 return (C ? emitForMemory(C, destType) : nullptr);
1492 }
1493
1494 llvm::Constant *
1495 ConstantEmitter::tryEmitAbstractForMemory(const APValue &value,
1496 QualType destType) {
1497 auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1498 auto C = tryEmitAbstract(value, nonMemoryDestType);
1499 return (C ? emitForMemory(C, destType) : nullptr);
1500 }
1501
1502 llvm::Constant *ConstantEmitter::tryEmitPrivateForMemory(const Expr *E,
1503 QualType destType) {
1504 auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1505 llvm::Constant *C = tryEmitPrivate(E, nonMemoryDestType);
1506 return (C ? emitForMemory(C, destType) : nullptr);
1507 }
1508
1509 llvm::Constant *ConstantEmitter::tryEmitPrivateForMemory(const APValue &value,
1510 QualType destType) {
1511 auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1512 auto C = tryEmitPrivate(value, nonMemoryDestType);
1513 return (C ? emitForMemory(C, destType) : nullptr);
1514 }
1515
1516 llvm::Constant *ConstantEmitter::emitForMemory(CodeGenModule &CGM,
1517 llvm::Constant *C,
1518 QualType destType) {
1519 // For an _Atomic-qualified constant, we may need to add tail padding.
1520 if (auto AT = destType->getAs<AtomicType>()) {
1521 QualType destValueType = AT->getValueType();
1522 C = emitForMemory(CGM, C, destValueType);
1523
1524 uint64_t innerSize = CGM.getContext().getTypeSize(destValueType);
1525 uint64_t outerSize = CGM.getContext().getTypeSize(destType);
1526 if (innerSize == outerSize)
1527 return C;
1528
1529 assert(innerSize < outerSize && "emitted over-large constant for atomic");
1530 llvm::Constant *elts[] = {
1531 C,
1532 llvm::ConstantAggregateZero::get(
1533 llvm::ArrayType::get(CGM.Int8Ty, (outerSize - innerSize) / 8))
1534 };
1535 return llvm::ConstantStruct::getAnon(elts);
1536 }
1537
1538 // Zero-extend bool.
1539 if (C->getType()->isIntegerTy(1)) {
1540 llvm::Type *boolTy = CGM.getTypes().ConvertTypeForMem(destType);
1541 return llvm::ConstantExpr::getZExt(C, boolTy);
1542 }
1543
1544 return C;
1545 }
1546
1547 llvm::Constant *ConstantEmitter::tryEmitPrivate(const Expr *E,
1548 QualType destType) {
1549 Expr::EvalResult Result;
1550
1551 bool Success = false;
1552
1553 if (destType->isReferenceType())
1554 Success = E->EvaluateAsLValue(Result, CGM.getContext());
1555 else
1556 Success = E->EvaluateAsRValue(Result, CGM.getContext(), InConstantContext);
1557
1558 llvm::Constant *C;
1559 if (Success && !Result.HasSideEffects)
1560 C = tryEmitPrivate(Result.Val, destType);
1561 else
1562 C = ConstExprEmitter(*this).Visit(const_cast<Expr*>(E), destType);
1563
1564 return C;
1565 }
1566
1567 llvm::Constant *CodeGenModule::getNullPointer(llvm::PointerType *T, QualType QT) {
1568 return getTargetCodeGenInfo().getNullPointer(*this, T, QT);
1569 }
1570
1571 namespace {
1572 /// A struct which can be used to peephole certain kinds of finalization
1573 /// that normally happen during l-value emission.
1574 struct ConstantLValue {
1575 llvm::Constant *Value;
1576 bool HasOffsetApplied;
1577
1578 /*implicit*/ ConstantLValue(llvm::Constant *value,
1579 bool hasOffsetApplied = false)
1580 : Value(value), HasOffsetApplied(false) {}
1581
1582 /*implicit*/ ConstantLValue(ConstantAddress address)
1583 : ConstantLValue(address.getPointer()) {}
1584 };
1585
1586 /// A helper class for emitting constant l-values.
1587 class ConstantLValueEmitter : public ConstStmtVisitor<ConstantLValueEmitter,
1588 ConstantLValue> {
1589 CodeGenModule &CGM;
1590 ConstantEmitter &Emitter;
1591 const APValue &Value;
1592 QualType DestType;
1593
1594 // Befriend StmtVisitorBase so that we don't have to expose Visit*.
1595 friend StmtVisitorBase;
1596
1597 public:
1598 ConstantLValueEmitter(ConstantEmitter &emitter, const APValue &value,
1599 QualType destType)
1600 : CGM(emitter.CGM), Emitter(emitter), Value(value), DestType(destType) {}
1601
1602 llvm::Constant *tryEmit();
1603
1604 private:
1605 llvm::Constant *tryEmitAbsolute(llvm::Type *destTy);
1606 ConstantLValue tryEmitBase(const APValue::LValueBase &base);
1607
1608 ConstantLValue VisitStmt(const Stmt *S) { return nullptr; }
1609 ConstantLValue VisitConstantExpr(const ConstantExpr *E);
1610 ConstantLValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
1611 ConstantLValue VisitStringLiteral(const StringLiteral *E);
1612 ConstantLValue VisitObjCEncodeExpr(const ObjCEncodeExpr *E);
1613 ConstantLValue VisitObjCStringLiteral(const ObjCStringLiteral *E);
1614 ConstantLValue VisitPredefinedExpr(const PredefinedExpr *E);
1615 ConstantLValue VisitAddrLabelExpr(const AddrLabelExpr *E);
1616 ConstantLValue VisitCallExpr(const CallExpr *E);
1617 ConstantLValue VisitBlockExpr(const BlockExpr *E);
1618 ConstantLValue VisitCXXTypeidExpr(const CXXTypeidExpr *E);
1619 ConstantLValue VisitCXXUuidofExpr(const CXXUuidofExpr *E);
1620 ConstantLValue VisitMaterializeTemporaryExpr(
1621 const MaterializeTemporaryExpr *E);
1622
1623 bool hasNonZeroOffset() const {
1624 return !Value.getLValueOffset().isZero();
1625 }
1626
1627 /// Return the value offset.
1628 llvm::Constant *getOffset() {
1629 return llvm::ConstantInt::get(CGM.Int64Ty,
1630 Value.getLValueOffset().getQuantity());
1631 }
1632
1633 /// Apply the value offset to the given constant.
1634 llvm::Constant *applyOffset(llvm::Constant *C) {
1635 if (!hasNonZeroOffset())
1636 return C;
1637
1638 llvm::Type *origPtrTy = C->getType();
1639 unsigned AS = origPtrTy->getPointerAddressSpace();
1640 llvm::Type *charPtrTy = CGM.Int8Ty->getPointerTo(AS);
1641 C = llvm::ConstantExpr::getBitCast(C, charPtrTy);
1642 C = llvm::ConstantExpr::getGetElementPtr(CGM.Int8Ty, C, getOffset());
1643 C = llvm::ConstantExpr::getPointerCast(C, origPtrTy);
1644 return C;
1645 }
1646 };
1647
1648 }
1649
1650 llvm::Constant *ConstantLValueEmitter::tryEmit() {
1651 const APValue::LValueBase &base = Value.getLValueBase();
1652
1653 // Certain special array initializers are represented in APValue
1654 // as l-values referring to the base expression which generates the
1655 // array. This happens with e.g. string literals. These should
1656 // probably just get their own representation kind in APValue.
1657 if (DestType->isArrayType()) {
1658 assert(!hasNonZeroOffset() && "offset on array initializer");
1659 auto expr = const_cast<Expr*>(base.get<const Expr*>());
1660 return ConstExprEmitter(Emitter).Visit(expr, DestType);
1661 }
1662
1663 // Otherwise, the destination type should be a pointer or reference
1664 // type, but it might also be a cast thereof.
1665 //
1666 // FIXME: the chain of casts required should be reflected in the APValue.
1667 // We need this in order to correctly handle things like a ptrtoint of a
1668 // non-zero null pointer and addrspace casts that aren't trivially
1669 // represented in LLVM IR.
1670 auto destTy = CGM.getTypes().ConvertTypeForMem(DestType);
1671 assert(isa<llvm::IntegerType>(destTy) || isa<llvm::PointerType>(destTy));
1672
1673 // If there's no base at all, this is a null or absolute pointer,
1674 // possibly cast back to an integer type.
1675 if (!base) {
1676 return tryEmitAbsolute(destTy);
1677 }
1678
1679 // Otherwise, try to emit the base.
1680 ConstantLValue result = tryEmitBase(base);
1681
1682 // If that failed, we're done.
1683 llvm::Constant *value = result.Value;
1684 if (!value) return nullptr;
1685
1686 // Apply the offset if necessary and not already done.
1687 if (!result.HasOffsetApplied) {
1688 value = applyOffset(value);
1689 }
1690
1691 // Convert to the appropriate type; this could be an lvalue for
1692 // an integer. FIXME: performAddrSpaceCast
1693 if (isa<llvm::PointerType>(destTy))
1694 return llvm::ConstantExpr::getPointerCast(value, destTy);
1695
1696 return llvm::ConstantExpr::getPtrToInt(value, destTy);
1697 }
1698
1699 /// Try to emit an absolute l-value, such as a null pointer or an integer
1700 /// bitcast to pointer type.
1701 llvm::Constant *
1702 ConstantLValueEmitter::tryEmitAbsolute(llvm::Type *destTy) {
1703 auto offset = getOffset();
1704
1705 // If we're producing a pointer, this is easy.
1706 if (auto destPtrTy = cast<llvm::PointerType>(destTy)) {
1707 if (Value.isNullPointer()) {
1708 // FIXME: integer offsets from non-zero null pointers.
1709 return CGM.getNullPointer(destPtrTy, DestType);
1710 }
1711
1712 // Convert the integer to a pointer-sized integer before converting it
1713 // to a pointer.
1714 // FIXME: signedness depends on the original integer type.
1715 auto intptrTy = CGM.getDataLayout().getIntPtrType(destPtrTy);
1716 llvm::Constant *C = offset;
1717 C = llvm::ConstantExpr::getIntegerCast(getOffset(), intptrTy,
1718 /*isSigned*/ false);
1719 C = llvm::ConstantExpr::getIntToPtr(C, destPtrTy);
1720 return C;
1721 }
1722
1723 // Otherwise, we're basically returning an integer constant.
1724
1725 // FIXME: this does the wrong thing with ptrtoint of a null pointer,
1726 // but since we don't know the original pointer type, there's not much
1727 // we can do about it.
1728
1729 auto C = getOffset();
1730 C = llvm::ConstantExpr::getIntegerCast(C, destTy, /*isSigned*/ false);
1731 return C;
1732 }
1733
1734 ConstantLValue
1735 ConstantLValueEmitter::tryEmitBase(const APValue::LValueBase &base) {
1736 // Handle values.
1737 if (const ValueDecl *D = base.dyn_cast<const ValueDecl*>()) {
1738 if (D->hasAttr<WeakRefAttr>())
1739 return CGM.GetWeakRefReference(D).getPointer();
1740
1741 if (auto FD = dyn_cast<FunctionDecl>(D))
1742 return CGM.GetAddrOfFunction(FD);
1743
1744 if (auto VD = dyn_cast<VarDecl>(D)) {
1745 // We can never refer to a variable with local storage.
1746 if (!VD->hasLocalStorage()) {
1747 if (VD->isFileVarDecl() || VD->hasExternalStorage())
1748 return CGM.GetAddrOfGlobalVar(VD);
1749
1750 if (VD->isLocalVarDecl()) {
1751 return CGM.getOrCreateStaticVarDecl(
1752 *VD, CGM.getLLVMLinkageVarDefinition(VD, /*isConstant=*/false));
1753 }
1754 }
1755 }
1756
1757 return nullptr;
1758 }
1759
1760 // Otherwise, it must be an expression.
1761 return Visit(base.get<const Expr*>());
1762 }
1763
1764 ConstantLValue
1765 ConstantLValueEmitter::VisitConstantExpr(const ConstantExpr *E) {
1766 return Visit(E->getSubExpr());
1767 }
1768
1769 ConstantLValue
1770 ConstantLValueEmitter::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
1771 return tryEmitGlobalCompoundLiteral(CGM, Emitter.CGF, E);
1772 }
1773
1774 ConstantLValue
1775 ConstantLValueEmitter::VisitStringLiteral(const StringLiteral *E) {
1776 return CGM.GetAddrOfConstantStringFromLiteral(E);
1777 }
1778
1779 ConstantLValue
1780 ConstantLValueEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
1781 return CGM.GetAddrOfConstantStringFromObjCEncode(E);
1782 }
1783
1784 ConstantLValue
1785 ConstantLValueEmitter::VisitObjCStringLiteral(const ObjCStringLiteral *E) {
1786 auto C = CGM.getObjCRuntime().GenerateConstantString(E->getString());
1787 return C.getElementBitCast(CGM.getTypes().ConvertTypeForMem(E->getType()));
1788 }
1789
1790 ConstantLValue
1791 ConstantLValueEmitter::VisitPredefinedExpr(const PredefinedExpr *E) {
1792 if (auto CGF = Emitter.CGF) {
1793 LValue Res = CGF->EmitPredefinedLValue(E);
1794 return cast<ConstantAddress>(Res.getAddress());
1795 }
1796
1797 auto kind = E->getIdentKind();
1798 if (kind == PredefinedExpr::PrettyFunction) {
1799 return CGM.GetAddrOfConstantCString("top level", ".tmp");
1800 }
1801
1802 return CGM.GetAddrOfConstantCString("", ".tmp");
1803 }
1804
1805 ConstantLValue
1806 ConstantLValueEmitter::VisitAddrLabelExpr(const AddrLabelExpr *E) {
1807 assert(Emitter.CGF && "Invalid address of label expression outside function");
1808 llvm::Constant *Ptr = Emitter.CGF->GetAddrOfLabel(E->getLabel());
1809 Ptr = llvm::ConstantExpr::getBitCast(Ptr,
1810 CGM.getTypes().ConvertType(E->getType()));
1811 return Ptr;
1812 }
1813
1814 ConstantLValue
1815 ConstantLValueEmitter::VisitCallExpr(const CallExpr *E) {
1816 unsigned builtin = E->getBuiltinCallee();
1817 if (builtin != Builtin::BI__builtin___CFStringMakeConstantString &&
1818 builtin != Builtin::BI__builtin___NSStringMakeConstantString)
1819 return nullptr;
1820
1821 auto literal = cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts());
1822 if (builtin == Builtin::BI__builtin___NSStringMakeConstantString) {
1823 return CGM.getObjCRuntime().GenerateConstantString(literal);
1824 } else {
1825 // FIXME: need to deal with UCN conversion issues.
1826 return CGM.GetAddrOfConstantCFString(literal);
1827 }
1828 }
1829
1830 ConstantLValue
1831 ConstantLValueEmitter::VisitBlockExpr(const BlockExpr *E) {
1832 StringRef functionName;
1833 if (auto CGF = Emitter.CGF)
1834 functionName = CGF->CurFn->getName();
1835 else
1836 functionName = "global";
1837
1838 return CGM.GetAddrOfGlobalBlock(E, functionName);
1839 }
1840
1841 ConstantLValue
1842 ConstantLValueEmitter::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
1843 QualType T;
1844 if (E->isTypeOperand())
1845 T = E->getTypeOperand(CGM.getContext());
1846 else
1847 T = E->getExprOperand()->getType();
1848 return CGM.GetAddrOfRTTIDescriptor(T);
1849 }
1850
1851 ConstantLValue
1852 ConstantLValueEmitter::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {
1853 return CGM.GetAddrOfUuidDescriptor(E);
1854 }
1855
1856 ConstantLValue
1857 ConstantLValueEmitter::VisitMaterializeTemporaryExpr(
1858 const MaterializeTemporaryExpr *E) {
1859 assert(E->getStorageDuration() == SD_Static);
1860 SmallVector<const Expr *, 2> CommaLHSs;
1861 SmallVector<SubobjectAdjustment, 2> Adjustments;
1862 const Expr *Inner = E->GetTemporaryExpr()
1863 ->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments);
1864 return CGM.GetAddrOfGlobalTemporary(E, Inner);
1865 }
1866
1867 llvm::Constant *ConstantEmitter::tryEmitPrivate(const APValue &Value,
1868 QualType DestType) {
1869 switch (Value.getKind()) {
1870 case APValue::Uninitialized:
1871 llvm_unreachable("Constant expressions should be initialized.");
1872 case APValue::LValue:
1873 return ConstantLValueEmitter(*this, Value, DestType).tryEmit();
1874 case APValue::Int:
1875 return llvm::ConstantInt::get(CGM.getLLVMContext(), Value.getInt());
1876 case APValue::ComplexInt: {
1877 llvm::Constant *Complex[2];
1878
1879 Complex[0] = llvm::ConstantInt::get(CGM.getLLVMContext(),
1880 Value.getComplexIntReal());
1881 Complex[1] = llvm::ConstantInt::get(CGM.getLLVMContext(),
1882 Value.getComplexIntImag());
1883
1884 // FIXME: the target may want to specify that this is packed.
1885 llvm::StructType *STy =
1886 llvm::StructType::get(Complex[0]->getType(), Complex[1]->getType());
1887 return llvm::ConstantStruct::get(STy, Complex);
1888 }
1889 case APValue::Float: {
1890 const llvm::APFloat &Init = Value.getFloat();
1891 if (&Init.getSemantics() == &llvm::APFloat::IEEEhalf() &&
1892 !CGM.getContext().getLangOpts().NativeHalfType &&
1893 CGM.getContext().getTargetInfo().useFP16ConversionIntrinsics())
1894 return llvm::ConstantInt::get(CGM.getLLVMContext(),
1895 Init.bitcastToAPInt());
1896 else
1897 return llvm::ConstantFP::get(CGM.getLLVMContext(), Init);
1898 }
1899 case APValue::ComplexFloat: {
1900 llvm::Constant *Complex[2];
1901
1902 Complex[0] = llvm::ConstantFP::get(CGM.getLLVMContext(),
1903 Value.getComplexFloatReal());
1904 Complex[1] = llvm::ConstantFP::get(CGM.getLLVMContext(),
1905 Value.getComplexFloatImag());
1906
1907 // FIXME: the target may want to specify that this is packed.
1908 llvm::StructType *STy =
1909 llvm::StructType::get(Complex[0]->getType(), Complex[1]->getType());
1910 return llvm::ConstantStruct::get(STy, Complex);
1911 }
1912 case APValue::Vector: {
1913 unsigned NumElts = Value.getVectorLength();
1914 SmallVector<llvm::Constant *, 4> Inits(NumElts);
1915
1916 for (unsigned I = 0; I != NumElts; ++I) {
1917 const APValue &Elt = Value.getVectorElt(I);
1918 if (Elt.isInt())
1919 Inits[I] = llvm::ConstantInt::get(CGM.getLLVMContext(), Elt.getInt());
1920 else if (Elt.isFloat())
1921 Inits[I] = llvm::ConstantFP::get(CGM.getLLVMContext(), Elt.getFloat());
1922 else
1923 llvm_unreachable("unsupported vector element type");
1924 }
1925 return llvm::ConstantVector::get(Inits);
1926 }
1927 case APValue::AddrLabelDiff: {
1928 const AddrLabelExpr *LHSExpr = Value.getAddrLabelDiffLHS();
1929 const AddrLabelExpr *RHSExpr = Value.getAddrLabelDiffRHS();
1930 llvm::Constant *LHS = tryEmitPrivate(LHSExpr, LHSExpr->getType());
1931 llvm::Constant *RHS = tryEmitPrivate(RHSExpr, RHSExpr->getType());
1932 if (!LHS || !RHS) return nullptr;
1933
1934 // Compute difference
1935 llvm::Type *ResultType = CGM.getTypes().ConvertType(DestType);
1936 LHS = llvm::ConstantExpr::getPtrToInt(LHS, CGM.IntPtrTy);
1937 RHS = llvm::ConstantExpr::getPtrToInt(RHS, CGM.IntPtrTy);
1938 llvm::Constant *AddrLabelDiff = llvm::ConstantExpr::getSub(LHS, RHS);
1939
1940 // LLVM is a bit sensitive about the exact format of the
1941 // address-of-label difference; make sure to truncate after
1942 // the subtraction.
1943 return llvm::ConstantExpr::getTruncOrBitCast(AddrLabelDiff, ResultType);
1944 }
1945 case APValue::Struct:
1946 case APValue::Union:
1947 return ConstStructBuilder::BuildStruct(*this, Value, DestType);
1948 case APValue::Array: {
1949 const ConstantArrayType *CAT =
1950 CGM.getContext().getAsConstantArrayType(DestType);
1951 unsigned NumElements = Value.getArraySize();
1952 unsigned NumInitElts = Value.getArrayInitializedElts();
1953
1954 // Emit array filler, if there is one.
1955 llvm::Constant *Filler = nullptr;
1956 if (Value.hasArrayFiller()) {
1957 Filler = tryEmitAbstractForMemory(Value.getArrayFiller(),
1958 CAT->getElementType());
1959 if (!Filler)
1960 return nullptr;
1961 }
1962
1963 // Emit initializer elements.
1964 SmallVector<llvm::Constant*, 16> Elts;
1965 if (Filler && Filler->isNullValue())
1966 Elts.reserve(NumInitElts + 1);
1967 else
1968 Elts.reserve(NumElements);
1969
1970 llvm::Type *CommonElementType = nullptr;
1971 for (unsigned I = 0; I < NumInitElts; ++I) {
1972 llvm::Constant *C = tryEmitPrivateForMemory(
1973 Value.getArrayInitializedElt(I), CAT->getElementType());
1974 if (!C) return nullptr;
1975
1976 if (I == 0)
1977 CommonElementType = C->getType();
1978 else if (C->getType() != CommonElementType)
1979 CommonElementType = nullptr;
1980 Elts.push_back(C);
1981 }
1982
1983 // This means that the array type is probably "IncompleteType" or some
1984 // type that is not ConstantArray.
1985 if (CAT == nullptr && CommonElementType == nullptr && !NumInitElts) {
1986 const ArrayType *AT = CGM.getContext().getAsArrayType(DestType);
1987 CommonElementType = CGM.getTypes().ConvertType(AT->getElementType());
1988 llvm::ArrayType *AType = llvm::ArrayType::get(CommonElementType,
1989 NumElements);
1990 return llvm::ConstantAggregateZero::get(AType);
1991 }
1992
1993 return EmitArrayConstant(CGM, CAT, CommonElementType, NumElements, Elts,
1994 Filler);
1995 }
1996 case APValue::MemberPointer:
1997 return CGM.getCXXABI().EmitMemberPointer(Value, DestType);
1998 }
1999 llvm_unreachable("Unknown APValue kind");
2000 }
2001
2002 llvm::GlobalVariable *CodeGenModule::getAddrOfConstantCompoundLiteralIfEmitted(
2003 const CompoundLiteralExpr *E) {
2004 return EmittedCompoundLiterals.lookup(E);
2005 }
2006
2007 void CodeGenModule::setAddrOfConstantCompoundLiteral(
2008 const CompoundLiteralExpr *CLE, llvm::GlobalVariable *GV) {
2009 bool Ok = EmittedCompoundLiterals.insert(std::make_pair(CLE, GV)).second;
2010 (void)Ok;
2011 assert(Ok && "CLE has already been emitted!");
2012 }
2013
2014 ConstantAddress
2015 CodeGenModule::GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *E) {
2016 assert(E->isFileScope() && "not a file-scope compound literal expr");
2017 return tryEmitGlobalCompoundLiteral(*this, nullptr, E);
2018 }
2019
2020 llvm::Constant *
2021 CodeGenModule::getMemberPointerConstant(const UnaryOperator *uo) {
2022 // Member pointer constants always have a very particular form.
2023 const MemberPointerType *type = cast<MemberPointerType>(uo->getType());
2024 const ValueDecl *decl = cast<DeclRefExpr>(uo->getSubExpr())->getDecl();
2025
2026 // A member function pointer.
2027 if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(decl))
2028 return getCXXABI().EmitMemberFunctionPointer(method);
2029
2030 // Otherwise, a member data pointer.
2031 uint64_t fieldOffset = getContext().getFieldOffset(decl);
2032 CharUnits chars = getContext().toCharUnitsFromBits((int64_t) fieldOffset);
2033 return getCXXABI().EmitMemberDataPointer(type, chars);
2034 }
2035
2036 static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
2037 llvm::Type *baseType,
2038 const CXXRecordDecl *base);
2039
2040 static llvm::Constant *EmitNullConstant(CodeGenModule &CGM,
2041 const RecordDecl *record,
2042 bool asCompleteObject) {
2043 const CGRecordLayout &layout = CGM.getTypes().getCGRecordLayout(record);
2044 llvm::StructType *structure =
2045 (asCompleteObject ? layout.getLLVMType()
2046 : layout.getBaseSubobjectLLVMType());
2047
2048 unsigned numElements = structure->getNumElements();
2049 std::vector<llvm::Constant *> elements(numElements);
2050
2051 auto CXXR = dyn_cast<CXXRecordDecl>(record);
2052 // Fill in all the bases.
2053 if (CXXR) {
2054 for (const auto &I : CXXR->bases()) {
2055 if (I.isVirtual()) {
2056 // Ignore virtual bases; if we're laying out for a complete
2057 // object, we'll lay these out later.
2058 continue;
2059 }
2060
2061 const CXXRecordDecl *base =
2062 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
2063
2064 // Ignore empty bases.
2065 if (base->isEmpty() ||
2066 CGM.getContext().getASTRecordLayout(base).getNonVirtualSize()
2067 .isZero())
2068 continue;
2069
2070 unsigned fieldIndex = layout.getNonVirtualBaseLLVMFieldNo(base);
2071 llvm::Type *baseType = structure->getElementType(fieldIndex);
2072 elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
2073 }
2074 }
2075
2076 // Fill in all the fields.
2077 for (const auto *Field : record->fields()) {
2078 // Fill in non-bitfields. (Bitfields always use a zero pattern, which we
2079 // will fill in later.)
2080 if (!Field->isBitField()) {
2081 unsigned fieldIndex = layout.getLLVMFieldNo(Field);
2082 elements[fieldIndex] = CGM.EmitNullConstant(Field->getType());
2083 }
2084
2085 // For unions, stop after the first named field.
2086 if (record->isUnion()) {
2087 if (Field->getIdentifier())
2088 break;
2089 if (const auto *FieldRD = Field->getType()->getAsRecordDecl())
2090 if (FieldRD->findFirstNamedDataMember())
2091 break;
2092 }
2093 }
2094
2095 // Fill in the virtual bases, if we're working with the complete object.
2096 if (CXXR && asCompleteObject) {
2097 for (const auto &I : CXXR->vbases()) {
2098 const CXXRecordDecl *base =
2099 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
2100
2101 // Ignore empty bases.
2102 if (base->isEmpty())
2103 continue;
2104
2105 unsigned fieldIndex = layout.getVirtualBaseIndex(base);
2106
2107 // We might have already laid this field out.
2108 if (elements[fieldIndex]) continue;
2109
2110 llvm::Type *baseType = structure->getElementType(fieldIndex);
2111 elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
2112 }
2113 }
2114
2115 // Now go through all other fields and zero them out.
2116 for (unsigned i = 0; i != numElements; ++i) {
2117 if (!elements[i])
2118 elements[i] = llvm::Constant::getNullValue(structure->getElementType(i));
2119 }
2120
2121 return llvm::ConstantStruct::get(structure, elements);
2122 }
2123
2124 /// Emit the null constant for a base subobject.
2125 static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
2126 llvm::Type *baseType,
2127 const CXXRecordDecl *base) {
2128 const CGRecordLayout &baseLayout = CGM.getTypes().getCGRecordLayout(base);
2129
2130 // Just zero out bases that don't have any pointer to data members.
2131 if (baseLayout.isZeroInitializableAsBase())
2132 return llvm::Constant::getNullValue(baseType);
2133
2134 // Otherwise, we can just use its null constant.
2135 return EmitNullConstant(CGM, base, /*asCompleteObject=*/false);
2136 }
2137
2138 llvm::Constant *ConstantEmitter::emitNullForMemory(CodeGenModule &CGM,
2139 QualType T) {
2140 return emitForMemory(CGM, CGM.EmitNullConstant(T), T);
2141 }
2142
2143 llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) {
2144 if (T->getAs<PointerType>())
2145 return getNullPointer(
2146 cast<llvm::PointerType>(getTypes().ConvertTypeForMem(T)), T);
2147
2148 if (getTypes().isZeroInitializable(T))
2149 return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T));
2150
2151 if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) {
2152 llvm::ArrayType *ATy =
2153 cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T));
2154
2155 QualType ElementTy = CAT->getElementType();
2156
2157 llvm::Constant *Element =
2158 ConstantEmitter::emitNullForMemory(*this, ElementTy);
2159 unsigned NumElements = CAT->getSize().getZExtValue();
2160 SmallVector<llvm::Constant *, 8> Array(NumElements, Element);
2161 return llvm::ConstantArray::get(ATy, Array);
2162 }
2163
2164 if (const RecordType *RT = T->getAs<RecordType>())
2165 return ::EmitNullConstant(*this, RT->getDecl(), /*complete object*/ true);
2166
2167 assert(T->isMemberDataPointerType() &&
2168 "Should only see pointers to data members here!");
2169
2170 return getCXXABI().EmitNullMemberPointer(T->castAs<MemberPointerType>());
2171 }
2172
2173 llvm::Constant *
2174 CodeGenModule::EmitNullConstantForBase(const CXXRecordDecl *Record) {
2175 return ::EmitNullConstant(*this, Record, false);
2176 }
clang::CodeGen::CodeGenModule::getDataLayout
const llvm::DataLayout & getDataLayout() const
Definition: CodeGenModule.h:661
clang::FullExpr::getSubExpr
const Expr * getSubExpr() const
Definition: Expr.h:890
clang::ast_matchers::type
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
Definition: ASTMatchersInternal.cpp:592
ASTContext.h
Defines the clang::ASTContext interface.
clang::CharUnits::alignTo
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
clang::ChooseExpr::getChosenSubExpr
Expr * getChosenSubExpr() const
getChosenSubExpr - Return the subexpression chosen according to the condition.
Definition: Expr.h:4018
clang::CodeGen::CGCXXABI::EmitMemberPointer
virtual llvm::Constant * EmitMemberPointer(const APValue &MP, QualType MPT)
Create a member pointer for the given member pointer constant.
Definition: CGCXXABI.cpp:119
clang::PointerType
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition: Type.h:2537
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:638
clang::SD_Static
Static storage duration.
Definition: Specifiers.h:281
clang::CodeGen::CodeGenModule::getTypes
CodeGenTypes & getTypes()
Definition: CodeGenModule.h:676
clang::Type::isArrayType
bool isArrayType() const
Definition: Type.h:6345
clang::CodeGen::ConstantEmitter::emitForInitializer
llvm::Constant * emitForInitializer(const APValue &value, LangAS destAddrSpace, QualType destType)
Definition: CGExprConstant.cpp:1251
clang::CallExpr::getArg
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition: Expr.h:2553
clang::Expr::skipRValueSubobjectAdjustments
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:76
clang::ConstStmtVisitor
ConstStmtVisitor - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:193
size_t
__SIZE_TYPE__ size_t
The unsigned integer type of the result of the sizeof operator.
Definition: opencl-c.h:68
clang::CodeGen::CGRecordLayout
CGRecordLayout - This class handles struct and union layout info while lowering AST types to LLVM typ...
Definition: CGRecordLayout.h:109
clang::Type::isMemberDataPointerType
bool isMemberDataPointerType() const
Definition: Type.h:6338
clang::CodeGen::CodeGenModule::getMemberPointerConstant
llvm::Constant * getMemberPointerConstant(const UnaryOperator *e)
Definition: CGExprConstant.cpp:2021
clang::CodeGen::CodeGenModule::getLLVMContext
llvm::LLVMContext & getLLVMContext()
Definition: CodeGenModule.h:670
clang::InitListExpr::getInit
const Expr * getInit(unsigned Init) const
Definition: Expr.h:4233
clang::CodeGen::CodeGenModule::GetAddrOfConstantStringFromObjCEncode
ConstantAddress GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *)
Return a pointer to a constant array for the given ObjCEncodeExpr node.
Definition: CodeGenModule.cpp:4543
clang::ASTContext::getASTRecordLayout
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D...
Definition: RecordLayoutBuilder.cpp:2966
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:66
clang::CodeGen::ConstantEmitter::tryEmitForInitializer
llvm::Constant * tryEmitForInitializer(const VarDecl &D)
Try to emit the initiaizer of the given declaration as an abstract constant.
Definition: CGExprConstant.cpp:1239
clang::Type::getPointeeType
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee...
Definition: Type.cpp:505
clang::ASTRecordLayout::getBaseClassOffset
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
Definition: RecordLayout.h:233
clang::CodeGen::StructorType::Base
Base
trunc
#define trunc(__x)
Definition: tgmath.h:1232
clang::Type::isRecordType
bool isRecordType() const
Definition: Type.h:6369
clang::ASTContext::getQualifiedType
QualType getQualifiedType(SplitQualType split) const
Un-split a SplitQualType.
Definition: ASTContext.h:1933
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:87
clang::CodeGen::ConstantEmitter::emitForMemory
llvm::Constant * emitForMemory(llvm::Constant *C, QualType T)
Definition: ConstantEmitter.h:117
clang::InitListExpr::isTransparent
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:2059
clang::ParenExpr
ParenExpr - This represents a parethesized expression, e.g.
Definition: Expr.h:1844
clang::ArrayType
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:2812
clang::CXXConstructExpr
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1262
clang::CharUnits::isZero
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition: CharUnits.h:116
clang::ASTContext::getTargetInfo
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:690
clang::CodeGen::AlignmentSource::Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
clang::PredefinedExpr::getIdentKind
IdentKind getIdentKind() const
Definition: Expr.h:1806
clang::APValue::LValueBase
Definition: APValue.h:57
clang::CodeGen::CodeGenTypeCache::Int8Ty
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
Definition: CodeGenTypeCache.h:38
clang::CXXConstructorDecl
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2484
clang::MaterializeTemporaryExpr
Represents a prvalue temporary that is written into memory so that a reference can bind to it...
Definition: ExprCXX.h:4156
clang::GenericSelectionExpr::getResultExpr
const Expr * getResultExpr() const
The generic selection&#39;s result expression.
Definition: Expr.h:5086
clang::APValue::getAddrLabelDiffLHS
const AddrLabelExpr * getAddrLabelDiffLHS() const
Definition: APValue.h:392
clang::ArrayType::getElementType
QualType getElementType() const
Definition: Type.h:2847
Value
Value
Definition: UninitializedValues.cpp:102
clang::CXXConstructorDecl::isDefaultConstructor
bool isDefaultConstructor() const
Whether this constructor is a default constructor (C++ [class.ctor]p5), which can be used to default-...
Definition: DeclCXX.cpp:2356
clang::CodeGen::ConstantEmitter::tryEmitPrivateForVarInit
llvm::Constant * tryEmitPrivateForVarInit(const VarDecl &D)
Definition: CGExprConstant.cpp:1445
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:813
clang::CompoundLiteralExpr
CompoundLiteralExpr - [C99 6.5.2.5].
Definition: Expr.h:2925
clang::ast_matchers::expr
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
Definition: ASTMatchersInternal.cpp:690
clang::APValue::getComplexFloatReal
APFloat & getComplexFloatReal()
Definition: APValue.h:284
clang::Type::getAs
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:6748
clang::TargetInfo::getCharAlign
unsigned getCharAlign() const
Definition: TargetInfo.h:372
clang::LangAS
LangAS
Defines the address space values used by the address space qualifier of QualType. ...
Definition: AddressSpaces.h:26
clang::CodeGen::ConstantEmitter::CGM
CodeGenModule & CGM
Definition: ConstantEmitter.h:26
clang::APValue::LValueBase::get
T get() const
Definition: APValue.h:71
clang::CodeGen::CodeGenTypes::ConvertTypeForMem
llvm::Type * ConvertTypeForMem(QualType T)
ConvertTypeForMem - Convert type T into a llvm::Type.
Definition: CodeGenTypes.cpp:87
clang::CXXTypeidExpr::getExprOperand
Expr * getExprOperand() const
Definition: ExprCXX.h:726
clang::ExprWithCleanups
Represents an expression – generally a full-expression – that introduces cleanups to be run at the ...
Definition: ExprCXX.h:3089
clang::CodeGen::CGRecordLayout::isZeroInitializableAsBase
bool isZeroInitializableAsBase() const
Check whether this struct can be C++ zero-initialized with a zeroinitializer when considered as a bas...
Definition: CGRecordLayout.h:179
clang::NamedDecl::getIdentifier
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:270
clang::RecordDecl
Represents a struct/union/class.
Definition: Decl.h:3593
clang::CXXRecordDecl::isEmpty
bool isEmpty() const
Determine whether this is an empty class in the sense of (C++11 [meta.unary.prop]).
Definition: DeclCXX.h:1327
clang::MaterializeTemporaryExpr::GetTemporaryExpr
Expr * GetTemporaryExpr() const
Retrieve the temporary-generating subexpression whose value will be materialized into a glvalue...
Definition: ExprCXX.h:4197
CGRecordLayout.h
clang::CodeGen::LValue::getAddress
Address getAddress() const
Definition: CGValue.h:327
clang::ExprWithCleanups::cleanupsHaveSideEffects
bool cleanupsHaveSideEffects() const
Definition: ExprCXX.h:3127
clang::APValue::ComplexInt
Definition: APValue.h:46
clang::CodeGen::CodeGenFunction
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
Definition: CodeGenFunction.h:227
clang::CodeGen::CGCXXABI::getVTableAddressPointForConstExpr
virtual llvm::Constant * getVTableAddressPointForConstExpr(BaseSubobject Base, const CXXRecordDecl *VTableClass)=0
Get the address point of the vtable for the given base subobject while building a constexpr...
clang::CodeGen::CodeGenTypes::ConvertType
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
Definition: CodeGenTypes.cpp:383
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:155
clang::CodeGen::ConstantEmitter::tryEmitAbstractForInitializer
llvm::Constant * tryEmitAbstractForInitializer(const VarDecl &D)
Try to emit the initializer of the given declaration as an abstract constant.
Definition: CGExprConstant.cpp:1192
clang::CodeGen::CodeGenTypeCache::Int64Ty
llvm::IntegerType * Int64Ty
Definition: CodeGenTypeCache.h:38
clang::CompoundLiteralExpr::isFileScope
bool isFileScope() const
Definition: Expr.h:2955
clang::RecordDecl::fields
field_range fields() const
Definition: Decl.h:3784
clang::FieldDecl
Represents a member of a struct/union/class.
Definition: Decl.h:2579
clang::ObjCStringLiteral::getString
StringLiteral * getString()
Definition: ExprObjC.h:63
clang::CodeGen::CGRecordLayout::getNonVirtualBaseLLVMFieldNo
unsigned getNonVirtualBaseLLVMFieldNo(const CXXRecordDecl *RD) const
Definition: CGRecordLayout.h:191
clang::Type::isReferenceType
bool isReferenceType() const
Definition: Type.h:6308
clang::APValue::getArraySize
unsigned getArraySize() const
Definition: APValue.h:348
clang::CharUnits::Zero
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition: CharUnits.h:53
clang::CastExpr::getSubExpr
Expr * getSubExpr()
Definition: Expr.h:3050
clang::CodeGen::state
i32 captured_struct **param SharedsTy A type which contains references the shared variables *param Shareds Context with the list of shared variables from the p *TaskFunction *param Data Additional data for task generation like final * state
Definition: CGOpenMPRuntime.h:748
clang::APValue::isFloat
bool isFloat() const
Definition: APValue.h:235
GCCTypeClass::Complex
llvm::SmallVectorImpl
Definition: LLVM.h:37
clang::CodeGen::CodeGenModule::GetConstantArrayFromStringLiteral
llvm::Constant * GetConstantArrayFromStringLiteral(const StringLiteral *E)
Return a constant array for the given string.
Definition: CodeGenModule.cpp:4431
clang::InitListExpr
Describes an C or C++ initializer list.
Definition: Expr.h:4185
clang::CXXTypeidExpr
A C++ typeid expression (C++ [expr.typeid]), which gets the type_info that corresponds to the supplie...
Definition: ExprCXX.h:669
clang::FieldDecl::isBitField
bool isBitField() const
Determines whether this field is a bitfield.
Definition: Decl.h:2657
APValue.h
clang::CharUnits
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
clang::Expr::EvalResult::Val
APValue Val
Val - This is the value the expression can be folded to.
Definition: Expr.h:573
clang::PredefinedExpr::PrettyFunction
Definition: Expr.h:1776
clang::CodeGen::ConstantEmitter::tryEmitPrivate
llvm::Constant * tryEmitPrivate(const Expr *E, QualType T)
Definition: CGExprConstant.cpp:1547
clang::CodeGen::ConstantEmitter::~ConstantEmitter
~ConstantEmitter()
Definition: CGExprConstant.cpp:1431
clang::CodeGen::CGRecordLayout::getBaseSubobjectLLVMType
llvm::StructType * getBaseSubobjectLLVMType() const
Return the "base subobject" LLVM type associated with this record.
Definition: CGRecordLayout.h:167
clang::Expr::IgnoreParenCasts
Expr * IgnoreParenCasts() LLVM_READONLY
IgnoreParenCasts - Ignore parentheses and casts.
Definition: Expr.cpp:2595
clang::ObjCStringLiteral
ObjCStringLiteral, used for Objective-C string literals i.e.
Definition: ExprObjC.h:51
clang::RecordDecl::field_begin
field_iterator field_begin() const
Definition: Decl.cpp:4145
clang::FieldDecl::getBitWidthValue
unsigned getBitWidthValue(const ASTContext &Ctx) const
Definition: Decl.cpp:3787
clang::APValue::isInt
bool isInt() const
Definition: APValue.h:234
clang::CastExpr
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Definition: Expr.h:2998
clang::CodeGen::ConstantEmitter
Definition: ConstantEmitter.h:24
llvm::ArrayRef
Definition: LLVM.h:32
clang::APValue::getComplexIntReal
APSInt & getComplexIntReal()
Definition: APValue.h:268
clang::CodeGen::ConstantEmitter::getCurrentAddrPrivate
llvm::GlobalValue * getCurrentAddrPrivate()
Get the address of the current location.
Definition: CGExprConstant.cpp:1260
clang::CXXDefaultArgExpr
A default argument (C++ [dcl.fct.default]).
Definition: ExprCXX.h:1073
clang::CodeGen::TargetCodeGenInfo::performAddrSpaceCast
virtual llvm::Value * performAddrSpaceCast(CodeGen::CodeGenFunction &CGF, llvm::Value *V, LangAS SrcAddr, LangAS DestAddr, llvm::Type *DestTy, bool IsNonNull=false) const
Perform address space cast of an expression of pointer type.
Definition: TargetInfo.cpp:447
clang::CodeGen::CodeGenModule::isTypeConstant
bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor)
isTypeConstant - Determine whether an object of this type can be emitted as a constant.
Definition: CodeGenModule.cpp:2999
clang::CXXDefaultInitExpr::getExpr
const Expr * getExpr() const
Get the initialization expression that will be used.
Definition: ExprCXX.h:1162
clang::APValue::getVectorElt
APValue & getVectorElt(unsigned I)
Definition: APValue.h:312
clang::Type::getAsRecordDecl
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition: Type.cpp:1617
clang::CharUnits::One
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition: CharUnits.h:58
clang::CodeGen::ConstantAddress::getElementBitCast
ConstantAddress getElementBitCast(llvm::Type *ty) const
Definition: Address.h:93
clang::Expr::EvaluateAsLValue
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 ...
Definition: ExprConstant.cpp:10971
clang::CodeGen::CodeGenModule::getTargetCodeGenInfo
const TargetCodeGenInfo & getTargetCodeGenInfo()
Definition: TargetInfo.cpp:9246
clang::CXXConstructExpr::getConstructor
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
Definition: ExprCXX.h:1334
clang::APValue::Int
Definition: APValue.h:44
clang::APValue::getArrayFiller
APValue & getArrayFiller()
Definition: APValue.h:336
clang::APValue::LValue
Definition: APValue.h:48
clang::CodeGen::CodeGenModule::getNullPointer
llvm::Constant * getNullPointer(llvm::PointerType *T, QualType QT)
Get target specific null pointer.
Definition: CGExprConstant.cpp:1567
clang::DesignatedInitUpdateExpr::getUpdater
InitListExpr * getUpdater() const
Definition: Expr.h:4771
clang::CharUnits::getQuantity
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:179
clang::ConstantExpr
ConstantExpr - An expression that occurs in a constant context.
Definition: Expr.h:904
clang::APValue::Uninitialized
Definition: APValue.h:43
Offset
unsigned Offset
Definition: Format.cpp:1631
clang::ASTRecordLayout
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
Definition: RecordLayout.h:39
clang::APValue::hasArrayFiller
bool hasArrayFiller() const
Definition: APValue.h:333
clang::CodeGen::CodeGenModule::getObjCRuntime
CGObjCRuntime & getObjCRuntime()
Return a reference to the configured Objective-C runtime.
Definition: CodeGenModule.h:556
clang::APValue::LValueBase::dyn_cast
T dyn_cast() const
Definition: APValue.h:74
clang::ValueDecl
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:637
clang::Expr
This represents one expression.
Definition: Expr.h:106
clang::CXXConstructorDecl::isCopyOrMoveConstructor
bool isCopyOrMoveConstructor(unsigned &TypeQuals) const
Determine whether this is a copy or move constructor.
Definition: DeclCXX.cpp:2376
CodeGenModule.h
clang::VarDecl::hasLocalStorage
bool hasLocalStorage() const
Returns true if a variable with function scope is a non-static local variable.
Definition: Decl.h:1036
clang::CodeGen::CodeGenModule::GetAddrOfGlobalVar
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.
Definition: CodeGenModule.cpp:3284
clang::Type::castAs
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:6811
clang::APValue::Struct
Definition: APValue.h:51
clang::DesignatedInitUpdateExpr
Definition: Expr.h:4749
Inits
Inits[]
Definition: OpenMPClause.h:151
clang::BlockExpr
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:5177
clang::ast_matchers::decl
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
Definition: ASTMatchersInternal.cpp:561
clang::InitListExpr::getNumInits
unsigned getNumInits() const
Definition: Expr.h:4215
CGCXXABI.h
clang::RecordDecl::field_end
field_iterator field_end() const
Definition: Decl.h:3787
CGObjCRuntime.h
clang::ASTContext::getTypeAlignInChars
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
Definition: ASTContext.cpp:2176
clang::ASTRecordLayout::getPrimaryBase
const CXXRecordDecl * getPrimaryBase() const
getPrimaryBase - Get the primary base for this record.
Definition: RecordLayout.h:218
clang::CharUnits::fromQuantity
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition: CharUnits.h:63
clang::CodeGen::CodeGenTypeCache::Int32Ty
llvm::IntegerType * Int32Ty
Definition: CodeGenTypeCache.h:38
clang::Expr::getType
QualType getType() const
Definition: Expr.h:128
clang::CodeGen::CodeGenModule::getLLVMLinkageVarDefinition
llvm::GlobalValue::LinkageTypes getLLVMLinkageVarDefinition(const VarDecl *VD, bool IsConstant)
Returns LLVM linkage for a declarator.
Definition: CodeGenModule.cpp:3849
clang::BaseSubobject
Definition: BaseSubobject.h:31
StmtVisitor.h
clang::APValue::getStructField
APValue & getStructField(unsigned i)
Definition: APValue.h:365
clang::MaterializeTemporaryExpr::getStorageDuration
StorageDuration getStorageDuration() const
Retrieve the storage duration for the materialized temporary.
Definition: ExprCXX.h:4200
clang::ObjCEncodeExpr::getEncodedType
QualType getEncodedType() const
Definition: ExprObjC.h:407
clang::CodeGen::CodeGenModule::GetAddrOfUuidDescriptor
ConstantAddress GetAddrOfUuidDescriptor(const CXXUuidofExpr *E)
Get the address of a uuid descriptor .
Definition: CodeGenModule.cpp:2064
clang::UnaryOperator
UnaryOperator - This represents the unary-expression&#39;s (except sizeof and alignof), the postinc/postdec operators from postfix-expression, and various extensions.
Definition: Expr.h:1896
clang::CXXTypeidExpr::getTypeOperand
QualType getTypeOperand(ASTContext &Context) const
Retrieves the type operand of this typeid() expression after various required adjustments (removing r...
Definition: ExprCXX.cpp:77
clang::SubstNonTypeTemplateParmExpr
Represents a reference to a non-type template parameter that has been substituted with a template arg...
Definition: ExprCXX.h:3958
clang::VarDecl::evaluateValue
APValue * evaluateValue() const
Attempt to evaluate the value of the initializer attached to this declaration, and produce notes expl...
Definition: Decl.cpp:2258
clang::APValue::getComplexIntImag
APSInt & getComplexIntImag()
Definition: APValue.h:276
clang::FunctionDecl::isTrivial
bool isTrivial() const
Whether this function is "trivial" in some specialized C++ senses.
Definition: Decl.h:2026
clang::ParenExpr::getSubExpr
const Expr * getSubExpr() const
Definition: Expr.h:1860
clang::Type::isUnionType
bool isUnionType() const
Definition: Type.cpp:475
clang::CodeGen::CodeGenModule::getContext
ASTContext & getContext() const
Definition: CodeGenModule.h:652
CodeGenFunction.h
clang::APValue::getUnionField
const FieldDecl * getUnionField() const
Definition: APValue.h:376
clang::CodeGen::CodeGenModule::GetAddrOfGlobalTemporary
ConstantAddress GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E, const Expr *Inner)
Returns a pointer to a global variable representing a temporary with static or thread storage duratio...
Definition: CodeGenModule.cpp:4586
clang::CallExpr::getBuiltinCallee
unsigned getBuiltinCallee() const
getBuiltinCallee - If this is a call to a builtin, return the builtin ID of the callee.
Definition: Expr.cpp:1368
clang::CodeGen::AlignmentSource::Type
The l-value was considered opaque, so the alignment was determined from a type.
clang::RecordType::getDecl
RecordDecl * getDecl() const
Definition: Type.h:4380
clang::APValue::getStructBase
APValue & getStructBase(unsigned i)
Definition: APValue.h:361
clang::TargetInfo::useFP16ConversionIntrinsics
virtual bool useFP16ConversionIntrinsics() const
Check whether llvm intrinsics such as llvm.convert.to.fp16 should be used to convert to and from __fp...
Definition: TargetInfo.h:723
clang::ASTRecordLayout::getFieldOffset
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
Definition: RecordLayout.h:191
clang::APValue::Union
Definition: APValue.h:52
clang::CodeGen::CodeGenModule::getOrCreateStaticVarDecl
llvm::Constant * getOrCreateStaticVarDecl(const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage)
Definition: CGDecl.cpp:211
clang::Expr::EvaluateAsRValue
bool EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx, bool InConstantContext=false) const
EvaluateAsRValue - Return true if this is a constant which we can fold to an rvalue using any crazy t...
Definition: ExprConstant.cpp:10937
clang::DesignatedInitUpdateExpr::getBase
Expr * getBase() const
Definition: Expr.h:4768
clang::APValue::getArrayInitializedElt
APValue & getArrayInitializedElt(unsigned I)
Definition: APValue.h:325
clang::CodeGen::CGCXXABI::EmitMemberPointerConversion
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
false
#define false
Definition: stdbool.h:33
clang::CodeGen::CodeGenFunction::CurFn
llvm::Function * CurFn
Definition: CodeGenFunction.h:301
getNonMemoryType
static QualType getNonMemoryType(CodeGenModule &CGM, QualType type)
Definition: CGExprConstant.cpp:1437
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
TargetInfo.h
clang::APValue::getAddrLabelDiffRHS
const AddrLabelExpr * getAddrLabelDiffRHS() const
Definition: APValue.h:396
clang::QualType::getAddressSpace
LangAS getAddressSpace() const
Return the address space of this type.
Definition: Type.h:6188
clang::APValue::Array
Definition: APValue.h:50
clang::CodeGen::CGObjCRuntime::GenerateConstantString
virtual ConstantAddress GenerateConstantString(const StringLiteral *)=0
Generate a constant string object.
clang::UnaryOperator::getSubExpr
Expr * getSubExpr() const
Definition: Expr.h:1926
clang::CodeGen::CodeGenModule::GetAddrOfConstantCFString
ConstantAddress GetAddrOfConstantCFString(const StringLiteral *Literal)
Return a pointer to a constant CFString object for the given string.
Definition: CodeGenModule.cpp:4208
clang::CastExpr::getCastKind
CastKind getCastKind() const
Definition: Expr.h:3044
clang::APValue::getUnionValue
APValue & getUnionValue()
Definition: APValue.h:380
clang::FieldDecl::isUnnamedBitfield
bool isUnnamedBitfield() const
Determines whether this is an unnamed bitfield.
Definition: Decl.h:2660
clang::APValue::getFloat
APFloat & getFloat()
Definition: APValue.h:260
clang::ASTContext::getBaseElementType
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
Definition: ASTContext.cpp:5519
clang::CXXMethodDecl
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2041
clang::ASTContext::getAsConstantArrayType
const ConstantArrayType * getAsConstantArrayType(QualType T) const
Definition: ASTContext.h:2413
clang::CodeGen::CodeGenModule::GetAddrOfConstantStringFromLiteral
ConstantAddress GetAddrOfConstantStringFromLiteral(const StringLiteral *S, StringRef Name=".str")
Return a pointer to a constant array for the given string literal.
Definition: CodeGenModule.cpp:4496
clang::StmtVisitor
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:183
clang::CodeGen::CodeGenModule::EmitNullConstantForBase
llvm::Constant * EmitNullConstantForBase(const CXXRecordDecl *Record)
Return a null constant appropriate for zero-initializing a base class with the given type...
Definition: CGExprConstant.cpp:2174
clang::CodeGen::CodeGenModule::GetAddrOfRTTIDescriptor
llvm::Constant * GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH=false)
Get the address of the RTTI descriptor for the given type.
Definition: CodeGenModule.cpp:5309
clang::ASTContext::getAsArrayType
const ArrayType * getAsArrayType(QualType T) const
Type Query functions.
Definition: ASTContext.cpp:5409
clang::CodeGen::ConstantEmitter::emitAbstract
llvm::Constant * emitAbstract(const Expr *E, QualType T)
Emit the result of the given expression as an abstract constant, asserting that it succeeded...
Definition: CGExprConstant.cpp:1213
clang::APValue::Float
Definition: APValue.h:45
llvm::SmallVector
Definition: LLVM.h:36
clang::CodeGen::CodeGenModule::GetWeakRefReference
ConstantAddress GetWeakRefReference(const ValueDecl *VD)
Get a reference to the target of VD.
Definition: CodeGenModule.cpp:2091
clang::CodeGen::ConstantEmitter::registerCurrentAddrPrivate
void registerCurrentAddrPrivate(llvm::Constant *signal, llvm::GlobalValue *placeholder)
Register a &#39;signal&#39; value with the emitter to inform it where to resolve a placeholder.
Definition: CGExprConstant.cpp:1280
clang::APValue::AddrLabelDiff
Definition: APValue.h:54
clang::CodeGen::CodeGenModule::getAddrOfConstantCompoundLiteralIfEmitted
llvm::GlobalVariable * getAddrOfConstantCompoundLiteralIfEmitted(const CompoundLiteralExpr *E)
If it&#39;s been emitted already, returns the GlobalVariable corresponding to a compound literal...
Definition: CGExprConstant.cpp:2002
clang::operator<
bool operator<(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
Definition: DeclarationName.h:541
clang::AtomicType
Definition: Type.h:5968
clang::CodeGen::CodeGenModule::Error
void Error(SourceLocation loc, StringRef error)
Emit a general error that something can&#39;t be done.
Definition: CodeGenModule.cpp:695
clang::CodeGen::ConstantEmitter::emitNullForMemory
llvm::Constant * emitNullForMemory(QualType T)
Definition: ConstantEmitter.h:114
clang::InitListExpr::getArrayFiller
Expr * getArrayFiller()
If this initializer list initializes an array with more elements than there are initializers in the l...
Definition: Expr.h:4279
clang::RecordDecl::hasFlexibleArrayMember
bool hasFlexibleArrayMember() const
Definition: Decl.h:3647
clang::ASTContext::hasSameUnqualifiedType
bool hasSameUnqualifiedType(QualType T1, QualType T2) const
Determine whether the given types are equivalent after cvr-qualifiers have been removed.
Definition: ASTContext.h:2293
clang::Expr::getExprLoc
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:215
clang::CompoundLiteralExpr::getInitializer
const Expr * getInitializer() const
Definition: Expr.h:2951
clang::CodeGen::CodeGenModule::GetAddrOfConstantCString
ConstantAddress GetAddrOfConstantCString(const std::string &Str, const char *GlobalName=nullptr)
Returns a pointer to a character array containing the literal and a terminating &#39;\0&#39; character...
Definition: CodeGenModule.cpp:4553
clang::GenericSelectionExpr
Represents a C11 generic selection.
Definition: Expr.h:5010
clang::AddrLabelExpr
AddrLabelExpr - The GNU address of label extension, representing &&label.
Definition: Expr.h:3757
clang::CastExpr::getTargetUnionField
const FieldDecl * getTargetUnionField() const
Definition: Expr.h:3075
clang::CodeGen::CodeGenModule
This class organizes the cross-function state that is used while generating LLVM code.
Definition: CodeGenModule.h:274
clang::CodeGen::ConstantEmitter::CGF
CodeGenFunction * CGF
Definition: ConstantEmitter.h:27
clang::CXXTypeidExpr::isTypeOperand
bool isTypeOperand() const
Definition: ExprCXX.h:709
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::ASTRecordLayout::getSize
CharUnits getSize() const
getSize - Get the record size in characters.
Definition: RecordLayout.h:184
clang::PredefinedExpr
[C99 6.4.2.2] - A predefined identifier such as func.
Definition: Expr.h:1758
clang::Expr::EvalResult
EvalResult is a struct with detailed info about an evaluated expression.
Definition: Expr.h:571
type
clang::VarDecl::getInit
const Expr * getInit() const
Definition: Decl.h:1220
EmitNullConstantForBase
static llvm::Constant * EmitNullConstantForBase(CodeGenModule &CGM, llvm::Type *baseType, const CXXRecordDecl *base)
Emit the null constant for a base subobject.
Definition: CGExprConstant.cpp:2125
clang::CodeGen::ConstantAddress::getPointer
llvm::Constant * getPointer() const
Definition: Address.h:84
clang::CodeGen::CodeGenTypeCache::IntPtrTy
llvm::IntegerType * IntPtrTy
Definition: CodeGenTypeCache.h:47
clang::CXXDefaultArgExpr::getExpr
const Expr * getExpr() const
Definition: ExprCXX.h:1106
clang::APValue::getArrayInitializedElts
unsigned getArrayInitializedElts() const
Definition: APValue.h:344
clang::APValue::ComplexFloat
Definition: APValue.h:47
clang::CodeGen::CodeGenModule::EmitNullConstant
llvm::Constant * EmitNullConstant(QualType T)
Return the result of value-initializing the given type, i.e.
Definition: CGExprConstant.cpp:2143
clang::MemberPointerType
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition: Type.h:2756
clang::CodeGen::CodeGenModule::getModule
llvm::Module & getModule() const
Definition: CodeGenModule.h:659
clang::DeclContext::specific_decl_iterator
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:2017
clang::AddrLabelExpr::getLabel
LabelDecl * getLabel() const
Definition: Expr.h:3779
clang::CodeGen::ConstantEmitter::tryEmitPrivateForMemory
llvm::Constant * tryEmitPrivateForMemory(const Expr *E, QualType T)
Definition: CGExprConstant.cpp:1502
clang::CodeGen::CodeGenModule::EmitExplicitCastExprType
void EmitExplicitCastExprType(const ExplicitCastExpr *E, CodeGenFunction *CGF=nullptr)
Emit type info if type of an expression is a variably modified type.
Definition: CGExpr.cpp:1013
clang::Expr::EvalStatus::HasSideEffects
bool HasSideEffects
Whether the evaluated expression has side effects.
Definition: Expr.h:544
clang::RecordType
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:4370
clang::StmtVisitorBase
StmtVisitorBase - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:36
clang::ObjCEncodeExpr
ObjCEncodeExpr, used for @encode in Objective-C.
Definition: ExprObjC.h:386
clang::ConstantArrayType::getSize
const llvm::APInt & getSize() const
Definition: Type.h:2890
clang::ASTContext::getCharWidth
uint64_t getCharWidth() const
Return the size of the character type, in bits.
Definition: ASTContext.h:2074
clang::ASTContext::getObjCEncodingForType
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.
Definition: ASTContext.cpp:6476
clang::APValue::Vector
Definition: APValue.h:49
clang::SubstNonTypeTemplateParmExpr::getReplacement
Expr * getReplacement() const
Definition: ExprCXX.h:3990
clang::CXXConstructExpr::getArg
Expr * getArg(unsigned Arg)
Return the specified argument.
Definition: ExprCXX.h:1413
clang::CodeGen::CGRecordLayout::getLLVMType
llvm::StructType * getLLVMType() const
Return the "complete object" LLVM type associated with this record.
Definition: CGRecordLayout.h:161
clang::CodeGen::CodeGenModule::GetAddrOfFunction
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.
Definition: CodeGenModule.cpp:2888
clang::InternalLinkage
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
Definition: Linkage.h:32
clang::CodeGen::CodeGenModule::GetAddrOfConstantCompoundLiteral
ConstantAddress GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *E)
Returns a pointer to a constant global variable for the given file-scope compound literal expression...
Definition: CGExprConstant.cpp:2015
clang::APValue
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat]...
Definition: APValue.h:38
clang::CXXBaseSpecifier
Represents a base class of a C++ class.
Definition: DeclCXX.h:192
clang::ASTContext::getTypeSize
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
Definition: ASTContext.h:2070
clang::CXXDefaultInitExpr
A use of a default initializer in a constructor or in aggregate initialization.
Definition: ExprCXX.h:1137
clang::CodeGen::CodeGenModule::setAddrOfConstantCompoundLiteral
void setAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *CLE, llvm::GlobalVariable *GV)
Notes that CLE&#39;s GlobalVariable is GV.
Definition: CGExprConstant.cpp:2007
EmitNullConstant
static llvm::Constant * EmitNullConstant(CodeGenModule &CGM, const RecordDecl *record, bool asCompleteObject)
Definition: CGExprConstant.cpp:2040
clang::ASTContext::toCharUnitsFromBits
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
Definition: ASTContext.cpp:2156
clang::ASTContext::toBits
int64_t toBits(CharUnits CharSize) const
Convert a size in characters to a size in bits.
Definition: ASTContext.cpp:2161
clang::APValue::getKind
ValueKind getKind() const
Definition: APValue.h:232
clang::APValue::getComplexFloatImag
APFloat & getComplexFloatImag()
Definition: APValue.h:292
clang::CXXRecordDecl
Represents a C++ struct/union/class.
Definition: DeclCXX.h:300
clang::ChooseExpr
ChooseExpr - GNU builtin-in function __builtin_choose_expr.
Definition: Expr.h:3977
RecordLayout.h
clang::QualType::getQualifiers
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition: Type.h:6099
clang::CodeGen::ConstantAddress
A specialization of Address that requires the address to be an LLVM Constant.
Definition: Address.h:75
clang::ASTRecordLayout::getNonVirtualSize
CharUnits getNonVirtualSize() const
getNonVirtualSize - Get the non-virtual size (in chars) of an object, which is the size of the object...
Definition: RecordLayout.h:203
clang::diag::kind
unsigned kind
All of the diagnostics that can be emitted by the frontend.
Definition: DiagnosticIDs.h:61
clang::CodeGen::CGRecordLayout::getVirtualBaseIndex
unsigned getVirtualBaseIndex(const CXXRecordDecl *base) const
Return the LLVM field index corresponding to the given virtual base.
Definition: CGRecordLayout.h:198
clang::CodeGen::CodeGenFunction::EmitPredefinedLValue
LValue EmitPredefinedLValue(const PredefinedExpr *E)
Definition: CGExpr.cpp:2674
clang::StringLiteral
StringLiteral - This represents a string literal expression, e.g.
Definition: Expr.h:1566
clang::CallExpr
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2396
clang::CodeGen::ConstantEmitter::finalize
void finalize(llvm::GlobalVariable *global)
Definition: CGExprConstant.cpp:1415
clang::APValue::MemberPointer
Definition: APValue.h:53
clang::CodeGen::ConstantEmitter::tryEmitAbstract
llvm::Constant * tryEmitAbstract(const Expr *E, QualType T)
Try to emit the result of the given expression as an abstract constant.
Definition: CGExprConstant.cpp:1199
clang::CodeGen::ConstantAddress::invalid
static ConstantAddress invalid()
Definition: Address.h:80
clang::CodeGen::CodeGenModule::getCXXABI
CGCXXABI & getCXXABI() const
Definition: CodeGenModule.h:669
max
__DEVICE__ int max(int __a, int __b)
Definition: __clang_cuda_device_functions.h:1611
clang::TagDecl::isUnion
bool isUnion() const
Definition: Decl.h:3252
clang::CodeGen::ConstantEmitter::tryEmitAbstractForMemory
llvm::Constant * tryEmitAbstractForMemory(const Expr *E, QualType T)
Definition: CGExprConstant.cpp:1488
min
__DEVICE__ int min(int __a, int __b)
Definition: __clang_cuda_device_functions.h:1618
clang::InitListExpr::getInitializedFieldInUnion
FieldDecl * getInitializedFieldInUnion()
If this initializes a union, specifies which field in the union to initialize.
Definition: Expr.h:4297
clang::CodeGen::CodeGenModule::GetAddrOfGlobalBlock
llvm::Constant * GetAddrOfGlobalBlock(const BlockExpr *BE, StringRef Name)
Gets the address of a block which requires no captures.
Definition: CGBlocks.cpp:1371
clang::ValueDecl::getType
QualType getType() const
Definition: Decl.h:648
clang::CXXConstructExpr::getNumArgs
unsigned getNumArgs() const
Return the number of arguments to the constructor call.
Definition: ExprCXX.h:1410
clang::CodeGen::LValue
LValue - This represents an lvalue references.
Definition: CGValue.h:167
clang::CodeGen::CodeGenFunction::GetAddrOfLabel
llvm::BlockAddress * GetAddrOfLabel(const LabelDecl *L)
Definition: CodeGenFunction.cpp:1857
clang::CXXUuidofExpr
A Microsoft C++ __uuidof expression, which gets the _GUID that corresponds to the supplied type or ex...
Definition: ExprCXX.h:887
clang::APValue::getInt
APSInt & getInt()
Definition: APValue.h:252
clang::ASTContext::getTargetAddressSpace
unsigned getTargetAddressSpace(QualType T) const
Definition: ASTContext.h:2499
clang::CodeGen::CodeGenTypes::getCGRecordLayout
const CGRecordLayout & getCGRecordLayout(const RecordDecl *)
getCGRecordLayout - Return record layout info for the given record decl.
Definition: CodeGenTypes.cpp:761
clang::ASTContext::getLangOpts
const LangOptions & getLangOpts() const
Definition: ASTContext.h:707
clang::ConstantArrayType
Represents the canonical version of C arrays with a specified constant size.
Definition: Type.h:2872
Builtins.h
Defines enum values for all the target-independent builtin functions.
clang::ImplicitValueInitExpr
Represents an implicitly-generated value initialization of an object of a given type.
Definition: Expr.h:4893
ConstantEmitter.h
clang::APValue::getLValueOffset
CharUnits & getLValueOffset()
Definition: APValue.cpp:613
clang::CodeGen::CGRecordLayout::getLLVMFieldNo
unsigned getLLVMFieldNo(const FieldDecl *FD) const
Return llvm::StructType element number that corresponds to the field FD.
Definition: CGRecordLayout.h:185
clang::APValue::getVectorLength
unsigned getVectorLength() const
Definition: APValue.h:320