zerodds_ast/node/annotation.rs
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 ZeroDDS Contributors
3//! Annotation declarations. Spec §3.6.
4
5use crate::facet::Facets;
6use crate::node::constant::ConstExprNode;
7use crate::node::module::ModuleNode;
8use crate::node::type_::TypeNode;
9use alloc::string::String;
10use alloc::vec::Vec;
11use zerodds_ast_core::{Id, NodeId, Ref};
12
13/// An `@annotation` declaration. Spec §3.6.
14///
15/// Built-in annotations are ordinary declarations in the builtin module,
16/// so user-defined and built-in annotations traverse identically.
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub struct AnnotationDefNode {
19 /// Stable identity.
20 pub node_id: NodeId,
21 /// Simple name, without the `@`.
22 pub name: String,
23 /// Enclosing module.
24 pub module: Id<ModuleNode>,
25 /// Members in declaration order.
26 pub members: Vec<Id<AnnotationMemberNode>>,
27 /// Per-source information. Spec §4.
28 pub facets: Facets,
29}
30
31/// A member of an annotation declaration. Spec §3.6.
32#[derive(Debug, Clone, PartialEq, Eq)]
33pub struct AnnotationMemberNode {
34 /// Stable identity.
35 pub node_id: NodeId,
36 /// Simple name.
37 pub name: String,
38 /// Declared type.
39 pub type_: Ref<TypeNode>,
40 /// Default value, if declared.
41 pub default: Option<Id<ConstExprNode>>,
42 /// Whether this is the implicit single member named `value`, which
43 /// enables the `@ann(x)` short form.
44 pub is_value_member: bool,
45 /// The declaration this member belongs to.
46 pub parent: Id<AnnotationDefNode>,
47 /// Per-source information. Spec §4.
48 pub facets: Facets,
49}
50
51/// The built-in annotations the model always carries.
52///
53/// Sources: DDS-XTypes 1.3 §7.3.1.2 and Annex B, plus the IDL 4.2
54/// building-block annotations. The order is fixed, because builtin
55/// identities are allocated in it and must be identical in every model
56/// so that snapshots from different processes compare. Spec §3.6, §9.
57pub const BUILTIN_ANNOTATIONS: [&str; 40] = [
58 "id",
59 "autoid",
60 "optional",
61 "position",
62 "value",
63 "extensibility",
64 "final",
65 "appendable",
66 "mutable",
67 "key",
68 "must_understand",
69 "default_literal",
70 "default",
71 "range",
72 "min",
73 "max",
74 "unit",
75 "bit_bound",
76 "external",
77 "nested",
78 "verbatim",
79 "service",
80 "oneway",
81 "ami",
82 "non_serialized",
83 "try_construct",
84 "topic",
85 "hashid",
86 "data_representation",
87 "ignore_literal_names",
88 // Appended, never inserted: the order allocates builtin identities
89 // and a snapshot written by another build has to compare.
90 "default_nested",
91 // DDS-RPC 1.0's own annotations, none of which the frontend can
92 // expect a file to declare: §7.4.2.2 `@DDSRequestTopic`/
93 // `@DDSReplyTopic`, §7.3.1.4 `@RPCRequestType`/`@RPCReplyType`,
94 // §7.10.1 `@Qos`, and §7.5.1.2.1 `@Choice`/`@Autoid`/`@Empty`/
95 // `@Mutable` (capitalised, and so distinct from the DDS-XTypes
96 // `@autoid`/`@mutable` above). Appended for the same reason.
97 "DDSRequestTopic",
98 "DDSReplyTopic",
99 "RPCRequestType",
100 "RPCReplyType",
101 "Qos",
102 "Choice",
103 "Autoid",
104 "Empty",
105 "Mutable",
106];
107
108/// Name of the module holding the built-in declarations.
109///
110/// Not a legal IDL identifier, so a frontend cannot declare a module
111/// that collides with it.
112pub const BUILTIN_MODULE_NAME: &str = "@builtin";