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"
18#include "clang/Basic/BuiltinTraits.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
37CharUnits GetAlignOfDynamicAlloc(const ASTContext &Ctx, QualType AllocType,
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,
46 Integer = 1,
47 // GCC reserves 2 for character types, but instead classifies them as
48 // integers.
49 Enum = 3,
50 Bool = 4,
51 Pointer = 5,
52 // GCC reserves 6 for references, but appears to never use it (because
53 // expressions never have reference type, presumably).
54 PointerToDataMember = 7,
55 RealFloat = 8,
56 Complex = 9,
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.
63 ClassOrStruct = 12,
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
75GCCTypeClass EvaluateBuiltinClassifyType(QualType T,
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.
103unsigned ConvertBuiltinIDToX86BuiltinID(const ASTContext &Ctx,
104 unsigned BuiltinID);
105unsigned ConvertBuiltinIDToX86BuiltinID(const ASTContext &Ctx,
106 const CallExpr *E);
107
108uint8_t GFNIMultiplicativeInverse(uint8_t Byte);
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
121bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD);
122bool isReadByLvalueToRvalueConversion(QualType T);
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).
126inline bool isForManglingOnly(ConstantExprKind Kind) {
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
142inline bool isTemplateArgument(ConstantExprKind Kind) {
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
168bool FunctionDefinitionCanBeLazilyInstantiated(const FunctionDecl *FD);
169
170#endif
171