DWARF Standard


141117.1 Adrian Prantl Arbitrary expressions as formal parameter default values Enhancement Accepted with modifications Adrian Prantl

Section 4, pg 
Background for discussion

Languages such as C++ allow for arbitrary expressions to be provided
as default arguments. In particular, C++11 allows any initializer
expression as a default argument as the following example from the
language specification illustrates::

    int a = 1;
    int f(int);
    int g(int x = f(a));
    void h() {
      a = 2;
      int a = 3;
      g(); // g(f(::a)) --> g(f(2))

    from (8.3.6) http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf

It would be useful to encode default arguments in DWARF because
developers may want to evaluate expressions in the context of the
program. Currently, it is possible to encode constant-value default
arguments in DWARF. While at first it may seem tempting to try and
encode default arguments using DWARF expressions, this is actually not
possible, because DWARF expressions cannot call into user code; i.e.,
it would be impossible to express the above default argument of
::f(::a) as a DWARF expression.

We therefore suggest encoding complex default arguments as strings
containing a fragment in the programming language specified by the
compile unit. A consumer that is sophisticated enough to make use of a
default expression should also be able to evaluate a source language

Section 4.1 Data Object Entries

A DW_AT_default_value attribute for a formal parameter entry. The
value of this attribute may be a constant, or a reference to the
debugging information entry for a variable, or a reference to a
debugging information entry containing a DWARF procedure.

, or a string containing a source language fragment.
If the attribute form is of class string, that string is interpreted
as an expression in the source language defined by the compilation
unit's DW_AT_language attribute that is to be evaluated according to
the rules defined by the source language.

Table 7.5: Attribute encodings
DW_AT_default_value    0x1e   constant, reference, flag, string

Appendix D

[add new section]

Default value examples

The default expression for parameter "x" in the C++ function
declaration in Figure [1] can be described in DWARF as illustrated in
Figure [2].

Figure 1.

    void g (int x = 13;
            int y = f());
Figure 2.

        DW_AT_name ("g")

           DW_AT_name ("x")
           DW_AT_type (reference to type "int")
           DW_AT_default_value@DW_FORM_sdata (13)

           DW_AT_name ("y")
           DW_AT_type (reference to type "int")
           DW_AT_default_value@DW_FORM_string ("f()")

Notes for Figure 2:
 1. This figure explicitly shows the form used by certain attributes
     (indicated by a trailing @DW_FORM_xxx) when it is critical, while
     the form is most often left implicit.
 2. The string value for 'y' is three characters in length and
     does not include any quotes. (The quotes are an artifact of the
     presumed dumper tool that created this interpretation.)
 3. The default value for x could also be encoded as
     DW_AT_default_value@DW_FORM_string("13"); however. this is generally
     a less convenient form for consumers to process.

A string FORM in DW_AT_default_value always represents a source
code fragment, even in languages that have a native string type. For
example, the default string parameter of the Ada function in Figure
[3] is encoded in DWARF as a string containing the Ada string literal
including the quotation marks (see Figure [4]).

Figure 3.

    procedure s (x : string := "abc";
                 y : string := "abcd"+10) is
    end s;
Figure 4.

[NOTE TO THE EDITOR: Ideally we can use two different kinds of quotes
here to distinguish the literal double-quotes in the default value
from the ones that dwarfdump adds.]
[NOTE FROM EDITOR: Agreed. LaTeX can surely help here...]

       DW_AT_name (``s'')
          DW_AT_name (``x'')
          DW_AT_type (reference to type ``string'')
          DW_AT_default_value@DW_FORM_data4 (0x61626364)    \\ Big-endian
          DW_AT_name (``y'')
          DW_AT_type (reference to type ``string'')
          DW_AT_default_value@DW_FORM_string (``"abcd"+10'')

2014-11-19 -- Deferred to version 6.
2022-09-19 -- Updated examples.
2022-10-21 -- Updated examples.
2022-10-21 -- Accepted with modifications (non-normative text TBD)

All logos and trademarks in this site are property of their respective owner.
The comments are property of their posters, all the rest © 2007-2022 by DWARF Standards Committee.