5.6.9.2 Simple Data Operators
Note
This section describes the implementation of MS-VBAL §5.6.9.2 Simple Data Operators.
Runtime Semantics
A simple data operator - every operator but Is - "first evaluates its operands as simple data values": an operand that
is an object, or a Variant that holds one, is what its default member returns (MS-VBAL §5.6.2.2), before anything
else. o + 1.5 of an object whose default member returns 42 is 43.5, as in MS-VBA. An operand that has no value is
the operation's error: Nothing is 91, an object with no default member 438, one whose default member takes an
argument 450, and an object a default member returned that has no value 13 (MS-VBAL §5.6.2.3). An operand an
analysis assumes is not read.
The evaluation pipeline of all operators then follows a fixed sequence of three steps:
- Effective type. The effective type of the operation is determined, based on the declared type of its operands.
- Validation. All non-null operands (every operand that is not a VBNullValue) are let-coerced to the determined effective type of the operation (RD-VBAL §5.5.1.2 Runtime semantics).
- Evaluation. A templated method evaluates a result from the validated operands.
The sequence may be aborted at any point to return an error result. An error result encapsulates VBRuntimeErrorInfo error metadata.
Operand validation
The arithmetic, relational and concatenation (&) operators validate their operands through
OperatorRuntimeSemantics.LetCoerceNonNullOperand.
LetCoerceNonNullOperand has a TypeInfo-equality short-circuit: an operand whose TypeInfo equals the destination
type is not coerced. The short-circuit does not apply to a
VBVariantValue operand: a Variant operand is always
let-coerced, even when its TypeInfo equals the destination type.
A VBVariantValue's TypeInfo mirrors its wrapped value's; see
RD-VBAL §5.5.1.2.12 Let-coercion to Variant.
When an operand is read
MS-VBAL §5.6.9.2 says an operator evaluates its operands. It does not say when an operand that is a variable takes its value if a call in the same expression changes that variable. MS-VBA decides by the operand's type, and RD-VBA does what MS-VBA does:
- An operand of type
Byte,Boolean,Integer,Long,LongLongorCurrencyis read where it is written: before a call to its right, after a call to its left. This holds for a variable, a field of a record and an element of an array. - An operand of type
Single,Double,Date,StringorVariantis read when the operation is computed, after the calls on both sides of it. This holds for a variable and an element of an array, whose index is evaluated then too:v(i) + Poke(v, i), wherePokeadds 1 tov(k)and then tok, isv(2)for aDoublearray, andv(1)as it was for aLongone.
In these statements, a is 1 and Bump adds 1 to the variable it is given by reference and returns the new value.
These are the values measured in MS-VBA:
| Statement | Type of a |
Result |
|---|---|---|
x = a + Bump(a) |
Byte, Integer, Long, LongLong, Currency |
x is 3 |
x = a + Bump(a) |
Boolean, starting False |
x is -1 |
x = a + Bump(a) |
Single, Double, Date, Variant |
x is 4 |
x = Bump(a) + a |
Long |
x is 4 |
x = Bump(a) + a + Bump(a) |
Long |
x is 7 |
x = (a) + Bump(a) |
Long |
x is 3 |
x = (a) + Bump(a) |
Double |
x is 4 |
If a < Bump(a) Then |
Long |
the condition is True |
If a < Bump(a) Then |
Double |
the condition is False |
A String reads the same way: s & Ap(s), where s is "a" and Ap appends "b" to it, is "abab".
Note
Not implemented. MS-VBA computes some operations of a Double or String operand after the call:
a + 0 + Bump(a) of a Double is 4, -a + Bump(a) is 0 and Bump(a) + a + Bump(a) is 8. RD-VBA computes
the operation left of the last call before that call. s & "" & Ap(s) is "aab" in both. RD-VBA reads a Double
field of a record where it is written.
Computation in the effective type
The evaluation step (step 3) computes an operator's result in the operation's effective type, in that type's own
representation, and never through a Double intermediate.
| Effective type | Computed in |
|---|---|
Long |
The Long representation, a 32-bit int. |
Currency, Decimal |
decimal |
Single |
float |
See RD-VBAL §5.6.9.3 Arithmetic Operators for checked arithmetic and
the ^ operator, RD-VBAL §5.6.9.5 Relational Operators for
comparisons, and RD-VBAL §5.6.9.8 Logical Operators for bitwise
computation.
Implementation
- The node parameter of
OperatorRuntimeSemantics<TContext,TFlags>is typed ExpressionNode. - Nothing in the operator runtime-semantics hierarchy reads
.Left,.Rightor.Tokenoff the node parameter. It reads the node's identity (.Identity) and its source location, to attribute the operation and its errors to the node. Beyond those, it only tests the node's type: to flag which operand position (unary, binary left or binary right) an operand holds in the semantic analysis, and to name the node type in an internal-error message. - Only the token-dispatch layer,
OperatorRuntimeSemanticsProvider, reads.Tokenoff the operator node.
The operator strategies do not require a binary or unary operator node (VBBinaryOperatorExpressionNode, VBUnaryOperatorExpressionNode) as their node parameter. Requiring one would force the fabrication of synthetic nodes, which RD-VBA rules out, as it does for let-coercion (RD-VBAL §5.5.1.2 Runtime semantics).
See RD-VBAL §5.4.2.10 Select Case Statement for a caller that invokes the operator strategies directly, with its own expression node.