Pavona Software APIs
ujson_rust.h
1// Copyright lowRISC contributors (OpenTitan project).
2// Copyright zeroRISC Inc.
3// Licensed under the Apache License, Version 2.0, see LICENSE for details.
4// SPDX-License-Identifier: Apache-2.0
5
6#ifndef OPENTITAN_SW_DEVICE_LIB_UJSON_UJSON_RUST_H_
7#define OPENTITAN_SW_DEVICE_LIB_UJSON_UJSON_RUST_H_
8
9// Definitions of the UJSON macros that emit code that can be trivially
10// transformed into rust code:
11//
12// Preprocess the input, grep out all the preprocessor noise,
13// translate `rust_attr` into `#` (rust attributes) and finally format the code:
14// gcc -nostdinc -I. -E -DRUST_PREPROCESSOR_EMIT <input_file> \
15// | grep -v '#' \
16// | sed -e "s/rust_attr/#/g" \
17// | rustfmt > output_file
18
19#ifndef RUST_PREPROCESSOR_EMIT
20#error "Do not include this file directly. Include ujson_derive.h instead."
21#endif // RUST_PREPROCESSOR_EMIT
22#include "sw/device/lib/base/adv_macros.h"
23
24#define uint64_t u64
25#define uint32_t u32
26#define uint16_t u16
27#define uint8_t u8
28#define size_t usize
29
30#define int64_t i64
31#define int32_t i32
32#define int16_t i16
33#define int8_t i8
34
35#define RUST_DEFAULT_DERIVE \
36 Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq
37
38// clang-format off
39rust_attr[allow(unused_imports)]
40use opentitanlib::test_utils::status::status_t;
41rust_attr[allow(unused_imports)]
42use opentitanlib::test_utils::hex_bytes;
43
44// clang-format is turned off; as scary as these macros look, they look
45// even scarier after clang-format is done with them.
46#define ujson_struct_field_array_indirect() ujson_struct_field_array
47#define ujson_struct_field_array(t_, sz_, ...) \
48 OT_IIF(OT_NOT(OT_VA_ARGS_COUNT(dummy, ##__VA_ARGS__))) \
49 ( /*then*/ \
50 arrayvec::ArrayVec<OT_EXPAND(t_, sz_)> \
51 , /*else*/ \
52 arrayvec::ArrayVec<OT_OBSTRUCT(ujson_struct_field_array_indirect)()(t_, __VA_ARGS__), sz_> \
53 ) /*endif*/
54
55#define ujson_struct_field(name_, type_, ...) \
56 pub OT_IIF(OT_NOT(OT_VA_ARGS_COUNT(dummy, ##__VA_ARGS__))) \
57 ( /*then*/ \
58 name_: type_ \
59 , /*else*/ \
60 name_: OT_EVAL(ujson_struct_field_array(type_, __VA_ARGS__)) \
61 ) /*endif*/,
62
63#define ujson_struct_string(name_, size_, ...) \
64 ujson_struct_field(name_, String, ##__VA_ARGS__)
65
66#define ujson_struct_bytes(name_, size_, len_) \
67 rust_attr[serde(with = "hex_bytes")] /*transform into #[...] */ \
68 ujson_struct_field(name_, uint8_t, size_)
69
70#define UJSON_DECLARE_STRUCT(formal_name_, name_, decl_, ...) \
71 OT_IIF(OT_NOT(OT_VA_ARGS_COUNT(dummy, ##__VA_ARGS__))) \
72 ( /*then*/ \
73 rust_attr[derive(RUST_DEFAULT_DERIVE)] /*transform into #[...] */ \
74 , /*else*/ \
75 rust_attr[derive(__VA_ARGS__)] /*transform into #[...] */ \
76 ) /*endif*/ \
77 rust_attr[allow(non_camel_case_types)] \
78 pub struct name_ { \
79 decl_(ujson_struct_field, ujson_struct_string, ujson_struct_bytes) \
80 } \
81 rust_attr[allow(dead_code)] \
82 pub type formal_name_ = name_ /*eat_semicolon*/
83
84#define ujson_enum_value(formal_name_, name_, ...) \
85 pub OT_IIF(OT_NOT(OT_VA_ARGS_COUNT(dummy, ##__VA_ARGS__))) \
86 ( /*then*/ \
87 name_ \
88 , /*else*/ \
89 name_ = __VA_ARGS__ \
90 ) /*endif*/,
91
92#define UJSON_DECLARE_ENUM(formal_name_, name_, decl_, ...) \
93 OT_IIF(OT_NOT(OT_VA_ARGS_COUNT(dummy, ##__VA_ARGS__))) \
94 ( /*then*/ \
95 rust_attr[derive(RUST_DEFAULT_DERIVE)] /*transform into #[...] */ \
96 , /*else*/ \
97 rust_attr[derive(__VA_ARGS__)] /*transform into #[...] */ \
98 ) /*endif*/ \
99 rust_attr[repr(u32)] /* transform into #[...] */ \
100 rust_attr[allow(non_camel_case_types)] \
101 pub enum name_ { \
102 decl_(formal_name_, ujson_enum_value) \
103 RUST_ENUM_INTVALUE (u32), \
104 } \
105 rust_attr[allow(dead_code)] \
106 pub type formal_name_ = name_ /*eat_semicolon*/
107// clang-format on
108
109//////////////////////////////////////////////////////////////////////
110// Combined build-everything macro
111//////////////////////////////////////////////////////////////////////
112#define UJSON_SERDE_STRUCT(formal_name_, name_, decl_, ...) \
113 UJSON_DECLARE_STRUCT(formal_name_, name_, decl_, ##__VA_ARGS__)
114
115#define UJSON_SERDE_ENUM(formal_name_, name_, decl_, ...) \
116 UJSON_DECLARE_ENUM(formal_name_, name_, decl_, ##__VA_ARGS__)
117
118#define C_ONLY(x) const _ : () = {/* eat a semicolon */}
119#define RUST_ONLY(x) x
120#endif // OPENTITAN_SW_DEVICE_LIB_UJSON_UJSON_RUST_H_