StringFormat

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

Declaration

Sintaxe

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

Argumentos

Argument

Descrição

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.

Valores de retorno

The value of the expression.

Descrição

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.

Fortran formats support the Fortran 2023 LZP, LZS, and LZ controls: print, suppress, or restore processor-defined optional leading zeros in F, E, D, and real G output. The mode lasts for the current formatting operation, including format reversion; it does not affect EN, ES, EX, or input conversion. The related OPEN/WRITE LEADING_ZERO= specifiers are not implemented by these FORMAT controls. Embedded blanks in these controls are insignificant, including on input, where no target is consumed. Leading-zero selection does not change the significant digits or rounding of real G output, in either fixed or exponential form, including RC compatible rounding of halfway values away from zero. The regression integration_tests/format_leading_zero.f90 currently runs only with LLVM: GFortran 16 rejects these valid Fortran 2023 descriptors, so independent reference validation remains unavailable.

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.

Exemplos

(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_asynchronous 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
)

Veja Também

string_format_kind, Print, FileWrite, StringConstant