Skip to main content

luaur_analysis/methods/
ast_json_encoder_write_json_bridges.rs

1//! Source: `Analysis/src/AstJsonEncoder.cpp:89-1052` (hand-ported)
2//! C++ resolves `write(propName, value)` by overload on the static type of
3//! `value`; Rust resolves it through `WriteJson`. One impl per C++ overload,
4//! each delegating to the inherent method holding that overload's body.
5//! Base-node pointers go through `write(AstNode*) = node->visit(this)`
6//! (AstJsonEncoder.cpp:271); concrete-node pointers bind their concrete
7//! overload exactly as C++ overload resolution does.
8use crate::methods::ast_json_encoder_write_primitives::WriteJson;
9use crate::records::ast_json_encoder::AstJsonEncoder;
10use luaur_ast::records::ast_array::AstArray;
11use luaur_ast::records::ast_attr::AstAttr;
12use luaur_ast::records::ast_declared_extern_type_property::AstDeclaredExternTypeProperty;
13use luaur_ast::records::ast_expr::AstExpr;
14use luaur_ast::records::ast_expr_binary::AstExprBinary_Op;
15use luaur_ast::records::ast_expr_function::AstExprFunction;
16use luaur_ast::records::ast_expr_table::{Item, ItemKind};
17use luaur_ast::records::ast_expr_unary::AstExprUnaryOp;
18use luaur_ast::records::ast_generic_type::AstGenericType;
19use luaur_ast::records::ast_generic_type_pack::AstGenericTypePack;
20use luaur_ast::records::ast_local::AstLocal;
21use luaur_ast::records::ast_node::AstNode;
22use luaur_ast::records::ast_stat::AstStat;
23use luaur_ast::records::ast_stat_block::AstStatBlock;
24use luaur_ast::records::ast_table_indexer::AstTableIndexer;
25use luaur_ast::records::ast_table_prop::AstTableProp;
26use luaur_ast::records::ast_type::AstType;
27use luaur_ast::records::ast_type_list::AstTypeList;
28use luaur_ast::records::ast_type_or_pack::AstTypeOrPack;
29use luaur_ast::records::ast_type_pack::AstTypePack;
30use luaur_ast::records::location::Location;
31use luaur_ast::records::position::Position;
32use luaur_ast::type_aliases::ast_argument_name::AstArgumentName;
33
34// write(AstNode*) and the implicit base-pointer conversions: virtual dispatch.
35impl WriteJson for *mut AstNode {
36    fn write_json(&self, enc: &mut AstJsonEncoder) {
37        enc.write_ast_node(*self);
38    }
39}
40impl WriteJson for *mut AstExpr {
41    fn write_json(&self, enc: &mut AstJsonEncoder) {
42        enc.write_ast_node(*self as *mut AstNode);
43    }
44}
45impl WriteJson for *mut AstStat {
46    fn write_json(&self, enc: &mut AstJsonEncoder) {
47        enc.write_ast_node(*self as *mut AstNode);
48    }
49}
50impl WriteJson for *mut AstType {
51    fn write_json(&self, enc: &mut AstJsonEncoder) {
52        enc.write_ast_node(*self as *mut AstNode);
53    }
54}
55impl WriteJson for *mut AstTypePack {
56    fn write_json(&self, enc: &mut AstJsonEncoder) {
57        enc.write_ast_node(*self as *mut AstNode);
58    }
59}
60// Concrete static types bind their exact overload (no virtual call in C++).
61impl WriteJson for *mut AstStatBlock {
62    fn write_json(&self, enc: &mut AstJsonEncoder) {
63        enc.write_ast_stat_block(*self);
64    }
65}
66impl WriteJson for *mut AstExprFunction {
67    fn write_json(&self, enc: &mut AstJsonEncoder) {
68        enc.write_ast_expr_function(*self);
69    }
70}
71impl WriteJson for *mut AstLocal {
72    fn write_json(&self, enc: &mut AstJsonEncoder) {
73        enc.write_ast_local(*self);
74    }
75}
76impl WriteJson for *mut AstAttr {
77    fn write_json(&self, enc: &mut AstJsonEncoder) {
78        enc.write_ast_attr(*self);
79    }
80}
81impl WriteJson for *mut AstGenericType {
82    fn write_json(&self, enc: &mut AstJsonEncoder) {
83        enc.write_ast_generic_type(*self);
84    }
85}
86impl WriteJson for *mut AstGenericTypePack {
87    fn write_json(&self, enc: &mut AstJsonEncoder) {
88        enc.write_ast_generic_type_pack(*self);
89    }
90}
91impl WriteJson for *mut AstTableIndexer {
92    fn write_json(&self, enc: &mut AstJsonEncoder) {
93        enc.write_ast_table_indexer(*self);
94    }
95}
96impl WriteJson for Location {
97    fn write_json(&self, enc: &mut AstJsonEncoder) {
98        enc.write_location(self);
99    }
100}
101impl WriteJson for Position {
102    fn write_json(&self, enc: &mut AstJsonEncoder) {
103        enc.write_position(self);
104    }
105}
106// AstArgumentName = std::pair<AstName, Location>
107impl WriteJson for AstArgumentName {
108    fn write_json(&self, enc: &mut AstJsonEncoder) {
109        enc.write_ast_argument_name(*self);
110    }
111}
112impl WriteJson for AstTypeList {
113    fn write_json(&self, enc: &mut AstJsonEncoder) {
114        enc.write_ast_type_list(self);
115    }
116}
117impl WriteJson for Item {
118    fn write_json(&self, enc: &mut AstJsonEncoder) {
119        enc.write_ast_expr_table_item(self);
120    }
121}
122impl WriteJson for ItemKind {
123    fn write_json(&self, enc: &mut AstJsonEncoder) {
124        enc.write_ast_expr_table_item_kind(*self);
125    }
126}
127impl WriteJson for AstExprUnaryOp {
128    fn write_json(&self, enc: &mut AstJsonEncoder) {
129        enc.write_ast_expr_unary_op(*self);
130    }
131}
132impl WriteJson for AstExprBinary_Op {
133    fn write_json(&self, enc: &mut AstJsonEncoder) {
134        enc.write_ast_expr_binary_op(*self);
135    }
136}
137impl WriteJson for AstTypeOrPack {
138    fn write_json(&self, enc: &mut AstJsonEncoder) {
139        enc.write_ast_type_or_pack(self);
140    }
141}
142impl WriteJson for AstTableProp {
143    fn write_json(&self, enc: &mut AstJsonEncoder) {
144        enc.write_ast_table_prop(self);
145    }
146}
147impl WriteJson for AstDeclaredExternTypeProperty {
148    fn write_json(&self, enc: &mut AstJsonEncoder) {
149        enc.write_ast_declared_extern_type_property(self);
150    }
151}
152// NB: no WriteJson for c_char itself -- that would make AstArray<c_char>
153// overlap the generic AstArray<T: WriteJson> impl. The one char-typed field
154// (AstExprIndexName::op) calls `write_c_char` explicitly instead.
155// write(AstArray<char>): the chars as one string (AstJsonEncoder.cpp:387).
156impl WriteJson for AstArray<core::ffi::c_char> {
157    fn write_json(&self, enc: &mut AstJsonEncoder) {
158        enc.write_ast_array_c_char(*self);
159    }
160}