kinds

The kind member of an intrinsic type.

Declaration

Syntax

Integer(int kind)
UnsignedInteger(int kind)
Real(int kind)
Complex(int kind)
Logical(int kind)

Arguments

Argument

Description

kind

the kind of the type, as a number of bytes.

Return values

None. A type is not evaluated.

Description

The kind is part of the type, so every value in ASR has a width that is known where it appears: there is no integer whose size is implied by context, and an operation between two kinds is an explicit Cast rather than a promotion a backend performs.

LFortran supports these kinds:

Type

Kinds

Default

Integer

1 (i8), 2 (i16), 4 (i32), 8 (i64)

4

UnsignedInteger

1, 2, 4, 8

4

Real

4 (f32), 8 (f64)

4

Complex

4 (c32), 8 (c64)

4

String

1 (a byte)

1

Logical

1, 2, 4

4

A Complex of kind 8 holds two real(8) values: the kind is the kind of each part, not the size of the pair.

The default logical kind is 4, the same as the default integer kind. That follows both languages LFortran serves: in Fortran the „default logical kind has the same storage size as the default integer“, and in Python „Booleans are implemented as a subclass of integers“.

A kind is written in Fortran as a literal suffix, 1.0_dp, or as a type parameter, real(kind=dp). Either way the frontend resolves it to a number before ASR is built, so a kind in ASR is always a plain integer and never an expression.

Examples

A real(8) variable and a real(8) constant:

(RealConstant
  :r 2.5
  :type (Real
    :kind 8
  )
)

The complete translation unit:

(TranslationUnit
  :symtab (SymbolTable
    :id 0
    :symbols {
      "main" (Program
        :symtab (SymbolTable
          :id 1
          :symbols {
            "b" (Variable
              :parent_symtab 1
              :name "b"
              :dependencies []
              :intent :Local
              :symbolic_value nil
              :value nil
              :storage :Default
              :type (Logical
                :kind 4
              )
              :type_declaration nil
              :abi :Source
              :access :Public
              :presence :Required
              :value_attr false
              :target_attr false
              :contiguous_attr false
              :bindc_name nil
              :is_volatile false
              :is_protected false
              :pass_attr :NotMethod
              :self_argument nil
              :codims []
            )
            "x" (Variable
              :parent_symtab 1
              :name "x"
              :dependencies []
              :intent :Local
              :symbolic_value nil
              :value nil
              :storage :Default
              :type (Real
                :kind 8
              )
              :type_declaration nil
              :abi :Source
              :access :Public
              :presence :Required
              :value_attr false
              :target_attr false
              :contiguous_attr false
              :bindc_name nil
              :is_volatile false
              :is_protected false
              :pass_attr :NotMethod
              :self_argument nil
              :codims []
            )
            "y" (Variable
              :parent_symtab 1
              :name "y"
              :dependencies []
              :intent :Local
              :symbolic_value nil
              :value nil
              :storage :Default
              :type (Real
                :kind 8
              )
              :type_declaration nil
              :abi :Source
              :access :Public
              :presence :Required
              :value_attr false
              :target_attr false
              :contiguous_attr false
              :bindc_name nil
              :is_volatile false
              :is_protected false
              :pass_attr :NotMethod
              :self_argument nil
              :codims []
            )
          }
        )
        :name "main"
        :dependencies []
        :body [
          (Assignment
            :target (Var
              :v (SymbolRef 1 "x")
            )
            :value (RealConstant
              :r 2.5
              :type (Real
                :kind 8
              )
            )
            :overloaded nil
            :realloc_lhs false
            :move_allocation false
          )
          (Assignment
            :target (Var
              :v (SymbolRef 1 "y")
            )
            :value (RealBinOp
              :left (Var
                :v (SymbolRef 1 "x")
              )
              :op :Div
              :right (RealConstant
                :r 2.0
                :type (Real
                  :kind 8
                )
              )
              :type (Real
                :kind 8
              )
              :value nil
            )
            :overloaded nil
            :realloc_lhs false
            :move_allocation false
          )
          (Assignment
            :target (Var
              :v (SymbolRef 1 "y")
            )
            :value (RealUnaryMinus
              :arg (Var
                :v (SymbolRef 1 "x")
              )
              :type (Real
                :kind 8
              )
              :value nil
            )
            :overloaded nil
            :realloc_lhs false
            :move_allocation false
          )
          (Assignment
            :target (Var
              :v (SymbolRef 1 "y")
            )
            :value (RealSqrt
              :arg (Var
                :v (SymbolRef 1 "x")
              )
              :type (Real
                :kind 8
              )
              :value nil
            )
            :overloaded nil
            :realloc_lhs false
            :move_allocation false
          )
          (Assignment
            :target (Var
              :v (SymbolRef 1 "y")
            )
            :value (RealCopySign
              :target (Var
                :v (SymbolRef 1 "x")
              )
              :source (RealConstant
                :r -1.0
                :type (Real
                  :kind 8
                )
              )
              :type (Real
                :kind 8
              )
              :value nil
            )
            :overloaded nil
            :realloc_lhs false
            :move_allocation false
          )
          (Assignment
            :target (Var
              :v (SymbolRef 1 "b")
            )
            :value (RealCompare
              :left (Var
                :v (SymbolRef 1 "x")
              )
              :op :GtE
              :right (Var
                :v (SymbolRef 1 "y")
              )
              :type (Logical
                :kind 4
              )
              :value nil
            )
            :overloaded nil
            :realloc_lhs false
            :move_allocation false
          )
        ]
      )
    }
  )
  :items []
)

See Also

ttype, Integer, Real, Cast, TypeInquiry