clang 23.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 interp::Block.
31 /// A pointer to a AddrLabelExpr.
33 /// A pointer to a FunctionDecl.
35 /// Difference between two AddrLabelExpr.
37};
38
39/// Helper to compare two comparable types.
40template <typename T> ComparisonCategoryResult Compare(const T &X, const T &Y) {
41 if (X < Y)
43 if (X > Y)
46}
47
48template <typename T> inline bool CheckAddUB(T A, T B, T &R) {
49 if constexpr (std::is_signed_v<T>) {
50 return llvm::AddOverflow<T>(A, B, R);
51 } else {
52 R = A + B;
53 return false;
54 }
55}
56
57template <typename T> inline bool CheckSubUB(T A, T B, T &R) {
58 if constexpr (std::is_signed_v<T>) {
59 return llvm::SubOverflow<T>(A, B, R);
60 } else {
61 R = A - B;
62 return false;
63 }
64}
65
66template <typename T> inline bool CheckMulUB(T A, T B, T &R) {
67 if constexpr (std::is_signed_v<T>) {
68 return llvm::MulOverflow<T>(A, B, R);
69 } else if constexpr (sizeof(T) < sizeof(int)) {
70 // Silly integer promotion rules will convert both A and B to int,
71 // even it T is unsigned. Prevent that by manually casting to uint first.
72 R = static_cast<T>(static_cast<unsigned>(A) * static_cast<unsigned>(B));
73 return false;
74 } else {
75 R = A * B;
76 return false;
77 }
78}
79
80} // namespace interp
81} // namespace clang
82
83#endif
#define X(type, name)
Definition Value.h:97
bool CheckMulUB(T A, T B, T &R)
Definition Primitives.h:66
@ BlockAddress
A pointer to an interp::Block.
Definition Primitives.h:30
@ AddrLabelDiff
Difference between two AddrLabelExpr.
Definition Primitives.h:36
@ Number
Just a number, nothing else.
Definition Primitives.h:26
@ Address
A pointer to a ValueDecl.
Definition Primitives.h:28
@ LabelAddress
A pointer to a AddrLabelExpr.
Definition Primitives.h:32
@ FunctionAddress
A pointer to a FunctionDecl.
Definition Primitives.h:34
ComparisonCategoryResult Compare(const T &X, const T &Y)
Helper to compare two comparable types.
Definition Primitives.h:40
bool CheckSubUB(T A, T B, T &R)
Definition Primitives.h:57
bool CheckAddUB(T A, T B, T &R)
Definition Primitives.h:48
The JSON file list parser is used to communicate input to InstallAPI.
ComparisonCategoryResult
An enumeration representing the possible results of a three-way comparison.