clang 24.0.0git
ExprConstShared.h
Go to the documentation of this file.
1//===--- ExprConstShared.h - Shared consetxpr functionality ----*- 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// Shared functionality between the new constant expression
10// interpreter (AST/ByteCode/) and the current one (ExprConstant.cpp).
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_LIB_AST_EXPRCONSTSHARED_H
15#define LLVM_CLANG_LIB_AST_EXPRCONSTSHARED_H
16
17#include "ByteCode/State.h"
19#include <cstdint>
20#include <optional>
21
22namespace llvm {
23class APFloat;
24class APSInt;
25class APInt;
26}
27namespace clang {
28class QualType;
29class LangOptions;
30class ASTContext;
31class CharUnits;
32class Expr;
33class CallExpr;
34class CXXRecordDecl;
35class FunctionDecl;
36
38 DynAllocKind AllocKind);
39} // namespace clang
40using namespace clang;
41/// Values returned by __builtin_classify_type, chosen to match the values
42/// produced by GCC's builtin.
43enum class GCCTypeClass {
44 None = -1,
45 Void = 0,
47 // GCC reserves 2 for character types, but instead classifies them as
48 // integers.
49 Enum = 3,
50 Bool = 4,
52 // GCC reserves 6 for references, but appears to never use it (because
53 // expressions never have reference type, presumably).
57 // GCC reserves 10 for functions, but does not use it since GCC version 6 due
58 // to decay to pointer. (Prior to version 6 it was only used in C++ mode).
59 // GCC claims to reserve 11 for pointers to member functions, but *actually*
60 // uses 12 for that purpose, same as for a class or struct. Maybe it
61 // internally implements a pointer to member as a struct? Who knows.
62 PointerToMemberFunction = 12, // Not a bug, see above.
64 Union = 13,
65 // GCC reserves 14 for arrays, but does not use it since GCC version 6 due to
66 // decay to pointer. (Prior to version 6 it was only used in C++ mode).
67 // GCC reserves 15 for strings, but actually uses 5 (pointer) for string
68 // literals.
69 // Lang = 16,
70 // OpaqueType = 17,
71 BitInt = 18,
72 Vector = 19
73};
74
76 const LangOptions &LangOpts);
77
78void HandleComplexComplexMul(llvm::APFloat A, llvm::APFloat B, llvm::APFloat C,
79 llvm::APFloat D, llvm::APFloat &ResR,
80 llvm::APFloat &ResI);
81void HandleComplexComplexDiv(llvm::APFloat A, llvm::APFloat B, llvm::APFloat C,
82 llvm::APFloat D, llvm::APFloat &ResR,
83 llvm::APFloat &ResI);
84
85CharUnits GetAlignOfExpr(const ASTContext &Ctx, const Expr *E,
86 UnaryExprOrTypeTrait ExprKind);
87
88/// Convert a builtin ID to the canonical x86 builtin ID the constant evaluators
89/// dispatch on in their x86 target-specific cases.
90///
91/// Target-independent builtins are returned unchanged. An x86 target builtin
92/// (including an auxiliary-target x86 builtin, whose ID is shifted past the
93/// primary target's builtins) is translated to its canonical X86::BI* value.
94/// Any other target's builtin returns 0: the constant evaluators only fold x86
95/// target builtins, and target builtin IDs of different targets overlap (each
96/// numbers from Builtin::FirstTSBuiltin), so an unrelated target's ID must not
97/// be mistaken for an x86 one.
98///
99/// The ID-based overload performs no work beyond a single comparison for
100/// target-independent builtins, so it is suitable for hot paths (e.g. the
101/// bytecode interpreter's builtin dispatch) where re-deriving the ID from the
102/// call expression would be wasteful.
104 unsigned BuiltinID);
106 const CallExpr *E);
107
109uint8_t GFNIMul(uint8_t AByte, uint8_t BByte);
110uint8_t GFNIAffine(uint8_t XByte, const llvm::APInt &AQword,
111 const llvm::APSInt &Imm, bool Inverse = false);
112llvm::APSInt NormalizeRotateAmount(const llvm::APSInt &Value,
113 const llvm::APSInt &Amount);
114
115std::optional<llvm::APFloat>
116EvalScalarMinMaxFp(const llvm::APFloat &A, const llvm::APFloat &B,
117 std::optional<llvm::APSInt> RoundingMode, bool IsMin);
118
119const Expr *ignorePointerCastsAndParens(const Expr *E);
120
123/// Determines whether the given kind of constant expression is only ever
124/// used for name mangling. If so, it's permitted to reference things that we
125/// can't generate code for (in particular, dllimported functions).
127 switch (Kind) {
128 case ConstantExprKind::Normal:
129 case ConstantExprKind::ClassTemplateArgument:
130 case ConstantExprKind::ImmediateInvocation:
131 case ConstantExprKind::Initializer:
132 // Note that non-type template arguments of class type are emitted as
133 // template parameter objects.
134 return false;
135
136 case ConstantExprKind::NonClassTemplateArgument:
137 return true;
138 }
139 llvm_unreachable("unknown ConstantExprKind");
140}
141
143 switch (Kind) {
144 case ConstantExprKind::Normal:
145 case ConstantExprKind::ImmediateInvocation:
146 case ConstantExprKind::Initializer:
147 return false;
148
149 case ConstantExprKind::ClassTemplateArgument:
150 case ConstantExprKind::NonClassTemplateArgument:
151 return true;
152 }
153 llvm_unreachable("unknown ConstantExprKind");
154}
155
156/// Should this call expression be treated as forming an opaque constant?
157inline bool isOpaqueConstantCall(const CallExpr *E) {
158 unsigned Builtin = E->getBuiltinCallee();
159 return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString ||
160 Builtin == Builtin::BI__builtin___NSStringMakeConstantString ||
161 Builtin == Builtin::BI__builtin_ptrauth_sign_constant ||
162 Builtin == Builtin::BI__builtin_function_start);
163}
164
165bool isGlobalLValue(const ValueDecl *D, const Expr *E);
166
167/// Whether we can instantiate FD during constant evaluation
169
170#endif
Defines enumerations for traits support.
llvm::APSInt APSInt
Definition Compiler.cpp:26
const Expr * ignorePointerCastsAndParens(const Expr *E)
A more selective version of E->IgnoreParenCasts for tryEvaluateBuiltinObjectSize. This ignores some c...
bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD)
Determine whether a type would actually be read by an lvalue-to-rvalue conversion.
bool isTemplateArgument(ConstantExprKind Kind)
unsigned ConvertBuiltinIDToX86BuiltinID(const ASTContext &Ctx, unsigned BuiltinID)
Convert a builtin ID to the canonical x86 builtin ID the constant evaluators dispatch on in their x86...
uint8_t GFNIAffine(uint8_t XByte, const llvm::APInt &AQword, const llvm::APSInt &Imm, bool Inverse=false)
CharUnits GetAlignOfExpr(const ASTContext &Ctx, const Expr *E, UnaryExprOrTypeTrait ExprKind)
bool isOpaqueConstantCall(const CallExpr *E)
Should this call expression be treated as forming an opaque constant?
bool isForManglingOnly(ConstantExprKind Kind)
Determines whether the given kind of constant expression is only ever used for name mangling.
llvm::APSInt NormalizeRotateAmount(const llvm::APSInt &Value, const llvm::APSInt &Amount)
bool isGlobalLValue(const ValueDecl *D, const Expr *E)
bool FunctionDefinitionCanBeLazilyInstantiated(const FunctionDecl *FD)
Whether we can instantiate FD during constant evaluation.
GCCTypeClass EvaluateBuiltinClassifyType(QualType T, const LangOptions &LangOpts)
EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way as GCC.
void HandleComplexComplexMul(llvm::APFloat A, llvm::APFloat B, llvm::APFloat C, llvm::APFloat D, llvm::APFloat &ResR, llvm::APFloat &ResI)
std::optional< llvm::APFloat > EvalScalarMinMaxFp(const llvm::APFloat &A, const llvm::APFloat &B, std::optional< llvm::APSInt > RoundingMode, bool IsMin)
GCCTypeClass
Values returned by __builtin_classify_type, chosen to match the values produced by GCC's builtin.
uint8_t GFNIMul(uint8_t AByte, uint8_t BByte)
void HandleComplexComplexDiv(llvm::APFloat A, llvm::APFloat B, llvm::APFloat C, llvm::APFloat D, llvm::APFloat &ResR, llvm::APFloat &ResI)
uint8_t GFNIMultiplicativeInverse(uint8_t Byte)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:245
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:3012
unsigned getBuiltinCallee() const
getBuiltinCallee - If this is a call to a builtin, return the builtin ID of the callee.
Definition Expr.cpp:1624
This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
This represents one expression.
Definition Expr.h:114
Represents a function declaration or definition.
Definition Decl.h:2071
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
A (possibly-)qualified type.
Definition TypeBase.h:938
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:714
llvm::APFloat APFloat
Definition Floating.h:27
llvm::APInt APInt
Definition FixedPoint.h:19
Top level wrappers for InstallAPI frontend operations.
DynAllocKind
Kind of source for a dynamic allocation.
Definition APValue.h:69
Expr::ConstantExprKind ConstantExprKind
Definition Expr.h:1087
@ Vector
'vector' clause, allowed on 'loop', Combined, and 'routine' directives.
const FunctionProtoType * T
@ Union
The "union" keyword introduces the elaborated-type-specifier.
Definition TypeBase.h:6022
@ Enum
The "enum" keyword introduces the elaborated-type-specifier.
Definition TypeBase.h:6028
CharUnits GetAlignOfDynamicAlloc(const ASTContext &Ctx, QualType AllocType, DynAllocKind AllocKind)
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
__builtin_bitreverse32 uint32_t __builtin_riscv_aaddu_u32 __builtin_riscv_asubu_u32 __builtin_elementwise_add_sat __builtin_elementwise_sub_sat int32_t uint32_t uint32_t __builtin_riscv_mhacc_i32 __builtin_riscv_mhaccu_u32 __builtin_riscv_mhaccsu_i32 __builtin_riscv_mulq_i32 __builtin_riscv_mqwacc_i64 uint8_t