clang 24.0.0git
CIRGenTypes.h
Go to the documentation of this file.
1//===--- CIRGenTypes.h - Type translation for CIR CodeGen -------*- C++ -*-===//
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 -> CIR type lowering.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H
14#define LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H
15
16#include "ABIInfo.h"
17#include "CIRGenFunctionInfo.h"
18#include "CIRGenRecordLayout.h"
19
20#include "clang/AST/DeclCXX.h"
21#include "clang/AST/Type.h"
22#include "clang/Basic/ABI.h"
24
25#include "llvm/ADT/DenseSet.h"
26#include "llvm/ADT/SmallPtrSet.h"
27
28namespace clang {
29class ASTContext;
30class FunctionType;
31class GlobalDecl;
32class QualType;
33class TargetInfo;
34class Type;
35} // namespace clang
36
37namespace mlir {
38class Type;
39}
40
41namespace clang::CIRGen {
42
43class CallArgList;
44class CIRGenBuilderTy;
45class CIRGenCXXABI;
46class CIRGenModule;
47class FunctionArgList;
48
49/// This class organizes the cross-module state that is used while lowering
50/// AST types to CIR types.
52 CIRGenModule &cgm;
53 clang::ASTContext &astContext;
54 CIRGenBuilderTy &builder;
55 CIRGenCXXABI &theCXXABI;
56
57 const ABIInfo &theABIInfo;
58
59 /// Contains the CIR type for any converted RecordDecl.
60 llvm::DenseMap<const clang::Type *, std::unique_ptr<CIRGenRecordLayout>>
61 cirGenRecordLayouts;
62
63 /// Contains the CIR type for any converted RecordDecl
64 llvm::DenseMap<const clang::Type *, cir::RecordType> recordDeclTypes;
65
66 /// Hold memoized CIRGenFunctionInfo results
67 llvm::FoldingSet<CIRGenFunctionInfo> functionInfos;
68
69 /// This set keeps track of records that we're currently converting to a CIR
70 /// type. For example, when converting:
71 /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
72 /// types will be in this set.
74
76
77 /// Cache of record type keys known to be safe to convert (i.e.,
78 /// isSafeToConvert returned true). Cleared whenever recordsBeingLaidOut
79 /// changes, since the safety result depends on which records are currently
80 /// being laid out.
81 llvm::DenseSet<const clang::Type *> safeToConvertCache;
82
83 /// Heper for convertType.
84 mlir::Type convertFunctionTypeInternal(clang::QualType ft);
85
86public:
89
90 CIRGenBuilderTy &getBuilder() const { return builder; }
91 CIRGenModule &getCGModule() const { return cgm; }
92
93 /// Utility to check whether a function type can be converted to a CIR type
94 /// (i.e. doesn't depend on an incomplete tag type).
97
98 /// Derives the 'this' type for CIRGen purposes, i.e. ignoring method CVR
99 /// qualification.
101 const clang::CXXMethodDecl *md);
102
103 /// This map of clang::Type to mlir::Type (which includes CIR type) is a
104 /// cache of types that have already been processed.
105 using TypeCacheTy = llvm::DenseMap<const clang::Type *, mlir::Type>;
107
108 mlir::MLIRContext &getMLIRContext() const;
109 clang::ASTContext &getASTContext() const { return astContext; }
110
111 bool isRecordLayoutComplete(const clang::Type *ty) const;
112 bool noRecordsBeingLaidOut() const { return recordsBeingLaidOut.empty(); }
113 bool isRecordBeingLaidOut(const clang::Type *ty) const {
114 return recordsBeingLaidOut.count(ty);
115 }
116
117 /// Check if a record type key is in the safe-to-convert cache.
118 bool isCachedSafeToConvert(const clang::Type *key) const {
119 return safeToConvertCache.count(key);
120 }
121
122 /// Add a record type key to the safe-to-convert cache.
124 safeToConvertCache.insert(key);
125 }
126
127 const ABIInfo &getABIInfo() const { return theABIInfo; }
128
129 /// Convert a Clang type into a mlir::Type.
130 mlir::Type convertType(clang::QualType type);
131
133
134 std::unique_ptr<CIRGenRecordLayout>
136
137 std::string getRecordTypeName(const clang::RecordDecl *,
138 llvm::StringRef suffix);
139
141
142 /// Convert type T into an mlir::Type. This differs from convertType in that
143 /// it is used to convert to the memory representation for a type. For
144 /// example, the scalar representation for bool is i1, but the memory
145 /// representation is usually i8 or i32, depending on the target.
146 // TODO: convert this comment to account for MLIR's equivalence
147 mlir::Type convertTypeForMem(clang::QualType, bool forBitField = false);
148
149 /// Get the CIR function type for \arg Info.
150 cir::FuncType getFunctionType(const CIRGenFunctionInfo &info);
151
152 cir::FuncType getFunctionType(clang::GlobalDecl gd);
153
154 /// Determine if a C++ inheriting constructor should have parameters matching
155 /// those of its inherited constructor.
156 bool inheritingCtorHasParams(const InheritedConstructor &inherited,
158
159 // The arrangement methods are split into three families:
160 // - those meant to drive the signature and prologue/epilogue
161 // of a function declaration or definition,
162 // - those meant for the computation of the CIR type for an abstract
163 // appearance of a function, and
164 // - those meant for performing the CIR-generation of a call.
165 // They differ mainly in how they deal with optional (i.e. variadic)
166 // arguments, as well as unprototyped functions.
167 //
168 // Key points:
169 // - The CIRGenFunctionInfo for emitting a specific call site must include
170 // entries for the optional arguments.
171 // - The function type used at the call site must reflect the formal
172 // signature
173 // of the declaration being called, or else the call will go away.
174 // - For the most part, unprototyped functions are called by casting to a
175 // formal signature inferred from the specific argument types used at the
176 // call-site. However, some targets (e.g. x86-64) screw with this for
177 // compatability reasons.
178
180
181 /// UpdateCompletedType - when we find the full definition for a TagDecl,
182 /// replace the 'opaque' type we previously made for it if applicable.
183 void updateCompletedType(const clang::TagDecl *td);
184
185 /// Free functions are functions that are compatible with an ordinary C
186 /// function pointer type.
187 const CIRGenFunctionInfo &
189
190 /// Arrange the function info for a device kernel caller entry point (e.g. a
191 /// SYCL kernel caller).
192 const CIRGenFunctionInfo &
194 const FunctionArgList &args);
195
196 /// A builtin function is a freestanding function using the default
197 /// C conventions.
199 const CallArgList &args);
200
201 /// Return whether a type can be zero-initialized (in the C++ sense) with an
202 /// LLVM zeroinitializer.
204 bool isZeroInitializable(const RecordDecl *rd);
205
207 const CallArgList &args, const clang::CXXConstructorDecl *d,
208 clang::CXXCtorType ctorKind, unsigned extraPrefixArgs,
209 unsigned extraSuffixArgs, bool passProtoArgs = true);
210
211 const CIRGenFunctionInfo &
214 RequiredArgs required, unsigned numPrefixArgs);
215
216 /// C++ methods have some special rules and also have implicit parameters.
217 const CIRGenFunctionInfo &
220
221 const CIRGenFunctionInfo &
223 const clang::FunctionProtoType *ftp,
224 const clang::CXXMethodDecl *md);
225
227 const FunctionType *fnType);
228
229 const CIRGenFunctionInfo &
232 FunctionType::ExtInfo info, RequiredArgs required);
233
234 const CIRGenFunctionInfo &
236 const CIRGenFunctionInfo &
238
239 unsigned getTargetAddressSpace(QualType ty) const;
240};
241
242} // namespace clang::CIRGen
243
244#endif
Enums/classes describing ABI related information about constructors, destructors and thunks.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
C Language Family Type Representation.
C++ view class that accepts both !cir.struct and !cir.union types.
Definition CIRTypes.h:104
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
Implements C++ ABI-specific code generation functions.
This class organizes the cross-function state that is used while generating CIR code.
This class handles record and union layout info while lowering AST types to CIR types.
CIRGenModule & getCGModule() const
Definition CIRGenTypes.h:91
const CIRGenFunctionInfo & arrangeGlobalDeclaration(GlobalDecl gd)
const CIRGenFunctionInfo & arrangeCXXMethodDeclaration(const clang::CXXMethodDecl *md)
C++ methods have some special rules and also have implicit parameters.
unsigned getTargetAddressSpace(QualType ty) const
const CIRGenFunctionInfo & arrangeCXXStructorDeclaration(clang::GlobalDecl gd)
const CIRGenFunctionInfo & arrangeCIRFunctionInfo(CanQualType returnType, bool isInstanceMethod, llvm::ArrayRef< CanQualType > argTypes, FunctionType::ExtInfo info, RequiredArgs required)
const CIRGenFunctionInfo & arrangeFreeFunctionCall(const CallArgList &args, const FunctionType *fnType)
const CIRGenFunctionInfo & arrangeFreeFunctionType(CanQual< FunctionProtoType > fpt)
bool isZeroInitializable(clang::QualType ty)
Return whether a type can be zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
bool isFuncTypeConvertible(const clang::FunctionType *ft)
Utility to check whether a function type can be converted to a CIR type (i.e.
CIRGenTypes(CIRGenModule &cgm)
bool isRecordBeingLaidOut(const clang::Type *ty) const
CIRGenBuilderTy & getBuilder() const
Definition CIRGenTypes.h:90
const CIRGenFunctionInfo & arrangeBuiltinFunctionCall(QualType resultType, const CallArgList &args)
A builtin function is a freestanding function using the default C conventions.
const CIRGenFunctionInfo & arrangeCXXConstructorCall(const CallArgList &args, const clang::CXXConstructorDecl *d, clang::CXXCtorType ctorKind, unsigned extraPrefixArgs, unsigned extraSuffixArgs, bool passProtoArgs=true)
Arrange a call to a C++ method, passing the given arguments.
const CIRGenFunctionInfo & arrangeCXXMethodType(const clang::CXXRecordDecl *rd, const clang::FunctionProtoType *ftp, const clang::CXXMethodDecl *md)
Arrange the argument and result information for a call to an unknown C++ non-static member function o...
mlir::MLIRContext & getMLIRContext() const
bool isCachedSafeToConvert(const clang::Type *key) const
Check if a record type key is in the safe-to-convert cache.
cir::FuncType getFunctionType(const CIRGenFunctionInfo &info)
Get the CIR function type for.
bool isFuncParamTypeConvertible(clang::QualType type)
Return true if the specified type in a function parameter or result position can be converted to a CI...
bool inheritingCtorHasParams(const InheritedConstructor &inherited, CXXCtorType type)
Determine if a C++ inheriting constructor should have parameters matching those of its inherited cons...
clang::CanQualType deriveThisType(const clang::CXXRecordDecl *rd, const clang::CXXMethodDecl *md)
Derives the 'this' type for CIRGen purposes, i.e.
void updateCompletedType(const clang::TagDecl *td)
UpdateCompletedType - when we find the full definition for a TagDecl, replace the 'opaque' type we pr...
std::string getRecordTypeName(const clang::RecordDecl *, llvm::StringRef suffix)
bool noRecordsBeingLaidOut() const
const ABIInfo & getABIInfo() const
const CIRGenFunctionInfo & arrangeFunctionDeclaration(const clang::FunctionDecl *fd)
Free functions are functions that are compatible with an ordinary C function pointer type.
clang::ASTContext & getASTContext() const
bool isRecordLayoutComplete(const clang::Type *ty) const
Return true if the specified type is already completely laid out.
const CIRGenFunctionInfo & arrangeCXXMethodCall(const CallArgList &args, const clang::FunctionProtoType *type, RequiredArgs required, unsigned numPrefixArgs)
Arrange a call to a C++ method, passing the given arguments.
const CIRGenFunctionInfo & arrangeDeviceKernelCallerDeclaration(clang::QualType resultType, const FunctionArgList &args)
Arrange the function info for a device kernel caller entry point (e.g.
mlir::Type convertType(clang::QualType type)
Convert a Clang type into a mlir::Type.
const CIRGenRecordLayout & getCIRGenRecordLayout(const clang::RecordDecl *rd)
Return record layout info for the given record decl.
std::unique_ptr< CIRGenRecordLayout > computeRecordLayout(const clang::RecordDecl *rd, cir::RecordType *ty)
mlir::Type convertRecordDeclType(const clang::RecordDecl *recordDecl)
Lay out a tagged decl type like struct or union.
void cacheSafeToConvert(const clang::Type *key)
Add a record type key to the safe-to-convert cache.
mlir::Type convertTypeForMem(clang::QualType, bool forBitField=false)
Convert type T into an mlir::Type.
llvm::DenseMap< const clang::Type *, mlir::Type > TypeCacheTy
This map of clang::Type to mlir::Type (which includes CIR type) is a cache of types that have already...
Type for representing both the decl and type of parameters to a function.
Definition CIRGenCall.h:193
A class for recording the number of arguments that a function signature requires.
Represents a C++ constructor within a class.
Definition DeclCXX.h:2637
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2145
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
Represents a canonical, potentially-qualified type.
Represents a function declaration or definition.
Definition Decl.h:2058
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5421
A class which abstracts out some details necessary for making a call.
Definition TypeBase.h:4728
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition TypeBase.h:4617
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:57
Description of a constructor that was inherited from a base class.
Definition DeclCXX.h:2608
A (possibly-)qualified type.
Definition TypeBase.h:938
Represents a struct/union/class.
Definition Decl.h:4459
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3851
Exposes information about the current target.
Definition TargetInfo.h:227
The base class of the type hierarchy.
Definition TypeBase.h:1879
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const internal::VariadicDynCastAllOfMatcher< Decl, RecordDecl > recordDecl
Matches class, struct, and union declarations.
Top level wrappers for InstallAPI frontend operations.
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
CXXCtorType
C++ constructor types.
Definition ABI.h:24
bool isInstanceMethod(const Decl *D)
Definition Attr.h:152