An expression may reference any field within visible scope. A field is referenced by its name, which is case sensitive. Name lookup continues from child scope to parent scope until the match is found. If the field is not found, constants and enumeration values are searched. If no match is found in constants and enumeration values, an error is generated.
The following example illustrates this:
enum
{
EnumerationValue =0x20,
};
const ConstantValue = 0x30;
struct A
{
int a;
int Array1[a]; // references a in this structure
int Array2[ConstantValue]; // references ConstantValue in global scope
int Array3[EnumerationValue]; // references EnumerationValue in global scope
struct
{
int Array4[a]; // references a in parent scope
} contained;
};
Pointed to types are allowed to reference fields in pointer field's scope. It is their direct enclosing scope. For example:
struct PointedType; // forward declaration
struct PointerType
{
int a;
int b as PointedType *; // b is converted to a pointer to PointedType structure
int c;
};
struct PointedType
{
int Array[a + c]; // references a and c in PointerType (note c is declared AFTER b, the reference is still valid)
};
this
Pseudo-FieldThe special field this is defined for each bound user-defined type and evaluates to the absolute offset of this bound structure.
struct B
{
// …
};
struct A
{
int OffsetToB as B *(this); // B will automatically be bound to &A + OffsetToB
};
When passed to the built-in ref
function, the result is a reference
to the current object.
array_index
Built-In VariableThis built-in variable evaluates to the current array element index, if the structure is being used inside an array, otherwise it is evaluated to -1.
struct B;
struct A
{
B b[10];
};
struct B
{
int a;
if (array_index == 3) // array_index evaluates to a current array index
int b;
};
current_offset
Built-In VariableThis built-in variable evaluates to the address the next field will be bound to.
#pragma script("get_doc_size.js") // we use a GetDocumentSize() function
struct A
{
int b[10];
// pad this structure to the end of the file
char padding[GetDocumentSize() - current_offset];
};