1 //===-- include/flang/Runtime/type-code.h -----------------------*- 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 #ifndef FORTRAN_RUNTIME_TYPE_CODE_H_ 10 #define FORTRAN_RUNTIME_TYPE_CODE_H_ 11 12 #include "flang/Common/Fortran-consts.h" 13 #include "flang/Common/optional.h" 14 #include "flang/ISO_Fortran_binding_wrapper.h" 15 #include <utility> 16 17 namespace Fortran::runtime { 18 19 using common::TypeCategory; 20 21 class TypeCode { 22 public: 23 TypeCode() {} 24 explicit RT_API_ATTRS TypeCode(ISO::CFI_type_t t) : raw_{t} {} 25 RT_API_ATTRS TypeCode(TypeCategory, int kind); 26 27 RT_API_ATTRS int raw() const { return raw_; } 28 29 constexpr RT_API_ATTRS bool IsValid() const { 30 return raw_ >= CFI_type_signed_char && raw_ <= CFI_TYPE_LAST; 31 } 32 constexpr RT_API_ATTRS bool IsInteger() const { 33 return raw_ >= CFI_type_signed_char && raw_ <= CFI_type_ptrdiff_t; 34 } 35 constexpr RT_API_ATTRS bool IsReal() const { 36 return raw_ >= CFI_type_half_float && raw_ <= CFI_type_float128; 37 } 38 constexpr RT_API_ATTRS bool IsComplex() const { 39 return raw_ >= CFI_type_half_float_Complex && 40 raw_ <= CFI_type_float128_Complex; 41 } 42 constexpr RT_API_ATTRS bool IsCharacter() const { 43 return raw_ == CFI_type_char || raw_ == CFI_type_char16_t || 44 raw_ == CFI_type_char32_t; 45 } 46 constexpr RT_API_ATTRS bool IsLogical() const { 47 return raw_ == CFI_type_Bool || 48 (raw_ >= CFI_type_int_least8_t && raw_ <= CFI_type_int_least64_t); 49 } 50 constexpr RT_API_ATTRS bool IsDerived() const { 51 return raw_ == CFI_type_struct; 52 } 53 constexpr RT_API_ATTRS bool IsIntrinsic() const { 54 return IsValid() && !IsDerived(); 55 } 56 57 RT_API_ATTRS Fortran::common::optional<std::pair<TypeCategory, int>> 58 GetCategoryAndKind() const; 59 60 RT_API_ATTRS bool operator==(TypeCode that) const { 61 if (raw_ == that.raw_) { // fast path 62 return true; 63 } else { 64 // Multiple raw CFI_type_... codes can represent the same Fortran 65 // type category + kind type parameter, e.g. CFI_type_int and 66 // CFI_type_int32_t. 67 auto thisCK{GetCategoryAndKind()}; 68 auto thatCK{that.GetCategoryAndKind()}; 69 return thisCK && thatCK && *thisCK == *thatCK; 70 } 71 } 72 RT_API_ATTRS bool operator!=(TypeCode that) const { return !(*this == that); } 73 74 private: 75 ISO::CFI_type_t raw_{CFI_type_other}; 76 }; 77 } // namespace Fortran::runtime 78 #endif // FORTRAN_RUNTIME_TYPE_CODE_H_ 79