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;
35
37 DynAllocKind AllocKind);
38} // namespace clang
39using namespace clang;
40/// Values returned by __builtin_classify_type, chosen to match the values
41/// produced by GCC's builtin.
42enum class GCCTypeClass {
43 None = -1,
44 Void = 0,
46 // GCC reserves 2 for character types, but instead classifies them as
47 // integers.
48 Enum = 3,
49 Bool = 4,
51 // GCC reserves 6 for references, but appears to never use it (because
52 // expressions never have reference type, presumably).
56 // GCC reserves 10 for functions, but does not use it since GCC version 6 due
57 // to decay to pointer. (Prior to version 6 it was only used in C++ mode).
58 // GCC claims to reserve 11 for pointers to member functions, but *actually*
59 // uses 12 for that purpose, same as for a class or struct. Maybe it
60 // internally implements a pointer to member as a struct? Who knows.
61 PointerToMemberFunction = 12, // Not a bug, see above.
63 Union = 13,
64 // GCC reserves 14 for arrays, but does not use it since GCC version 6 due to
65 // decay to pointer. (Prior to version 6 it was only used in C++ mode).
66 // GCC reserves 15 for strings, but actually uses 5 (pointer) for string
67 // literals.
68 // Lang = 16,
69 // OpaqueType = 17,
70 BitInt = 18,
71 Vector = 19
72};
73
75 const LangOptions &LangOpts);
76
77void HandleComplexComplexMul(llvm::APFloat A, llvm::APFloat B, llvm::APFloat C,
78 llvm::APFloat D, llvm::APFloat &ResR,
79 llvm::APFloat &ResI);
80void HandleComplexComplexDiv(llvm::APFloat A, llvm::APFloat B, llvm::APFloat C,
81 llvm::APFloat D, llvm::APFloat &ResR,
82 llvm::APFloat &ResI);
83
84CharUnits GetAlignOfExpr(const ASTContext &Ctx, const Expr *E,
85 UnaryExprOrTypeTrait ExprKind);
86
87/// Convert a builtin ID to the canonical x86 builtin ID the constant evaluators
88/// dispatch on in their x86 target-specific cases.
89///
90/// Target-independent builtins are returned unchanged. An x86 target builtin
91/// (including an auxiliary-target x86 builtin, whose ID is shifted past the
92/// primary target's builtins) is translated to its canonical X86::BI* value.
93/// Any other target's builtin returns 0: the constant evaluators only fold x86
94/// target builtins, and target builtin IDs of different targets overlap (each
95/// numbers from Builtin::FirstTSBuiltin), so an unrelated target's ID must not
96/// be mistaken for an x86 one.
97///
98/// The ID-based overload performs no work beyond a single comparison for
99/// target-independent builtins, so it is suitable for hot paths (e.g. the
100/// bytecode interpreter's builtin dispatch) where re-deriving the ID from the
101/// call expression would be wasteful.
103 unsigned BuiltinID);
105 const CallExpr *E);
106
108uint8_t GFNIMul(uint8_t AByte, uint8_t BByte);
109uint8_t GFNIAffine(uint8_t XByte, const llvm::APInt &AQword,
110 const llvm::APSInt &Imm, bool Inverse = false);
111llvm::APSInt NormalizeRotateAmount(const llvm::APSInt &Value,
112 const llvm::APSInt &Amount);
113
114std::optional<llvm::APFloat>
115EvalScalarMinMaxFp(const llvm::APFloat &A, const llvm::APFloat &B,
116 std::optional<llvm::APSInt> RoundingMode, bool IsMin);
117
118const Expr *ignorePointerCastsAndParens(const Expr *E);
119
122/// Determines whether the given kind of constant expression is only ever
123/// used for name mangling. If so, it's permitted to reference things that we
124/// can't generate code for (in particular, dllimported functions).
126 switch (Kind) {
127 case ConstantExprKind::Normal:
128 case ConstantExprKind::ClassTemplateArgument:
129 case ConstantExprKind::ImmediateInvocation:
130 case ConstantExprKind::Initializer:
131 // Note that non-type template arguments of class type are emitted as
132 // template parameter objects.
133 return false;
134
135 case ConstantExprKind::NonClassTemplateArgument:
136 return true;
137 }
138 llvm_unreachable("unknown ConstantExprKind");
139}
140
142 switch (Kind) {
143 case ConstantExprKind::Normal:
144 case ConstantExprKind::ImmediateInvocation:
145 case ConstantExprKind::Initializer:
146 return false;
147
148 case ConstantExprKind::ClassTemplateArgument:
149 case ConstantExprKind::NonClassTemplateArgument:
150 return true;
151 }
152 llvm_unreachable("unknown ConstantExprKind");
153}
154
155/// Should this call expression be treated as forming an opaque constant?
156inline bool isOpaqueConstantCall(const CallExpr *E) {
157 unsigned Builtin = E->getBuiltinCallee();
158 return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString ||
159 Builtin == Builtin::BI__builtin___NSStringMakeConstantString ||
160 Builtin == Builtin::BI__builtin_ptrauth_sign_constant ||
161 Builtin == Builtin::BI__builtin_function_start);
162}
163
164bool isGlobalLValue(const ValueDecl *D, const Expr *E);
165
166#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)
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:2991
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:113
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:713
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:1066
@ 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 uint8_t