1 //===-- 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.h"
13 #include "flang/ISO_Fortran_binding.h"
14 #include <optional>
15 #include <utility>
16 
17 namespace Fortran::runtime {
18 
19 using common::TypeCategory;
20 
21 class TypeCode {
22 public:
TypeCode()23   TypeCode() {}
TypeCode(ISO::CFI_type_t t)24   explicit TypeCode(ISO::CFI_type_t t) : raw_{t} {}
25   TypeCode(TypeCategory, int kind);
26 
raw()27   int raw() const { return raw_; }
28 
IsValid()29   constexpr bool IsValid() const {
30     return raw_ >= CFI_type_signed_char && raw_ <= CFI_TYPE_LAST;
31   }
IsInteger()32   constexpr bool IsInteger() const {
33     return raw_ >= CFI_type_signed_char && raw_ <= CFI_type_ptrdiff_t;
34   }
IsReal()35   constexpr bool IsReal() const {
36     return raw_ >= CFI_type_float && raw_ <= CFI_type_long_double;
37   }
IsComplex()38   constexpr bool IsComplex() const {
39     return raw_ >= CFI_type_float_Complex &&
40         raw_ <= CFI_type_long_double_Complex;
41   }
IsCharacter()42   constexpr bool IsCharacter() const {
43     return raw_ == CFI_type_char || raw_ == CFI_type_char16_t ||
44         raw_ == CFI_type_char32_t;
45   }
IsLogical()46   constexpr bool IsLogical() const {
47     return raw_ == CFI_type_Bool ||
48         (raw_ >= CFI_type_int_fast8_t && raw_ <= CFI_type_int_fast64_t);
49   }
IsDerived()50   constexpr bool IsDerived() const { return raw_ == CFI_type_struct; }
IsIntrinsic()51   constexpr bool IsIntrinsic() const { return IsValid() && !IsDerived(); }
52 
53   std::optional<std::pair<TypeCategory, int>> GetCategoryAndKind() const;
54 
55 private:
56   ISO::CFI_type_t raw_{CFI_type_other};
57 };
58 } // namespace Fortran::runtime
59 #endif // FORTRAN_RUNTIME_TYPE_CODE_H_
60