poke-devel
[Top][All Lists]

## Casting integral structs, and coercions

 From: Jose E. Marchesi Subject: Casting integral structs, and coercions Date: Mon, 27 Jul 2020 13:21:45 +0200 User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

```Hi people!

In the last pokology I mentioned I was working on adding support for
casting integral structs to integers.  That support is ready now in
master.

Let's use the following type as an example:

deftype Elf64_RelInfo =
struct Elf64_Xword
{
uint<32> r_sym;
uint<32> r_type;
};

There are two ways to handle integral structs as integers:

1. Using explicit casts.  For example:

(poke) defvar info = Elf64_RelInfo { r_sym = 3 }
(poke) info
Elf64_RelInfo {
r_sym=0x3U,
r_type=0x0U
}
(poke) info as uint<64>
0x300000000UL

2. Integral structs are automatically coerced to an integer in contexts
where integer values are expected.  This includes arithmetic
opeators, passing arguments to functions, returning values from
functions, conditions in if, while, for-in-where statements, etc.
Examples:

(poke) +info
0x300000000UL
(poke) -info
0xfffffffd00000000UL
(poke) defun getinfo = uint<64>: { return Elf64_RelInfo {}; }
(poke) getinfo
0x0UL
(poke) Elf64_RelInfo { r_sym = 2 } + Elf64_RelInfo { r_sym = 3 }
0x500000000UL

Next step is to add support for casting integers to integral structs,
i.e.:

(poke) 0x300000000 as Elf64_RelInfo
Elf64_RelInfo {
r_sym=0x3U,
r_type=0x0U
}

Salud!

```