Skip to content

Commit 51efb1e

Browse files
authored
docs: update API guidelines to account for recent polymorphism changes (#1533)
1 parent 88e240b commit 51efb1e

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

book/src/libraries/api_lib_guide.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ int32_t myFunc(int32_t);
3636
In contrast, a `FUNCTION_BLOCK` is backed by a struct and is globally accessible by a defined instance.
3737
To declare a `FUNCTION_BLOCK`, a backing struct has to be declared and passed as a reference to the function block implementation.
3838
39+
> **_NOTE:_** Due to OOP polymorphism features, all function blocks **must** include a `__vtable` parameter as the first field in their C struct representation. This vtable (virtual table) pointer enables dynamic method dispatch and polymorphic behavior. Since structured text does not support a `virtual` keyword, all function blocks have this vtable parameter regardless of whether they use inheritance or polymorphism. When interfacing with C code, this parameter must be included in the struct definition.
40+
3941
```iecst
4042
FUNCTION_BLOCK myFb
4143
VAR_INPUT
@@ -46,6 +48,7 @@ END_FUNCTION_BLOCK
4648

4749
```c
4850
typedef struct {
51+
void* __vtable;
4952
int32_t x;
5053
} myFunctStr;
5154

@@ -107,6 +110,7 @@ END_FUNCTION_BLOCK
107110
```c
108111

109112
typedef struct {
113+
void* __vtable;
110114
int32_t myInt;
111115
char myString[256];
112116
int32_t* myInOutInt;
@@ -134,6 +138,7 @@ END_FUNCTION_BLOCK
134138
```c
135139
136140
typedef struct {
141+
void* __vtable;
137142
int32_t current;
138143
} CountStruct;
139144
@@ -213,6 +218,7 @@ The C interface would look like:
213218

214219
```c
215220
typedef struct {
221+
void* __vtable;
216222
int32_t x;
217223
int32_t* y;
218224
int32_t myOut;
@@ -296,6 +302,8 @@ Struct alignment in plc follows the default behaviour of `C`.
296302
When developing a library in `C` a normal struct can be declared.
297303
In langugages other than `C` the struct has to be `C` compatible. For example in `rust` the `#[repr(C)]` can be used to make the struct `C` compatible.
298304
305+
> **_IMPORTANT:_** All `FUNCTION_BLOCK` structs must include a `__vtable` parameter as the first field. This is required for the OOP polymorphism implementation. The vtable is a `void*` pointer that points to the virtual function table for the function block instance. This affects struct layout and alignment when interfacing with external code.
306+
299307
Example:
300308
301309
```iecst
@@ -313,6 +321,7 @@ The `C` struct would look like:
313321
```c
314322

315323
typedef struct {
324+
void* __vtable;
316325
int32_t x;
317326
int32_t* y;
318327
char z[256];
@@ -323,8 +332,11 @@ typedef struct {
323332

324333
The `rust` struct would look like
325334
```rust
335+
use std::ffi::{c_void, c_char};
336+
326337
#[repr(C)]
327338
pub struct myStruct {
339+
__vtable: *mut c_void,
328340
x: i32,
329341
y: *mut i32,
330342
z: [c_char; 256],
@@ -347,6 +359,7 @@ For a C implementation:
347359

348360
```c
349361
typedef struct {
362+
void* __vtable;
350363
int a;
351364
int b;
352365
// Other members as needed

0 commit comments

Comments
 (0)