Cast

Converts a value to another type.

Declaration

Syntax

Cast(expr arg, cast_kind kind, ttype type, expr? value, expr? dest)

Arguments

Argument

Description

arg

the value being converted.

kind

which conversion this is; see cast_kind.

type

the type of the result.

value

the compile time value of the expression, when the frontend could fold it; nil otherwise.

dest

the destination the converted value is written into, when the conversion needs one; nil otherwise.

Return values

The value of the expression.

Description

Cast changes the bits of arg, unlike ArrayPhysicalCast and StringPhysicalCast, which only change how the same bits are described.

Every implicit conversion Fortran performs is an explicit Cast in ASR. The frontend inserts it, so a backend never has to decide whether an operand needs converting: it lowers what it is given. If a backend appears to need a conversion of its own, the bug is upstream in the semantics.

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_kind, ArrayPhysicalCast, StringPhysicalCast