| 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 | |
| 19 | #include "clang/AST/ComparisonCategories.h" |
| 20 | |
| 21 | namespace clang { |
| 22 | namespace interp { |
| 23 | |
| 24 | enum class IntegralKind : uint8_t { |
| 25 | /// Just a number, nothing else. |
| 26 | Number = 0, |
| 27 | /// A pointer to a ValueDecl. |
| 28 | Address, |
| 29 | /// A pointer to an interp::Block. |
| 30 | BlockAddress, |
| 31 | /// A pointer to a AddrLabelExpr. |
| 32 | LabelAddress, |
| 33 | /// A pointer to a FunctionDecl. |
| 34 | FunctionAddress, |
| 35 | /// Difference between two AddrLabelExpr. |
| 36 | AddrLabelDiff |
| 37 | }; |
| 38 | |
| 39 | /// Helper to compare two comparable types. |
| 40 | template <typename T> ComparisonCategoryResult Compare(const T &X, const T &Y) { |
| 41 | if (X < Y) |
| 42 | return ComparisonCategoryResult::Less; |
| 43 | if (X > Y) |
| 44 | return ComparisonCategoryResult::Greater; |
| 45 | return ComparisonCategoryResult::Equal; |
| 46 | } |
| 47 | |
| 48 | template <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 | |
| 57 | template <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 | |
| 66 | template <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 |
| 84 | |