clang 24.0.0git
Primitives.h
Go to the documentation of this file.
1//===------ Primitives.h - Types for the constexpr VM -----------*- 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// Utilities and helper functions for all primitive types:
10// - Integral
11// - Floating
12// - Boolean
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_CLANG_AST_INTERP_PRIMITIVES_H
17#define LLVM_CLANG_AST_INTERP_PRIMITIVES_H
18
20
21namespace clang {
22namespace interp {
23
24enum class IntegralKind : uint8_t {
25 /// Just a number, nothing else.
26 Number = 0,
27 /// A pointer to a ValueDecl.
29 /// A pointer to an Expr.
31 /// A pointer to an interp::Block.
33 /// A pointer to a AddrLabelExpr.
35 /// A pointer to a FunctionDecl.
37 /// Difference between two AddrLabelExpr.
39};
40
41/// Helper to compare two comparable types.
42template <typename T> ComparisonCategoryResult Compare(const T &X, const T &Y) {
43 if (X < Y)
45 if (X > Y)
48}
49
50template <typename T> inline bool CheckAddUB(T A, T B, T &R) {
51 if constexpr (std::is_signed_v<T>) {
52 return llvm::AddOverflow<T>(A, B, R);
53 } else {
54 R = A + B;
55 return false;
56 }
57}
58
59template <typename T> inline bool CheckSubUB(T A, T B, T &R) {
60 if constexpr (std::is_signed_v<T>) {
61 return llvm::SubOverflow<T>(A, B, R);
62 } else {
63 R = A - B;
64 return false;
65 }
66}
67
68template <typename T> inline bool CheckMulUB(T A, T B, T &R) {
69 if constexpr (std::is_signed_v<T>) {
70 return llvm::MulOverflow<T>(A, B, R);
71 } else if constexpr (sizeof(T) < sizeof(int)) {
72 // Silly integer promotion rules will convert both A and B to int,
73 // even it T is unsigned. Prevent that by manually casting to uint first.
74 R = static_cast<T>(static_cast<unsigned>(A) * static_cast<unsigned>(B));
75 return false;
76 } else {
77 R = A * B;
78 return false;
79 }
80}
81
82} // namespace interp
83} // namespace clang
84
85#endif
#define X(type, name)
Definition Value.h:97
bool CheckMulUB(T A, T B, T &R)
Definition Primitives.h:68
@ BlockAddress
A pointer to an interp::Block.
Definition Primitives.h:32
@ AddrLabelDiff
Difference between two AddrLabelExpr.
Definition Primitives.h:38
@ Number
Just a number, nothing else.
Definition Primitives.h:26
@ ExprAddress
A pointer to an Expr.
Definition Primitives.h:30
@ Address
A pointer to a ValueDecl.
Definition Primitives.h:28
@ LabelAddress
A pointer to a AddrLabelExpr.
Definition Primitives.h:34
@ FunctionAddress
A pointer to a FunctionDecl.
Definition Primitives.h:36
ComparisonCategoryResult Compare(const T &X, const T &Y)
Helper to compare two comparable types.
Definition Primitives.h:42
bool CheckSubUB(T A, T B, T &R)
Definition Primitives.h:59
bool CheckAddUB(T A, T B, T &R)
Definition Primitives.h:50
Top level wrappers for InstallAPI frontend operations.
ComparisonCategoryResult
An enumeration representing the possible results of a three-way comparison.
const FunctionProtoType * T
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 uint8_t