Union

A union: several members sharing one storage location.

Declaration

Syntax

Union(symbol_table symtab, identifier name, identifier* dependencies,
    identifier* members, abi abi, access access,
    call_arg* initializers, symbol? parent)

Arguments

Argument

Description

symtab

the symbol table of the union, with one Variable per member.

name

the name of the union.

dependencies

the names of the symbols the definition refers to.

members

the names of the members, in declaration order.

abi

Source, or BindC for a union declared in C.

access

Public or Private.

initializers

the default initializers of the members.

parent

reserved for an extended union; nil today.

Return values

None.

Description

A Union lays every member out at the same address, so its size is the size of its largest member. Fortran has no union type of its own; this node exists to describe a C union reached through bind(c), and to give LPython’s Union a representation.

Reading a member that was not the one last written is not diagnosed by ASR.

Examples

An ASR text document that uses it:

(TranslationUnit
  :symtab (SymbolTable
    :id 0
    :symbols {
      "m" (Module
        :symtab (SymbolTable
          :id 1
          :symbols {
            "word" (Union
              :symtab (SymbolTable
                :id 2
                :symbols {
                  "i" (Variable
                    :parent_symtab 2
                    :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 []
                  )
                  "r" (Variable
                    :parent_symtab 2
                    :name "r"
                    :dependencies []
                    :intent :Local
                    :symbolic_value nil
                    :value nil
                    :storage :Default
                    :type (Real
                      :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 []
                  )
                }
              )
              :name "word"
              :dependencies []
              :members [
                "i"
                "r"
              ]
              :abi :Source
              :access :Public
              :initializers []
              :parent nil
            )
          }
        )
        :name "m"
        :parent_module nil
        :dependencies []
        :loaded_from_mod false
        :intrinsic false
        :has_submodules false
      )
      "main" (Program
        :symtab (SymbolTable
          :id 3
          :symbols {}
        )
        :name "main"
        :dependencies []
        :body []
      )
    }
  )
  :items []
)

See Also

UnionType, UnionConstructor, UnionInstanceMember, Struct