clang 19.0.0git
CodeGenTypes.cpp
Go to the documentation of this file.
1//===--- CodeGenTypes.cpp - Type translation for LLVM CodeGen -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This is the code that handles AST -> LLVM type lowering.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CodeGenTypes.h"
14#include "CGCXXABI.h"
15#include "CGCall.h"
16#include "CGOpenCLRuntime.h"
17#include "CGRecordLayout.h"
18#include "TargetInfo.h"
20#include "clang/AST/DeclCXX.h"
21#include "clang/AST/DeclObjC.h"
22#include "clang/AST/Expr.h"
25#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/DerivedTypes.h"
27#include "llvm/IR/Module.h"
28
29using namespace clang;
30using namespace CodeGen;
31
33 : CGM(cgm), Context(cgm.getContext()), TheModule(cgm.getModule()),
34 Target(cgm.getTarget()), TheCXXABI(cgm.getCXXABI()),
35 TheABIInfo(cgm.getTargetCodeGenInfo().getABIInfo()) {
36 SkippedLayout = false;
37 LongDoubleReferenced = false;
38}
39
41 for (llvm::FoldingSet<CGFunctionInfo>::iterator
42 I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
43 delete &*I++;
44}
45
47 return CGM.getCodeGenOpts();
48}
49
51 llvm::StructType *Ty,
52 StringRef suffix) {
54 llvm::raw_svector_ostream OS(TypeName);
55 OS << RD->getKindName() << '.';
56
57 // FIXME: We probably want to make more tweaks to the printing policy. For
58 // example, we should probably enable PrintCanonicalTypes and
59 // FullyQualifiedNames.
61 Policy.SuppressInlineNamespace = false;
62
63 // Name the codegen type after the typedef name
64 // if there is no tag type name available
65 if (RD->getIdentifier()) {
66 // FIXME: We should not have to check for a null decl context here.
67 // Right now we do it because the implicit Obj-C decls don't have one.
68 if (RD->getDeclContext())
69 RD->printQualifiedName(OS, Policy);
70 else
71 RD->printName(OS, Policy);
72 } else if (const TypedefNameDecl *TDD = RD->getTypedefNameForAnonDecl()) {
73 // FIXME: We should not have to check for a null decl context here.
74 // Right now we do it because the implicit Obj-C decls don't have one.
75 if (TDD->getDeclContext())
76 TDD->printQualifiedName(OS, Policy);
77 else
78 TDD->printName(OS);
79 } else
80 OS << "anon";
81
82 if (!suffix.empty())
83 OS << suffix;
84
85 Ty->setName(OS.str());
86}
87
88/// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
89/// ConvertType in that it is used to convert to the memory representation for
90/// a type. For example, the scalar representation for _Bool is i1, but the
91/// memory representation is usually i8 or i32, depending on the target.
92llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T, bool ForBitField) {
93 if (T->isConstantMatrixType()) {
94 const Type *Ty = Context.getCanonicalType(T).getTypePtr();
95 const ConstantMatrixType *MT = cast<ConstantMatrixType>(Ty);
96 return llvm::ArrayType::get(ConvertType(MT->getElementType()),
97 MT->getNumRows() * MT->getNumColumns());
98 }
99
100 llvm::Type *R = ConvertType(T);
101
102 // Check for the boolean vector case.
103 if (T->isExtVectorBoolType()) {
104 auto *FixedVT = cast<llvm::FixedVectorType>(R);
105 // Pad to at least one byte.
106 uint64_t BytePadded = std::max<uint64_t>(FixedVT->getNumElements(), 8);
107 return llvm::IntegerType::get(FixedVT->getContext(), BytePadded);
108 }
109
110 // If this is a bool type, or a bit-precise integer type in a bitfield
111 // representation, map this integer to the target-specified size.
112 if ((ForBitField && T->isBitIntType()) ||
113 (!T->isBitIntType() && R->isIntegerTy(1)))
114 return llvm::IntegerType::get(getLLVMContext(),
115 (unsigned)Context.getTypeSize(T));
116
117 // Else, don't map it.
118 return R;
119}
120
121/// isRecordLayoutComplete - Return true if the specified type is already
122/// completely laid out.
124 llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I =
125 RecordDeclTypes.find(Ty);
126 return I != RecordDeclTypes.end() && !I->second->isOpaque();
127}
128
129/// isFuncParamTypeConvertible - Return true if the specified type in a
130/// function parameter or result position can be converted to an IR type at this
131/// point. This boils down to being whether it is complete.
133 // Some ABIs cannot have their member pointers represented in IR unless
134 // certain circumstances have been reached.
135 if (const auto *MPT = Ty->getAs<MemberPointerType>())
137
138 // If this isn't a tagged type, we can convert it!
139 const TagType *TT = Ty->getAs<TagType>();
140 if (!TT) return true;
141
142 // Incomplete types cannot be converted.
143 return !TT->isIncompleteType();
144}
145
146
147/// Code to verify a given function type is complete, i.e. the return type
148/// and all of the parameter types are complete. Also check to see if we are in
149/// a RS_StructPointer context, and if so whether any struct types have been
150/// pended. If so, we don't want to ask the ABI lowering code to handle a type
151/// that cannot be converted to an IR type.
154 return false;
155
156 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
157 for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++)
158 if (!isFuncParamTypeConvertible(FPT->getParamType(i)))
159 return false;
160
161 return true;
162}
163
164/// UpdateCompletedType - When we find the full definition for a TagDecl,
165/// replace the 'opaque' type we previously made for it if applicable.
167 // If this is an enum being completed, then we flush all non-struct types from
168 // the cache. This allows function types and other things that may be derived
169 // from the enum to be recomputed.
170 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) {
171 // Only flush the cache if we've actually already converted this type.
172 if (TypeCache.count(ED->getTypeForDecl())) {
173 // Okay, we formed some types based on this. We speculated that the enum
174 // would be lowered to i32, so we only need to flush the cache if this
175 // didn't happen.
176 if (!ConvertType(ED->getIntegerType())->isIntegerTy(32))
177 TypeCache.clear();
178 }
179 // If necessary, provide the full definition of a type only used with a
180 // declaration so far.
181 if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
182 DI->completeType(ED);
183 return;
184 }
185
186 // If we completed a RecordDecl that we previously used and converted to an
187 // anonymous type, then go ahead and complete it now.
188 const RecordDecl *RD = cast<RecordDecl>(TD);
189 if (RD->isDependentType()) return;
190
191 // Only complete it if we converted it already. If we haven't converted it
192 // yet, we'll just do it lazily.
193 if (RecordDeclTypes.count(Context.getTagDeclType(RD).getTypePtr()))
195
196 // If necessary, provide the full definition of a type only used with a
197 // declaration so far.
198 if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
199 DI->completeType(RD);
200}
201
203 QualType T = Context.getRecordType(RD);
204 T = Context.getCanonicalType(T);
205
206 const Type *Ty = T.getTypePtr();
207 if (RecordsWithOpaqueMemberPointers.count(Ty)) {
208 TypeCache.clear();
209 RecordsWithOpaqueMemberPointers.clear();
210 }
211}
212
213static llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext,
214 const llvm::fltSemantics &format,
215 bool UseNativeHalf = false) {
216 if (&format == &llvm::APFloat::IEEEhalf()) {
217 if (UseNativeHalf)
218 return llvm::Type::getHalfTy(VMContext);
219 else
220 return llvm::Type::getInt16Ty(VMContext);
221 }
222 if (&format == &llvm::APFloat::BFloat())
223 return llvm::Type::getBFloatTy(VMContext);
224 if (&format == &llvm::APFloat::IEEEsingle())
225 return llvm::Type::getFloatTy(VMContext);
226 if (&format == &llvm::APFloat::IEEEdouble())
227 return llvm::Type::getDoubleTy(VMContext);
228 if (&format == &llvm::APFloat::IEEEquad())
229 return llvm::Type::getFP128Ty(VMContext);
230 if (&format == &llvm::APFloat::PPCDoubleDouble())
231 return llvm::Type::getPPC_FP128Ty(VMContext);
232 if (&format == &llvm::APFloat::x87DoubleExtended())
233 return llvm::Type::getX86_FP80Ty(VMContext);
234 llvm_unreachable("Unknown float format!");
235}
236
237llvm::Type *CodeGenTypes::ConvertFunctionTypeInternal(QualType QFT) {
238 assert(QFT.isCanonical());
239 const FunctionType *FT = cast<FunctionType>(QFT.getTypePtr());
240 // First, check whether we can build the full function type. If the
241 // function type depends on an incomplete type (e.g. a struct or enum), we
242 // cannot lower the function type.
243 if (!isFuncTypeConvertible(FT)) {
244 // This function's type depends on an incomplete tag type.
245
246 // Force conversion of all the relevant record types, to make sure
247 // we re-convert the FunctionType when appropriate.
248 if (const RecordType *RT = FT->getReturnType()->getAs<RecordType>())
249 ConvertRecordDeclType(RT->getDecl());
250 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
251 for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++)
252 if (const RecordType *RT = FPT->getParamType(i)->getAs<RecordType>())
253 ConvertRecordDeclType(RT->getDecl());
254
255 SkippedLayout = true;
256
257 // Return a placeholder type.
258 return llvm::StructType::get(getLLVMContext());
259 }
260
261 // The function type can be built; call the appropriate routines to
262 // build it.
263 const CGFunctionInfo *FI;
264 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
267 } else {
268 const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT);
271 }
272
273 llvm::Type *ResultType = nullptr;
274 // If there is something higher level prodding our CGFunctionInfo, then
275 // don't recurse into it again.
276 if (FunctionsBeingProcessed.count(FI)) {
277
278 ResultType = llvm::StructType::get(getLLVMContext());
279 SkippedLayout = true;
280 } else {
281
282 // Otherwise, we're good to go, go ahead and convert it.
283 ResultType = GetFunctionType(*FI);
284 }
285
286 return ResultType;
287}
288
289/// ConvertType - Convert the specified type to its LLVM form.
291 T = Context.getCanonicalType(T);
292
293 const Type *Ty = T.getTypePtr();
294
295 // For the device-side compilation, CUDA device builtin surface/texture types
296 // may be represented in different types.
297 if (Context.getLangOpts().CUDAIsDevice) {
299 if (auto *Ty = CGM.getTargetCodeGenInfo()
301 return Ty;
302 } else if (T->isCUDADeviceBuiltinTextureType()) {
303 if (auto *Ty = CGM.getTargetCodeGenInfo()
305 return Ty;
306 }
307 }
308
309 // RecordTypes are cached and processed specially.
310 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
311 return ConvertRecordDeclType(RT->getDecl());
312
313 llvm::Type *CachedType = nullptr;
314 auto TCI = TypeCache.find(Ty);
315 if (TCI != TypeCache.end())
316 CachedType = TCI->second;
317 // With expensive checks, check that the type we compute matches the
318 // cached type.
319#ifndef EXPENSIVE_CHECKS
320 if (CachedType)
321 return CachedType;
322#endif
323
324 // If we don't have it in the cache, convert it now.
325 llvm::Type *ResultType = nullptr;
326 switch (Ty->getTypeClass()) {
327 case Type::Record: // Handled above.
328#define TYPE(Class, Base)
329#define ABSTRACT_TYPE(Class, Base)
330#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
331#define DEPENDENT_TYPE(Class, Base) case Type::Class:
332#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
333#include "clang/AST/TypeNodes.inc"
334 llvm_unreachable("Non-canonical or dependent types aren't possible.");
335
336 case Type::Builtin: {
337 switch (cast<BuiltinType>(Ty)->getKind()) {
338 case BuiltinType::Void:
339 case BuiltinType::ObjCId:
340 case BuiltinType::ObjCClass:
341 case BuiltinType::ObjCSel:
342 // LLVM void type can only be used as the result of a function call. Just
343 // map to the same as char.
344 ResultType = llvm::Type::getInt8Ty(getLLVMContext());
345 break;
346
347 case BuiltinType::Bool:
348 // Note that we always return bool as i1 for use as a scalar type.
349 ResultType = llvm::Type::getInt1Ty(getLLVMContext());
350 break;
351
352 case BuiltinType::Char_S:
353 case BuiltinType::Char_U:
354 case BuiltinType::SChar:
355 case BuiltinType::UChar:
356 case BuiltinType::Short:
357 case BuiltinType::UShort:
358 case BuiltinType::Int:
359 case BuiltinType::UInt:
360 case BuiltinType::Long:
361 case BuiltinType::ULong:
362 case BuiltinType::LongLong:
363 case BuiltinType::ULongLong:
364 case BuiltinType::WChar_S:
365 case BuiltinType::WChar_U:
366 case BuiltinType::Char8:
367 case BuiltinType::Char16:
368 case BuiltinType::Char32:
369 case BuiltinType::ShortAccum:
370 case BuiltinType::Accum:
371 case BuiltinType::LongAccum:
372 case BuiltinType::UShortAccum:
373 case BuiltinType::UAccum:
374 case BuiltinType::ULongAccum:
375 case BuiltinType::ShortFract:
376 case BuiltinType::Fract:
377 case BuiltinType::LongFract:
378 case BuiltinType::UShortFract:
379 case BuiltinType::UFract:
380 case BuiltinType::ULongFract:
381 case BuiltinType::SatShortAccum:
382 case BuiltinType::SatAccum:
383 case BuiltinType::SatLongAccum:
384 case BuiltinType::SatUShortAccum:
385 case BuiltinType::SatUAccum:
386 case BuiltinType::SatULongAccum:
387 case BuiltinType::SatShortFract:
388 case BuiltinType::SatFract:
389 case BuiltinType::SatLongFract:
390 case BuiltinType::SatUShortFract:
391 case BuiltinType::SatUFract:
392 case BuiltinType::SatULongFract:
393 ResultType = llvm::IntegerType::get(getLLVMContext(),
394 static_cast<unsigned>(Context.getTypeSize(T)));
395 break;
396
397 case BuiltinType::Float16:
398 ResultType =
400 /* UseNativeHalf = */ true);
401 break;
402
403 case BuiltinType::Half:
404 // Half FP can either be storage-only (lowered to i16) or native.
405 ResultType = getTypeForFormat(
407 Context.getLangOpts().NativeHalfType ||
409 break;
410 case BuiltinType::LongDouble:
411 LongDoubleReferenced = true;
412 LLVM_FALLTHROUGH;
413 case BuiltinType::BFloat16:
414 case BuiltinType::Float:
415 case BuiltinType::Double:
416 case BuiltinType::Float128:
417 case BuiltinType::Ibm128:
418 ResultType = getTypeForFormat(getLLVMContext(),
419 Context.getFloatTypeSemantics(T),
420 /* UseNativeHalf = */ false);
421 break;
422
423 case BuiltinType::NullPtr:
424 // Model std::nullptr_t as i8*
425 ResultType = llvm::PointerType::getUnqual(getLLVMContext());
426 break;
427
428 case BuiltinType::UInt128:
429 case BuiltinType::Int128:
430 ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
431 break;
432
433#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
434 case BuiltinType::Id:
435#include "clang/Basic/OpenCLImageTypes.def"
436#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
437 case BuiltinType::Id:
438#include "clang/Basic/OpenCLExtensionTypes.def"
439 case BuiltinType::OCLSampler:
440 case BuiltinType::OCLEvent:
441 case BuiltinType::OCLClkEvent:
442 case BuiltinType::OCLQueue:
443 case BuiltinType::OCLReserveID:
444 ResultType = CGM.getOpenCLRuntime().convertOpenCLSpecificType(Ty);
445 break;
446 case BuiltinType::SveInt8:
447 case BuiltinType::SveUint8:
448 case BuiltinType::SveInt8x2:
449 case BuiltinType::SveUint8x2:
450 case BuiltinType::SveInt8x3:
451 case BuiltinType::SveUint8x3:
452 case BuiltinType::SveInt8x4:
453 case BuiltinType::SveUint8x4:
454 case BuiltinType::SveInt16:
455 case BuiltinType::SveUint16:
456 case BuiltinType::SveInt16x2:
457 case BuiltinType::SveUint16x2:
458 case BuiltinType::SveInt16x3:
459 case BuiltinType::SveUint16x3:
460 case BuiltinType::SveInt16x4:
461 case BuiltinType::SveUint16x4:
462 case BuiltinType::SveInt32:
463 case BuiltinType::SveUint32:
464 case BuiltinType::SveInt32x2:
465 case BuiltinType::SveUint32x2:
466 case BuiltinType::SveInt32x3:
467 case BuiltinType::SveUint32x3:
468 case BuiltinType::SveInt32x4:
469 case BuiltinType::SveUint32x4:
470 case BuiltinType::SveInt64:
471 case BuiltinType::SveUint64:
472 case BuiltinType::SveInt64x2:
473 case BuiltinType::SveUint64x2:
474 case BuiltinType::SveInt64x3:
475 case BuiltinType::SveUint64x3:
476 case BuiltinType::SveInt64x4:
477 case BuiltinType::SveUint64x4:
478 case BuiltinType::SveBool:
479 case BuiltinType::SveBoolx2:
480 case BuiltinType::SveBoolx4:
481 case BuiltinType::SveFloat16:
482 case BuiltinType::SveFloat16x2:
483 case BuiltinType::SveFloat16x3:
484 case BuiltinType::SveFloat16x4:
485 case BuiltinType::SveFloat32:
486 case BuiltinType::SveFloat32x2:
487 case BuiltinType::SveFloat32x3:
488 case BuiltinType::SveFloat32x4:
489 case BuiltinType::SveFloat64:
490 case BuiltinType::SveFloat64x2:
491 case BuiltinType::SveFloat64x3:
492 case BuiltinType::SveFloat64x4:
493 case BuiltinType::SveBFloat16:
494 case BuiltinType::SveBFloat16x2:
495 case BuiltinType::SveBFloat16x3:
496 case BuiltinType::SveBFloat16x4: {
498 Context.getBuiltinVectorTypeInfo(cast<BuiltinType>(Ty));
499 return llvm::ScalableVectorType::get(ConvertType(Info.ElementType),
500 Info.EC.getKnownMinValue() *
501 Info.NumVectors);
502 }
503 case BuiltinType::SveCount:
504 return llvm::TargetExtType::get(getLLVMContext(), "aarch64.svcount");
505#define PPC_VECTOR_TYPE(Name, Id, Size) \
506 case BuiltinType::Id: \
507 ResultType = \
508 llvm::FixedVectorType::get(ConvertType(Context.BoolTy), Size); \
509 break;
510#include "clang/Basic/PPCTypes.def"
511#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
512#include "clang/Basic/RISCVVTypes.def"
513 {
515 Context.getBuiltinVectorTypeInfo(cast<BuiltinType>(Ty));
516 // Tuple types are expressed as aggregregate types of the same scalable
517 // vector type (e.g. vint32m1x2_t is two vint32m1_t, which is {<vscale x
518 // 2 x i32>, <vscale x 2 x i32>}).
519 if (Info.NumVectors != 1) {
520 llvm::Type *EltTy = llvm::ScalableVectorType::get(
521 ConvertType(Info.ElementType), Info.EC.getKnownMinValue());
523 return llvm::StructType::get(getLLVMContext(), EltTys);
524 }
525 return llvm::ScalableVectorType::get(ConvertType(Info.ElementType),
526 Info.EC.getKnownMinValue() *
527 Info.NumVectors);
528 }
529#define WASM_REF_TYPE(Name, MangledName, Id, SingletonId, AS) \
530 case BuiltinType::Id: { \
531 if (BuiltinType::Id == BuiltinType::WasmExternRef) \
532 ResultType = CGM.getTargetCodeGenInfo().getWasmExternrefReferenceType(); \
533 else \
534 llvm_unreachable("Unexpected wasm reference builtin type!"); \
535 } break;
536#include "clang/Basic/WebAssemblyReferenceTypes.def"
537 case BuiltinType::Dependent:
538#define BUILTIN_TYPE(Id, SingletonId)
539#define PLACEHOLDER_TYPE(Id, SingletonId) \
540 case BuiltinType::Id:
541#include "clang/AST/BuiltinTypes.def"
542 llvm_unreachable("Unexpected placeholder builtin type!");
543 }
544 break;
545 }
546 case Type::Auto:
547 case Type::DeducedTemplateSpecialization:
548 llvm_unreachable("Unexpected undeduced type!");
549 case Type::Complex: {
550 llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType());
551 ResultType = llvm::StructType::get(EltTy, EltTy);
552 break;
553 }
554 case Type::LValueReference:
555 case Type::RValueReference: {
556 const ReferenceType *RTy = cast<ReferenceType>(Ty);
557 QualType ETy = RTy->getPointeeType();
558 unsigned AS = getTargetAddressSpace(ETy);
559 ResultType = llvm::PointerType::get(getLLVMContext(), AS);
560 break;
561 }
562 case Type::Pointer: {
563 const PointerType *PTy = cast<PointerType>(Ty);
564 QualType ETy = PTy->getPointeeType();
565 unsigned AS = getTargetAddressSpace(ETy);
566 ResultType = llvm::PointerType::get(getLLVMContext(), AS);
567 break;
568 }
569
570 case Type::VariableArray: {
571 const VariableArrayType *A = cast<VariableArrayType>(Ty);
572 assert(A->getIndexTypeCVRQualifiers() == 0 &&
573 "FIXME: We only handle trivial array types so far!");
574 // VLAs resolve to the innermost element type; this matches
575 // the return of alloca, and there isn't any obviously better choice.
576 ResultType = ConvertTypeForMem(A->getElementType());
577 break;
578 }
579 case Type::IncompleteArray: {
580 const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
581 assert(A->getIndexTypeCVRQualifiers() == 0 &&
582 "FIXME: We only handle trivial array types so far!");
583 // int X[] -> [0 x int], unless the element type is not sized. If it is
584 // unsized (e.g. an incomplete struct) just use [0 x i8].
585 ResultType = ConvertTypeForMem(A->getElementType());
586 if (!ResultType->isSized()) {
587 SkippedLayout = true;
588 ResultType = llvm::Type::getInt8Ty(getLLVMContext());
589 }
590 ResultType = llvm::ArrayType::get(ResultType, 0);
591 break;
592 }
593 case Type::ArrayParameter:
594 case Type::ConstantArray: {
595 const ConstantArrayType *A = cast<ConstantArrayType>(Ty);
596 llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
597
598 // Lower arrays of undefined struct type to arrays of i8 just to have a
599 // concrete type.
600 if (!EltTy->isSized()) {
601 SkippedLayout = true;
602 EltTy = llvm::Type::getInt8Ty(getLLVMContext());
603 }
604
605 ResultType = llvm::ArrayType::get(EltTy, A->getZExtSize());
606 break;
607 }
608 case Type::ExtVector:
609 case Type::Vector: {
610 const auto *VT = cast<VectorType>(Ty);
611 // An ext_vector_type of Bool is really a vector of bits.
612 llvm::Type *IRElemTy = VT->isExtVectorBoolType()
613 ? llvm::Type::getInt1Ty(getLLVMContext())
614 : ConvertType(VT->getElementType());
615 ResultType = llvm::FixedVectorType::get(IRElemTy, VT->getNumElements());
616 break;
617 }
618 case Type::ConstantMatrix: {
619 const ConstantMatrixType *MT = cast<ConstantMatrixType>(Ty);
620 ResultType =
621 llvm::FixedVectorType::get(ConvertType(MT->getElementType()),
622 MT->getNumRows() * MT->getNumColumns());
623 break;
624 }
625 case Type::FunctionNoProto:
626 case Type::FunctionProto:
627 ResultType = ConvertFunctionTypeInternal(T);
628 break;
629 case Type::ObjCObject:
630 ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
631 break;
632
633 case Type::ObjCInterface: {
634 // Objective-C interfaces are always opaque (outside of the
635 // runtime, which can do whatever it likes); we never refine
636 // these.
637 llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
638 if (!T)
639 T = llvm::StructType::create(getLLVMContext());
640 ResultType = T;
641 break;
642 }
643
644 case Type::ObjCObjectPointer:
645 ResultType = llvm::PointerType::getUnqual(getLLVMContext());
646 break;
647
648 case Type::Enum: {
649 const EnumDecl *ED = cast<EnumType>(Ty)->getDecl();
650 if (ED->isCompleteDefinition() || ED->isFixed())
651 return ConvertType(ED->getIntegerType());
652 // Return a placeholder 'i32' type. This can be changed later when the
653 // type is defined (see UpdateCompletedType), but is likely to be the
654 // "right" answer.
655 ResultType = llvm::Type::getInt32Ty(getLLVMContext());
656 break;
657 }
658
659 case Type::BlockPointer: {
660 // Block pointers lower to function type. For function type,
661 // getTargetAddressSpace() returns default address space for
662 // function pointer i.e. program address space. Therefore, for block
663 // pointers, it is important to pass the pointee AST address space when
664 // calling getTargetAddressSpace(), to ensure that we get the LLVM IR
665 // address space for data pointers and not function pointers.
666 const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
667 unsigned AS = Context.getTargetAddressSpace(FTy.getAddressSpace());
668 ResultType = llvm::PointerType::get(getLLVMContext(), AS);
669 break;
670 }
671
672 case Type::MemberPointer: {
673 auto *MPTy = cast<MemberPointerType>(Ty);
674 if (!getCXXABI().isMemberPointerConvertible(MPTy)) {
675 auto *C = MPTy->getClass();
676 auto Insertion = RecordsWithOpaqueMemberPointers.insert({C, nullptr});
677 if (Insertion.second)
678 Insertion.first->second = llvm::StructType::create(getLLVMContext());
679 ResultType = Insertion.first->second;
680 } else {
681 ResultType = getCXXABI().ConvertMemberPointerType(MPTy);
682 }
683 break;
684 }
685
686 case Type::Atomic: {
687 QualType valueType = cast<AtomicType>(Ty)->getValueType();
688 ResultType = ConvertTypeForMem(valueType);
689
690 // Pad out to the inflated size if necessary.
691 uint64_t valueSize = Context.getTypeSize(valueType);
692 uint64_t atomicSize = Context.getTypeSize(Ty);
693 if (valueSize != atomicSize) {
694 assert(valueSize < atomicSize);
695 llvm::Type *elts[] = {
696 ResultType,
697 llvm::ArrayType::get(CGM.Int8Ty, (atomicSize - valueSize) / 8)
698 };
699 ResultType =
700 llvm::StructType::get(getLLVMContext(), llvm::ArrayRef(elts));
701 }
702 break;
703 }
704 case Type::Pipe: {
705 ResultType = CGM.getOpenCLRuntime().getPipeType(cast<PipeType>(Ty));
706 break;
707 }
708 case Type::BitInt: {
709 const auto &EIT = cast<BitIntType>(Ty);
710 ResultType = llvm::Type::getIntNTy(getLLVMContext(), EIT->getNumBits());
711 break;
712 }
713 }
714
715 assert(ResultType && "Didn't convert a type?");
716 assert((!CachedType || CachedType == ResultType) &&
717 "Cached type doesn't match computed type");
718
719 TypeCache[Ty] = ResultType;
720 return ResultType;
721}
722
724 return isPaddedAtomicType(type->castAs<AtomicType>());
725}
726
728 return Context.getTypeSize(type) != Context.getTypeSize(type->getValueType());
729}
730
731/// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
733 // TagDecl's are not necessarily unique, instead use the (clang)
734 // type connected to the decl.
735 const Type *Key = Context.getTagDeclType(RD).getTypePtr();
736
737 llvm::StructType *&Entry = RecordDeclTypes[Key];
738
739 // If we don't have a StructType at all yet, create the forward declaration.
740 if (!Entry) {
741 Entry = llvm::StructType::create(getLLVMContext());
742 addRecordTypeName(RD, Entry, "");
743 }
744 llvm::StructType *Ty = Entry;
745
746 // If this is still a forward declaration, or the LLVM type is already
747 // complete, there's nothing more to do.
748 RD = RD->getDefinition();
749 if (!RD || !RD->isCompleteDefinition() || !Ty->isOpaque())
750 return Ty;
751
752 // Force conversion of non-virtual base classes recursively.
753 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
754 for (const auto &I : CRD->bases()) {
755 if (I.isVirtual()) continue;
756 ConvertRecordDeclType(I.getType()->castAs<RecordType>()->getDecl());
757 }
758 }
759
760 // Layout fields.
761 std::unique_ptr<CGRecordLayout> Layout = ComputeRecordLayout(RD, Ty);
762 CGRecordLayouts[Key] = std::move(Layout);
763
764 // If this struct blocked a FunctionType conversion, then recompute whatever
765 // was derived from that.
766 // FIXME: This is hugely overconservative.
767 if (SkippedLayout)
768 TypeCache.clear();
769
770 return Ty;
771}
772
773/// getCGRecordLayout - Return record layout info for the given record decl.
774const CGRecordLayout &
776 const Type *Key = Context.getTagDeclType(RD).getTypePtr();
777
778 auto I = CGRecordLayouts.find(Key);
779 if (I != CGRecordLayouts.end())
780 return *I->second;
781 // Compute the type information.
783
784 // Now try again.
785 I = CGRecordLayouts.find(Key);
786
787 assert(I != CGRecordLayouts.end() &&
788 "Unable to find record layout information for type");
789 return *I->second;
790}
791
793 assert((T->isAnyPointerType() || T->isBlockPointerType()) && "Invalid type");
794 return isZeroInitializable(T);
795}
796
798 if (T->getAs<PointerType>())
799 return Context.getTargetNullPointerValue(T) == 0;
800
801 if (const auto *AT = Context.getAsArrayType(T)) {
802 if (isa<IncompleteArrayType>(AT))
803 return true;
804 if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
805 if (Context.getConstantArrayElementCount(CAT) == 0)
806 return true;
807 T = Context.getBaseElementType(T);
808 }
809
810 // Records are non-zero-initializable if they contain any
811 // non-zero-initializable subobjects.
812 if (const RecordType *RT = T->getAs<RecordType>()) {
813 const RecordDecl *RD = RT->getDecl();
814 return isZeroInitializable(RD);
815 }
816
817 // We have to ask the ABI about member pointers.
818 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
819 return getCXXABI().isZeroInitializable(MPT);
820
821 // Everything else is okay.
822 return true;
823}
824
827}
828
830 // Return the address space for the type. If the type is a
831 // function type without an address space qualifier, the
832 // program address space is used. Otherwise, the target picks
833 // the best address space based on the type information
834 return T->isFunctionType() && !T.hasAddressSpace()
835 ? getDataLayout().getProgramAddressSpace()
836 : getContext().getTargetAddressSpace(T.getAddressSpace());
837}
Defines the clang::ASTContext interface.
static llvm::Type * getTypeForFormat(llvm::LLVMContext &VMContext, const llvm::fltSemantics &format, bool UseNativeHalf=false)
static Decl::Kind getKind(const Decl *D)
Definition: DeclBase.cpp:1114
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
llvm::MachO::Target Target
Definition: MachO.h:47
BuiltinVectorTypeInfo getBuiltinVectorTypeInfo(const BuiltinType *VecTy) const
Returns the element type, element count and number of vectors (in case of tuple) for a builtin vector...
const llvm::fltSemantics & getFloatTypeSemantics(QualType T) const
Return the APFloat 'semantics' for the specified scalar floating point type.
QualType getTagDeclType(const TagDecl *Decl) const
Return the unique reference to the type for the specified TagDecl (struct/union/class/enum) decl.
QualType getRecordType(const RecordDecl *Decl) const
uint64_t getTargetNullPointerValue(QualType QT) const
Get target-dependent integer value for null pointer which is used for constant folding.
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:2563
const LangOptions & getLangOpts() const
Definition: ASTContext.h:775
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
const clang::PrintingPolicy & getPrintingPolicy() const
Definition: ASTContext.h:697
const ArrayType * getAsArrayType(QualType T) const
Type Query functions.
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
Definition: ASTContext.h:2329
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:757
uint64_t getConstantArrayElementCount(const ConstantArrayType *CA) const
Return number of constant array elements.
unsigned getTargetAddressSpace(LangAS AS) const
QualType getElementType() const
Definition: Type.h:3320
unsigned getIndexTypeCVRQualifiers() const
Definition: Type.h:3330
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
Represents a canonical, potentially-qualified type.
Definition: CanonicalType.h:65
const T * getTypePtr() const
Retrieve the underlying type pointer, which refers to a canonical type.
Definition: CanonicalType.h:83
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
virtual bool isMemberPointerConvertible(const MemberPointerType *MPT) const
Return whether or not a member pointers type is convertible to an IR type.
Definition: CGCXXABI.h:213
virtual llvm::Type * ConvertMemberPointerType(const MemberPointerType *MPT)
Find the LLVM type used to represent the given member pointer type.
Definition: CGCXXABI.cpp:43
virtual bool isZeroInitializable(const MemberPointerType *MPT)
Return true if the given member pointer can be zero-initialized (in the C++ sense) with an LLVM zeroi...
Definition: CGCXXABI.cpp:123
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
Definition: CGDebugInfo.h:55
CGFunctionInfo - Class to encapsulate the information about a function definition.
virtual llvm::Type * getPipeType(const PipeType *T, StringRef Name, llvm::Type *&PipeTy)
virtual llvm::Type * convertOpenCLSpecificType(const Type *T)
CGRecordLayout - This class handles struct and union layout info while lowering AST types to LLVM typ...
bool isZeroInitializable() const
Check whether this struct can be C++ zero-initialized with a zeroinitializer.
This class organizes the cross-function state that is used while generating LLVM code.
CGDebugInfo * getModuleDebugInfo()
bool isPaddedAtomicType(QualType type)
CGOpenCLRuntime & getOpenCLRuntime()
Return a reference to the configured OpenCL runtime.
const TargetCodeGenInfo & getTargetCodeGenInfo()
const CodeGenOptions & getCodeGenOpts() const
CodeGenTypes(CodeGenModule &cgm)
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
bool isPointerZeroInitializable(QualType T)
Check if the pointer type can be zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
const CodeGenOptions & getCodeGenOpts() const
ASTContext & getContext() const
Definition: CodeGenTypes.h:108
const CGFunctionInfo & arrangeFreeFunctionType(CanQual< FunctionProtoType > Ty)
Arrange the argument and result information for a value of the given freestanding function type.
Definition: CGCall.cpp:206
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition: CGCall.cpp:1627
bool isFuncTypeConvertible(const FunctionType *FT)
isFuncTypeConvertible - Utility to check whether a function type can be converted to an LLVM type (i....
std::unique_ptr< CGRecordLayout > ComputeRecordLayout(const RecordDecl *D, llvm::StructType *Ty)
Compute a new LLVM record layout object for the given record.
const CGRecordLayout & getCGRecordLayout(const RecordDecl *)
getCGRecordLayout - Return record layout info for the given record decl.
unsigned getTargetAddressSpace(QualType T) const
llvm::StructType * ConvertRecordDeclType(const RecordDecl *TD)
ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
void RefreshTypeCacheForClass(const CXXRecordDecl *RD)
Remove stale types from the type cache when an inheritance model gets assigned to a class.
bool isRecordLayoutComplete(const Type *Ty) const
isRecordLayoutComplete - Return true if the specified type is already completely laid out.
void UpdateCompletedType(const TagDecl *TD)
UpdateCompletedType - When we find the full definition for a TagDecl, replace the 'opaque' type we pr...
llvm::LLVMContext & getLLVMContext()
Definition: CodeGenTypes.h:112
const llvm::DataLayout & getDataLayout() const
Definition: CodeGenTypes.h:104
CGCXXABI & getCXXABI() const
Definition: CodeGenTypes.h:111
bool isFuncParamTypeConvertible(QualType Ty)
isFuncParamTypeConvertible - Return true if the specified type in a function parameter or result posi...
bool isZeroInitializable(QualType T)
IsZeroInitializable - Return whether a type can be zero-initialized (in the C++ sense) with an LLVM z...
llvm::Type * ConvertTypeForMem(QualType T, bool ForBitField=false)
ConvertTypeForMem - Convert type T into a llvm::Type.
void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty, StringRef suffix)
addRecordTypeName - Compute a name from the given record decl with an optional suffix and name the gi...
virtual llvm::Type * getCUDADeviceBuiltinSurfaceDeviceType() const
Return the device-side type for the CUDA device builtin surface type.
Definition: TargetInfo.h:379
virtual llvm::Type * getCUDADeviceBuiltinTextureDeviceType() const
Return the device-side type for the CUDA device builtin texture type.
Definition: TargetInfo.h:384
Represents the canonical version of C arrays with a specified constant size.
Definition: Type.h:3346
uint64_t getZExtSize() const
Return the size zero-extended as a uint64_t.
Definition: Type.h:3422
Represents a concrete matrix type with constant number of rows and columns.
Definition: Type.h:3957
unsigned getNumColumns() const
Returns the number of columns in the matrix.
Definition: Type.h:3978
unsigned getNumRows() const
Returns the number of rows in the matrix.
Definition: Type.h:3975
ASTContext & getASTContext() const LLVM_READONLY
Definition: DeclBase.cpp:501
DeclContext * getDeclContext()
Definition: DeclBase.h:453
Represents an enum.
Definition: Decl.h:3847
bool isFixed() const
Returns true if this is an Objective-C, C++11, or Microsoft-style enumeration with a fixed underlying...
Definition: Decl.h:4061
QualType getIntegerType() const
Return the integer type this enum decl corresponds to.
Definition: Decl.h:4007
Represents a K&R-style 'int foo()' function, which has no information available about its arguments.
Definition: Type.h:4401
Represents a prototype with parameter type info, e.g.
Definition: Type.h:4446
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: Type.h:4046
QualType getReturnType() const
Definition: Type.h:4363
Represents a C array with an unspecified size.
Definition: Type.h:3493
QualType getElementType() const
Returns type of the elements being stored in the matrix.
Definition: Type.h:3935
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition: Type.h:3250
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:270
void printQualifiedName(raw_ostream &OS) const
Returns a human-readable qualified name for this declaration, like A::B::i, for i being member of nam...
Definition: Decl.cpp:1690
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition: Type.h:2929
QualType getPointeeType() const
Definition: Type.h:2939
A (possibly-)qualified type.
Definition: Type.h:738
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition: Type.h:7149
LangAS getAddressSpace() const
Return the address space of this type.
Definition: Type.h:7275
bool isCanonical() const
Definition: Type.h:7206
Represents a struct/union/class.
Definition: Decl.h:4148
RecordDecl * getDefinition() const
Returns the RecordDecl that actually defines this struct/union/class.
Definition: Decl.h:4339
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:5339
RecordDecl * getDecl() const
Definition: Type.h:5349
Base for LValueReferenceType and RValueReferenceType.
Definition: Type.h:3170
QualType getPointeeType() const
Definition: Type.h:3188
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3564
StringRef getKindName() const
Definition: Decl.h:3755
bool isCompleteDefinition() const
Return true if this decl has its body fully specified.
Definition: Decl.h:3667
TypedefNameDecl * getTypedefNameForAnonDecl() const
Definition: Decl.h:3792
void printName(raw_ostream &OS, const PrintingPolicy &Policy) const override
Pretty-print the unqualified name of this declaration.
Definition: Decl.cpp:4770
bool isDependentType() const
Whether this declaration declares a type that is dependent, i.e., a type that somehow depends on temp...
Definition: Decl.h:3718
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:971
The base class of the type hierarchy.
Definition: Type.h:1607
bool isBlockPointerType() const
Definition: Type.h:7410
bool isConstantMatrixType() const
Definition: Type.h:7526
bool isCUDADeviceBuiltinSurfaceType() const
Check if the type is the CUDA device builtin surface type.
Definition: Type.cpp:4906
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition: Type.cpp:694
bool isExtVectorBoolType() const
Definition: Type.h:7516
bool isBitIntType() const
Definition: Type.h:7630
bool isCUDADeviceBuiltinTextureType() const
Check if the type is the CUDA device builtin texture type.
Definition: Type.cpp:4913
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types,...
Definition: Type.cpp:2350
bool isFunctionType() const
Definition: Type.h:7398
bool isAnyPointerType() const
Definition: Type.h:7406
TypeClass getTypeClass() const
Definition: Type.h:2094
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:7913
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:3412
Represents a C array with a specified size that is not an integer-constant-expression.
Definition: Type.h:3537
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
Describes how types, statements, expressions, and declarations should be printed.
Definition: PrettyPrinter.h:57
unsigned SuppressInlineNamespace
Suppress printing parts of scope specifiers that correspond to inline namespaces, where the name is u...