C Has No Annotations — But It Has “Annotation-Like” Things: Starting from Ambiorix ODL
Grow through practice
No need to finish reading — test nowC programmers often have an illusion: annotations are a Java/Spring thing. They are not — C has no “language-level annotations”, but annotation-like mechanisms have always existed.
First, what is an annotation?
An annotation = declaring intent/metadata and letting a tool process it.
- Java:
@Autowired,@Override→ processed by the compiler/framework; - C: no
@, but other forms achieve the same goal.
Four kinds of “annotation-like” mechanisms
1) Compiler/checker attributes (closest to annotations)
int foo(const char *p) __attribute__((nonnull(1)));
- GCC/Clang:
__attribute__((...)),[[nodiscard]]; - Linux kernel +
sparse:__user,__iomem,__init— “annotations” for a static checker; - Clang thread safety:
GUARDED_BY(mu),PT_GUARDED_BY; - Microsoft SAL:
_In_,_Out_(for static analysis).
Characteristic: annotations for tools, no runtime effect.
2) Declarative DSL / IDL (closest to the “intent” of Spring annotations)
- Ambiorix ODL: describes objects/parameters/functions and assembles the data model;
- protobuf-c / Cap'n Proto / FlatBuffers: IDL → generated C;
- D-Bus introspection XML;
- OVSDB schema (Open vSwitch).
ODL sketch (syntax follows the libamxo docs):
%define {
object Device {
object DeviceInfo {
parameter ModelName {
type = "string";
}
}
}
}
Characteristic: structure/intent lives outside the code, then is loaded or generated — declarative, just like annotations.
3) Preprocessor metaprogramming (C's “crude way”)
X-macros: one table generates code.
#define COLORS(X) \
X(RED, 0xff0000) \
X(GREEN, 0x00ff00) \
X(BLUE, 0x0000ff)
#define AS_ENUM(name, val) COLOR_##name,
enum color { COLORS(AS_ENUM) };
#define AS_STR(name, val) #name,
const char *color_names[] = { COLORS(AS_STR) };
There are also P99, Boost.PP, and GObject's G_DECLARE_FINAL_TYPE / G_DEFINE_TYPE (declaring types/properties/signals).
Characteristic: it works, but readability and tooling are poor.
4) Formal annotations
- Frama-C ACSL:
@requires,@ensures— for formal verification.
Comparison
| Where | Processed by | Readability | Tooling | |
|---|---|---|---|---|
| Compiler attributes | in code | compiler/checker | medium | good |
| IDL / ODL | outside code | generator/loader | high | good |
| X-macros | in code | preprocessor | low | poor |
| Formal annotations | in code | verifier | medium | specialized |
Why AMX chose ODL
C has no annotations:
- simulating them with macros → hard to read, hard to tool;
- using an external DSL (ODL) → structure externalized, readable, validatable, serializable.
And ODL is not just for looks: it feeds the data model directly (libamxd) and the bus/RPC (libamxb), and it can be dumped/serialized. Annotations “declare intent”; ODL is “declaration + an executable data model”.
One sentence
C has no annotations, but annotation-like things have always existed: attributes, IDL/ODL, X-macros, formal annotations. AMX chose ODL — not because it is new, but because it is the most readable and the most serializable.
Series: Inside prplOS: AMX Runtime (Guide). Example code: github.com/zishuowang696/amx-examples
Comments 0
No comments yet — be the first!