StringFormat

A formatted string built from a format and a list of values.

Declaration

Syntax

StringFormat(expr? fmt, expr* args, string_format_kind kind,
    dtio_binding* dtio_bindings, expr? dtio_unit, ttype type, expr? value)

Arguments

Argument

Описание

fmt

the format, or nil for list-directed formatting.

args

the values to format.

kind

which formatting language the format is written in; see string_format_kind.

dtio_bindings

the defined output procedures of the derived-type items, one per declared type and polymorphism; see dtio_binding. Empty when no item has one.

dtio_unit

the unit passed to a defined output procedure: an IntegerConstant or a Var, or nil for an internal file. Always nil when dtio_bindings is empty.

type

the string type of the result.

value

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

Return values

The value of the expression.

Описание

Formatting is an expression producing a string, not a property of the I/O statements. Print and FileWrite therefore carry a StringFormat rather than a format and an output list of their own, and a backend that can build the string has already implemented both.

kind distinguishes Fortran edit descriptors from C’s printf and from Python’s several formatting languages, so one node serves every frontend.

A derived-type item with a defined output procedure is formatted by calling that procedure when its edit descriptor is reached. dtio_bindings says which procedure each such item calls, and dtio_unit which unit it is given, so the formatting stays one expression.

Examples

(StringFormat
  :fmt nil
  :args [
    (Var
      :v (SymbolRef 1 "n")
    )
  ]
  :kind :FormatFortran
  :dtio_bindings []
  :dtio_unit nil
  :type (String
    :kind 1
    :len nil
    :len_kind :DeferredLength
    :physical_type :DescriptorString
  )
  :value nil
)

It comes from this complete ASR text document:

(TranslationUnit
  :symtab (SymbolTable
    :id 0
    :symbols {
      "main" (Program
        :symtab (SymbolTable
          :id 1
          :symbols {
            "n" (Variable
              :parent_symtab 1
              :name "n"
              :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 []
              :common_block nil
            )
          }
        )
        :name "main"
        :dependencies []
        :body [
          (Print
            :text (StringFormat
              :fmt nil
              :args [
                (Var
                  :v (SymbolRef 1 "n")
                )
              ]
              :kind :FormatFortran
              :dtio_bindings []
              :dtio_unit nil
              :type (String
                :kind 1
                :len nil
                :len_kind :DeferredLength
                :physical_type :DescriptorString
              )
              :value nil
            )
          )
        ]
        :global_init nil
      )
    }
  )
  :items []
  :global_init nil
)

See Also

string_format_kind, Print, FileWrite, StringConstant