Skip to content

Commit 15bc10a

Browse files
committed
Add docs for union field resolution; Resolves #6578
1 parent d4f7a4b commit 15bc10a

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

docs/guide/types/type.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,40 @@ When used on an integer, all matching enumeration members will be shown.
3535

3636
However in instances where the hotkey is used on other variables, the display will only be used to apply the enum type to the selection and does not allow editing.
3737

38+
## Union Field Resolution
39+
40+
When a type contains a union, any number of fields can resolve to the same offset. To account for this, the "Field Resolution" menu allows you to select which field to use at different instructions and propagate that type information through the rest of the function and to callee function parameter types. Consider the following type definition:
41+
42+
```C
43+
typedef enum
44+
{
45+
TYPE_INT,
46+
TYPE_BOOL,
47+
TYPE_STRING
48+
} ValueType;
49+
50+
typedef struct
51+
{
52+
ValueType type;
53+
union
54+
{
55+
struct { int i; } int_data;
56+
struct { unsigned char b; } bool_data;
57+
struct { char* str; } str_data;
58+
} data;
59+
} Value;
60+
```
61+
62+
This is a tagged union; there's an enum defining the different variants of the union, and then the actual union variants/members. The union should take up as much space as the largest variant, so in this case it will usually be 8 bytes (depends on the target machine's pointer width), but only one variant can occupy those bytes at a time.
63+
64+
When you apply a union type to a variable, you can use the "Field Resolution" option in the right-click menu to change which variant is used for a given instruction.
65+
66+
![Field Resolution Menu](../../img/select-union-field-resolution.png "Field Resolution Menu")
67+
68+
In the above screenshot the first if-block uses the `TYPE_INT` variant, and the second if-block uses the `TYPE_BOOL` variant. The final return statement is incorrectly showing the `int_data` type (it should be showing the `char*` one), and the "Field Resolution" option in the right-click menu is showing all possible fields that instance can resolve to, including options we've determined should be invalid. Simply select the correct option from the menu, and the output will be updated.
69+
70+
You can also access these options through the [command palette](../index.md#command-palette) by searching for "Field Resolution."
71+
3872
## Smart Structures Workflow
3973

4074
![Auto Create Members](../../img/auto-create-members.png "Automatically Creating Struct Members")
608 KB
Loading

0 commit comments

Comments
 (0)