Precedence DTrace Rules

The D rules for operator precedence and associativity are described in the following table. These rules are somewhat complex, but are necessary to provide precise compatibility with the ANSI-C operator precedence rules. The table entries are in order from highest precedence to lowest precedence.

Table 2-11 D Operator Precedence and Associativity

Operators Associativity

() [] ->.

left to right

! ~ ++ - + - * &(type) sizeof stringof offsetof xlate

right to left

* / %

left to right

+ -

left to right

<< >>

left to right

< <= > >=

left to right

== !=

left to right

&

left to right

^

left to right

|

left to right

&&

left to right

^^

left to right

||

left to right

?:

right to left

= += -= *= /= %= &= ^= ?= <<= >>=

right to left

,

left to right

The following operators are discussed in subsequent sections:

  • sizeof – Computes the size of an object

  • offsetof – Computes the offset of a type member

  • stringof – Converts the operand to a string

  • xlate – Translates a data type

  • unary & – Computes the address of an object

  • unary * – Dereferences a pointer to an object

  • -> and . – Accesses a member of a structure or union type

The comma (,) operator listed in the table is for compatibility with the ANSI-C comma operator, which can be used to evaluate a set of expressions in left-to-right order and return the value of the right most expression. This operator is provided strictly for compatibility with C and should generally not be used.

The () entry in the table of operator precedence represents a function call; examples of calls to functions such as printf() and trace() are presented in Output Formatting in DTrace. A comma is also used in D to list arguments to functions and to form lists of associative array keys. This comma is not the same as the comma operator and does not guarantee left-to-right evaluation. The D compiler provides no guarantee as to the order of evaluation of arguments to a function or keys to an associative array. You should be careful of using expressions with interacting side-effects, such as the pair of expressions i and i++, in these contexts.

The []entry in the table of operator precedence represents an array or associative array reference. Examples of associative arrays are presented in Associative Arrays. A special kind of associative array called an aggregation is described in DTrace Aggregations. The [] operator can also be used to index into fixed-size C arrays as well, as described in Pointers and Arrays in DTrace.