cast_kind

Which conversion a Cast performs.

Declaration

Syntax

cast_kind
    = RealToInteger
    | IntegerToReal
    | LogicalToReal
    | RealToReal
    | IntegerToInteger
    | RealToComplex
    | IntegerToComplex
    | IntegerToLogical
    | RealToLogical
    | StringToLogical
    | StringToInteger
    | StringToList
    | ComplexToLogical
    | ComplexToComplex
    | ComplexToReal
    | ComplexToInteger
    | LogicalToInteger
    | LogicalToLogical
    | RealToString
    | IntegerToString
    | LogicalToString
    | StringToString
    | UnsignedIntegerToInteger
    | UnsignedIntegerToUnsignedInteger
    | UnsignedIntegerToReal
    | UnsignedIntegerToLogical
    | IntegerToUnsignedInteger
    | RealToUnsignedInteger
    | CPtrToUnsignedInteger
    | UnsignedIntegerToCPtr
    | IntegerToSymbolicExpression
    | ListToArray
    | StringToArray
    | PointerToInteger
    | ClassToStruct
    | ClassToClass
    | ClassToIntrinsic

Values

Value

Meaning

RealToInteger

truncates towards zero.

IntegerToReal

exact for values the real type can represent.

LogicalToReal

false becomes 0.0, true becomes 1.0.

RealToReal

changes the real kind, rounding when it narrows.

IntegerToInteger

changes the integer kind, wrapping when it narrows.

RealToComplex

the value becomes the real part; the imaginary part is zero.

IntegerToComplex

the value becomes the real part.

IntegerToLogical

zero becomes false, anything else true.

RealToLogical

zero becomes false, anything else true.

StringToLogical

an LPython conversion: the empty string is false.

StringToInteger

parses the string as an integer.

StringToList

an LPython conversion to a list of characters.

ComplexToLogical

zero becomes false.

ComplexToComplex

changes the complex kind.

ComplexToReal

takes the real part.

ComplexToInteger

takes the real part and truncates it.

LogicalToInteger

false becomes 0, true becomes 1.

LogicalToLogical

changes the logical kind.

RealToString

formats the value.

IntegerToString

formats the value.

LogicalToString

formats the value.

StringToString

changes the string kind.

UnsignedIntegerToInteger

reinterprets the value as signed.

UnsignedIntegerToUnsignedInteger

changes the unsigned kind.

UnsignedIntegerToReal

exact for values the real type can represent.

UnsignedIntegerToLogical

zero becomes false.

IntegerToUnsignedInteger

reinterprets the value as unsigned.

RealToUnsignedInteger

truncates towards zero.

CPtrToUnsignedInteger

the address as a number.

UnsignedIntegerToCPtr

a number as an address.

IntegerToSymbolicExpression

wraps an integer as a symbolic expression.

ListToArray

an LPython conversion from a list to an array.

StringToArray

a string as an array of characters.

PointerToInteger

the address as a number.

ClassToStruct

narrows a polymorphic value to a derived type.

ClassToClass

changes which class a polymorphic value is described as.

ClassToIntrinsic

narrows a polymorphic value to an intrinsic type.

Return values

None. An enumeration value is not evaluated.

Description

Every implicit conversion is explicit in ASR, and this enumeration says which conversion a Cast performs. The kind and the type of the Cast agree: the kind names the pair of types, and the type is the result.

Conversions that do not change the bits belong to ArrayPhysicalCast and StringPhysicalCast instead.

Examples

(Cast
  :arg (Var
    :v (SymbolRef 1 "i")
  )
  :kind :IntegerToReal
  :type (Real
    :kind 8
  )
  :value nil
  :dest nil
)

It comes from this complete ASR text document:

(TranslationUnit
  :symtab (SymbolTable
    :id 0
    :symbols {
      "main" (Program
        :symtab (SymbolTable
          :id 1
          :symbols {
            "i" (Variable
              :parent_symtab 1
              :name "i"
              :dependencies []
              :intent :Local
              :symbolic_value nil
              :value nil
              :storage :Default
              :type (Integer
                :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 []
            )
          }
        )
        :name "main"
        :dependencies []
        :body [
          (Assignment
            :target (Var
              :v (SymbolRef 1 "x")
            )
            :value (Cast
              :arg (Var
                :v (SymbolRef 1 "i")
              )
              :kind :IntegerToReal
              :type (Real
                :kind 8
              )
              :value nil
              :dest nil
            )
            :overloaded nil
            :realloc_lhs false
            :move_allocation false
          )
          (Assignment
            :target (Var
              :v (SymbolRef 1 "i")
            )
            :value (Cast
              :arg (Var
                :v (SymbolRef 1 "x")
              )
              :kind :RealToInteger
              :type (Integer
                :kind 4
              )
              :value nil
              :dest nil
            )
            :overloaded nil
            :realloc_lhs false
            :move_allocation false
          )
        ]
      )
    }
  )
  :items []
)

See Also

Cast, Integer, Real, ArrayPhysicalCast, StringPhysicalCast