clang 24.0.0git
OptionalUnsigned.h
Go to the documentation of this file.
1//===- OptionalUnsigned.h - simple optional index-----*- 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/// \file
10/// Defines clang::OptionalUnsigned.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_BASIC_OPTIONAL_UNSIGNED_H
15#define LLVM_CLANG_BASIC_OPTIONAL_UNSIGNED_H
16
17#include <cassert>
18#include <llvm/ADT/STLForwardCompat.h>
19#include <optional>
20
21namespace clang {
22
23template <class T> struct OptionalUnsigned {
25 typename std::conditional_t<std::is_enum_v<T>, std::underlying_type<T>,
26 llvm::type_identity<T>>::type;
27 static_assert(std::is_unsigned_v<underlying_type>);
28
29 constexpr OptionalUnsigned(std::nullopt_t) : Rep(0) {}
30 OptionalUnsigned(T Val) : Rep(static_cast<underlying_type>(Val) + 1) {
31 assert(has_value());
32 }
33
34 template <class U, std::enable_if_t<std::is_signed_v<U>, bool> = false>
36
37 constexpr static OptionalUnsigned
39 return {std::nullopt, Rep};
40 }
41 constexpr underlying_type toInternalRepresentation() const { return Rep; }
42
43 constexpr bool has_value() const { return Rep != 0; }
44
45 explicit constexpr operator bool() const { return has_value(); }
46 T operator*() const {
47 assert(has_value());
48 return static_cast<T>(Rep - 1);
49 }
50
51 T value_or(T Def) const { return has_value() ? operator*() : Def; }
52
53 friend constexpr bool operator==(OptionalUnsigned LHS, OptionalUnsigned RHS) {
54 return LHS && RHS ? *LHS == *RHS : bool(LHS) == bool(RHS);
55 }
56 friend constexpr bool operator!=(OptionalUnsigned LHS, OptionalUnsigned RHS) {
57 return !(LHS == RHS);
58 }
59
60 friend constexpr bool operator<(OptionalUnsigned LHS, OptionalUnsigned RHS) {
61 return LHS != RHS && (!LHS || (RHS && *LHS < *RHS));
62 }
63 friend constexpr bool operator<=(OptionalUnsigned LHS, OptionalUnsigned RHS) {
64 return LHS == RHS || LHS < RHS;
65 }
66 friend constexpr bool operator>=(OptionalUnsigned LHS, OptionalUnsigned RHS) {
67 return !(LHS < RHS);
68 }
69 friend constexpr bool operator>(OptionalUnsigned LHS, OptionalUnsigned RHS) {
70 return !(LHS <= RHS);
71 }
72
73private:
74 constexpr OptionalUnsigned(std::nullopt_t, underlying_type Rep) : Rep(Rep) {};
75
77};
78
80
81} // namespace clang
82
83#endif // LLVM_CLANG_BASIC_OPTIONAL_UNSIGNED_H
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
The JSON file list parser is used to communicate input to InstallAPI.
OptionalUnsigned< unsigned > UnsignedOrNone
const FunctionProtoType * T
friend constexpr bool operator>(OptionalUnsigned LHS, OptionalUnsigned RHS)
constexpr bool has_value() const
constexpr underlying_type toInternalRepresentation() const
friend constexpr bool operator!=(OptionalUnsigned LHS, OptionalUnsigned RHS)
static constexpr OptionalUnsigned fromInternalRepresentation(underlying_type Rep)
friend constexpr bool operator==(OptionalUnsigned LHS, OptionalUnsigned RHS)
typename std::conditional_t< std::is_enum_v< T >, std::underlying_type< T >, llvm::type_identity< T > >::type underlying_type
friend constexpr bool operator<(OptionalUnsigned LHS, OptionalUnsigned RHS)
friend constexpr bool operator>=(OptionalUnsigned LHS, OptionalUnsigned RHS)
constexpr OptionalUnsigned(std::nullopt_t)
friend constexpr bool operator<=(OptionalUnsigned LHS, OptionalUnsigned RHS)