clang 23.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;
47
48/// This class organizes the cross-module state that is used while lowering
49/// AST types to CIR types.
51 CIRGenModule &cgm;
52 clang::ASTContext &astContext;
53 CIRGenBuilderTy &builder;
54 CIRGenCXXABI &theCXXABI;
55
56 const ABIInfo &theABIInfo;
57
58 /// Contains the CIR type for any converted RecordDecl.
59 llvm::DenseMap<const clang::Type *, std::unique_ptr<CIRGenRecordLayout>>
60 cirGenRecordLayouts;
61
62 /// Contains the CIR type for any converted RecordDecl
63 llvm::DenseMap<const clang::Type *, cir::RecordType> recordDeclTypes;
64
65 /// Hold memoized CIRGenFunctionInfo results
66 llvm::FoldingSet<CIRGenFunctionInfo> functionInfos;
67
68 /// This set keeps track of records that we're currently converting to a CIR
69 /// type. For example, when converting:
70 /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
71 /// types will be in this set.
73
75
76 /// Cache of record type keys known to be safe to convert (i.e.,
77 /// isSafeToConvert returned true). Cleared whenever recordsBeingLaidOut
78 /// changes, since the safety result depends on which records are currently
79 /// being laid out.
80 llvm::DenseSet<const clang::Type *> safeToConvertCache;
81
82 /// Heper for convertType.
83 mlir::Type convertFunctionTypeInternal(clang::QualType ft);
84
85public:
88
89 CIRGenBuilderTy &getBuilder() const { return builder; }
90 CIRGenModule &getCGModule() const { return cgm; }
91
92 /// Utility to check whether a function type can be converted to a CIR type
93 /// (i.e. doesn't depend on an incomplete tag type).
96
97 /// Derives the 'this' type for CIRGen purposes, i.e. ignoring method CVR
98 /// qualification.
100 const clang::CXXMethodDecl *md);
101
102 /// This map of clang::Type to mlir::Type (which includes CIR type) is a
103 /// cache of types that have already been processed.
104 using TypeCacheTy = llvm::DenseMap<const clang::Type *, mlir::Type>;
106
107 mlir::MLIRContext &getMLIRContext() const;
108 clang::ASTContext &getASTContext() const { return astContext; }
109
110 bool isRecordLayoutComplete(const clang::Type *ty) const;
111 bool noRecordsBeingLaidOut() const { return recordsBeingLaidOut.empty(); }
112 bool isRecordBeingLaidOut(const clang::Type *ty) const {
113 return recordsBeingLaidOut.count(ty);
114 }
115
116 /// Check if a record type key is in the safe-to-convert cache.
117 bool isCachedSafeToConvert(const clang::Type *key) const {
118 return safeToConvertCache.count(key);
119 }
120
121 /// Add a record type key to the safe-to-convert cache.
123 safeToConvertCache.insert(key);
124 }
125
126 const ABIInfo &getABIInfo() const { return theABIInfo; }
127
128 /// Convert a Clang type into a mlir::Type.
129 mlir::Type convertType(clang::QualType type);
130
132
133 std::unique_ptr<CIRGenRecordLayout>
134 computeRecordLayout(const clang::RecordDecl *rd, cir::RecordType *ty);
135
136 std::string getRecordTypeName(const clang::RecordDecl *,
137 llvm::StringRef suffix);
138
140
141 /// Convert type T into an mlir::Type. This differs from convertType in that
142 /// it is used to convert to the memory representation for a type. For
143 /// example, the scalar representation for bool is i1, but the memory
144 /// representation is usually i8 or i32, depending on the target.
145 // TODO: convert this comment to account for MLIR's equivalence
146 mlir::Type convertTypeForMem(clang::QualType, bool forBitField = false);
147
148 /// Get the CIR function type for \arg Info.
149 cir::FuncType getFunctionType(const CIRGenFunctionInfo &info);
150
151 cir::FuncType getFunctionType(clang::GlobalDecl gd);
152
153 /// Determine if a C++ inheriting constructor should have parameters matching
154 /// those of its inherited constructor.
155 bool inheritingCtorHasParams(const InheritedConstructor &inherited,
157
158 // The arrangement methods are split into three families:
159 // - those meant to drive the signature and prologue/epilogue
160 // of a function declaration or definition,
161 // - those meant for the computation of the CIR type for an abstract
162 // appearance of a function, and
163 // - those meant for performing the CIR-generation of a call.
164 // They differ mainly in how they deal with optional (i.e. variadic)
165 // arguments, as well as unprototyped functions.
166 //
167 // Key points:
168 // - The CIRGenFunctionInfo for emitting a specific call site must include
169 // entries for the optional arguments.
170 // - The function type used at the call site must reflect the formal
171 // signature
172 // of the declaration being called, or else the call will go away.
173 // - For the most part, unprototyped functions are called by casting to a
174 // formal signature inferred from the specific argument types used at the
175 // call-site. However, some targets (e.g. x86-64) screw with this for
176 // compatability reasons.
177
179
180 /// UpdateCompletedType - when we find the full definition for a TagDecl,
181 /// replace the 'opaque' type we previously made for it if applicable.
182 void updateCompletedType(const clang::TagDecl *td);
183
184 /// Free functions are functions that are compatible with an ordinary C
185 /// function pointer type.
186 const CIRGenFunctionInfo &
188
189 /// Return whether a type can be zero-initialized (in the C++ sense) with an
190 /// LLVM zeroinitializer.
192 bool isZeroInitializable(const RecordDecl *rd);
193
195 const CallArgList &args, const clang::CXXConstructorDecl *d,
196 clang::CXXCtorType ctorKind, unsigned extraPrefixArgs,
197 unsigned extraSuffixArgs, bool passProtoArgs = true);
198
199 const CIRGenFunctionInfo &
202 RequiredArgs required, unsigned numPrefixArgs);
203
204 /// C++ methods have some special rules and also have implicit parameters.
205 const CIRGenFunctionInfo &
208
209 const CIRGenFunctionInfo &
211 const clang::FunctionProtoType *ftp,
212 const clang::CXXMethodDecl *md);
213
215 const FunctionType *fnType);
216
217 const CIRGenFunctionInfo &
220 FunctionType::ExtInfo info, RequiredArgs required);
221
222 const CIRGenFunctionInfo &
224 const CIRGenFunctionInfo &
226
227 unsigned getTargetAddressSpace(QualType ty) const;
228};
229
230} // namespace clang::CIRGen
231
232#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.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:227
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:90
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:89
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.
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...
A class for recording the number of arguments that a function signature requires.
Represents a C++ constructor within a class.
Definition DeclCXX.h:2620
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2132
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:2018
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5362
A class which abstracts out some details necessary for making a call.
Definition TypeBase.h:4669
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition TypeBase.h:4558
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:57
Description of a constructor that was inherited from a base class.
Definition DeclCXX.h:2591
A (possibly-)qualified type.
Definition TypeBase.h:937
Represents a struct/union/class.
Definition Decl.h:4343
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3735
Exposes information about the current target.
Definition TargetInfo.h:227
The base class of the type hierarchy.
Definition TypeBase.h:1871
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const internal::VariadicDynCastAllOfMatcher< Decl, RecordDecl > recordDecl
Matches class, struct, and union declarations.
The JSON file list parser is used to communicate input to InstallAPI.
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:120