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