Table of Contents

3.0.3 Binding Contexts

MS-VBAL §5.6.4 Expression Binding Contexts breaks down expression binding contexts, used for resolving name lookups, as follows:

Binding context Used by
Default binding context Most expressions.
Type binding context Expressions that expect to reference a type or class name.
Procedure pointer binding context Expressions that expect to return a pointer to a procedure.
Conditional compilation binding context Expressions within conditional compilation statements.

See also RD-VBAL §5.6.4 Expression Binding Contexts.

RDCore Model

🎯 In RDCore, name lookups are an explicit evaluation step involving specific AST nodes, such as SimpleNameExpressionNode (RD-VBAL §5.6.10 Simple Name Expressions).

The binding context is chosen by the node being evaluated, never by a parameter:

Node Binding context Lookup
A SimpleNameExpressionNode Default binding context ISymbolResolver.ResolveValue
An As clause Type binding context ISymbolResolver.ResolveType
The operand of a NewExpressionNode Type binding context ISymbolResolver.ResolveType
The qualifier of a qualified type name Bound as a namespace, not under the type binding context ISymbolResolver.ResolveQualifier

The ISymbolResolver members ResolveValue, ResolveType and ResolveQualifier are described in RD-VBAL §2.3.1.3 Name Resolution.

🎯 Evaluation returns an evaluation result record, RuntimeSemanticsEvaluationResult. The record describes and encapsulates either the evaluation result or runtime error metadata (RD-VBAL §5.0 Semantics).

The RD-VBA type system includes and uses meta-types such as VBTypeDescValue (RD-VBAL §2.4.3 Meta and Advanced Types). Because of this, the binding context can be inferred from the managed type of a provided value.

Warning

A VBTypeDescValue is a data value that represents a data type. The implementation of both static and runtime semantics must be mindful of the possibility of accidentally pattern-matching such a type descriptor.

Qualified Type Names

In a qualified type name A.B, the lookup is positional:

Part Bound as Lookup
B, the last part Like a bare name, in the type binding context. ISymbolResolver.ResolveType
A, the qualifier A namespace: the project, or a procedural or class module. ISymbolResolver.ResolveQualifier

Neither a user-defined type nor an Enum type is a candidate for the qualifier, because neither can contain a type. ResolveType binds only a user-defined type, an Enum type, a class or procedural module, or the project, in that order of precedence; ResolveQualifier is ResolveType without the user-defined types and Enum types (RD-VBAL §2.3.1.3 Name Resolution).

The positional rule holds wherever a type name appears:

Types Named Like a Namespace

The positional rule matters when a module declares a Type named like the project, or like another module. Given a module that declares Type MyProject in a project named MyProject:

Type name Names
MyProject The Type MyProject: the first tier of the type binding context (MS-VBAL §5.6.10 Simple Name Expressions).
MyProject.ClassName The class ClassName in the project.

Read literally, MS-VBAL §5.6.12 Member Access Expressions does not say which binding context the left-hand side of a member access under the type binding context is bound in. Applied literally, the first-match rule of MS-VBAL §5.6.10 would select the Type MyProject, which no member access could then qualify. The positional rule matches the observed behaviour of the VBA and VB6 compilers:

Source VBA compiler (VBE) VB6 compiler
New MyProject.Class, next to a Type MyProject Compiles; names the class. Names the class.
Dim c As MyProject.Class Not verified. Names the class.
Dim u As MyProject Not verified. Finds the Type MyProject.

Only the New MyProject.Class form is verified against the VBA compiler. The As New form follows from the positional rule, and is not verified against a compiler.

Whether a class named by New is creatable is not a name-lookup concern. See RD-VBAL §5.6.8 New Expressions.


⏮️ RD-VBAL §3.0.2 Node Types | ⏭️ RD-VBAL §3.1 Attributes and Directives