Skip to content

Commit d24d933

Browse files
authored
Merge pull request #17460 from github/redsun82/rust-typed-labels
Rust: introduce typed labels
2 parents 37f3ea1 + 1ce4707 commit d24d933

File tree

8 files changed

+4130
-1082
lines changed

8 files changed

+4130
-1082
lines changed

misc/codegen/generators/rustgen.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def _get_type(t: str) -> str:
2020
case "int":
2121
return "usize"
2222
case _ if t[0].isupper():
23-
return "trap::Label"
23+
return f"trap::Label<{t}>"
2424
case "boolean":
2525
assert False, "boolean unsupported"
2626
case _:
@@ -57,6 +57,15 @@ def _get_properties(
5757
yield cls, p
5858

5959

60+
def _get_ancestors(
61+
cls: schema.Class, lookup: dict[str, schema.Class]
62+
) -> typing.Iterable[schema.Class]:
63+
for b in cls.bases:
64+
base = lookup[b]
65+
yield base
66+
yield from _get_ancestors(base, lookup)
67+
68+
6069
class Processor:
6170
def __init__(self, data: schema.Schema):
6271
self._classmap = data.classes
@@ -69,14 +78,15 @@ def _get_class(self, name: str) -> rust.Class:
6978
_get_field(c, p)
7079
for c, p in _get_properties(cls, self._classmap)
7180
if "rust_skip" not in p.pragmas and not p.synth
72-
],
73-
table_name=inflection.tableize(cls.name),
81+
] if not cls.derived else [],
82+
ancestors=sorted(set(a.name for a in _get_ancestors(cls, self._classmap))),
83+
entry_table=inflection.tableize(cls.name) if not cls.derived else None,
7484
)
7585

7686
def get_classes(self):
7787
ret = {"": []}
7888
for k, cls in self._classmap.items():
79-
if not cls.synth and not cls.derived:
89+
if not cls.synth:
8090
ret.setdefault(cls.group, []).append(self._get_class(cls.name))
8191
return ret
8292

misc/codegen/lib/rust.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,19 @@ def is_label(self):
110110
@dataclasses.dataclass
111111
class Class:
112112
name: str
113-
table_name: str
113+
entry_table: str | None = None
114114
fields: list[Field] = dataclasses.field(default_factory=list)
115+
ancestors: list[str] = dataclasses.field(default_factory=list)
116+
117+
@property
118+
def is_entry(self) -> bool:
119+
return bool(self.entry_table)
115120

116121
@property
117-
def single_field_entries(self):
118-
ret = {self.table_name: []}
122+
def single_field_entries(self) -> dict[str, list[dict]]:
123+
ret = {}
124+
if self.is_entry:
125+
ret[self.entry_table] = []
119126
for f in self.fields:
120127
if f.is_single:
121128
ret.setdefault(f.table_name, []).append(f)

misc/codegen/templates/rust_classes.mustache

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,76 @@
22

33
#![cfg_attr(any(), rustfmt::skip)]
44

5-
use crate::trap::{TrapId, TrapEntry};
6-
use codeql_extractor::trap;
5+
use crate::trap;
76
{{#classes}}
87

8+
{{#is_entry}}
99
#[derive(Debug)]
1010
pub struct {{name}} {
11-
pub id: TrapId,
11+
pub id: trap::TrapId<{{name}}>,
1212
{{#fields}}
1313
pub {{field_name}}: {{type}},
1414
{{/fields}}
1515
}
1616

17-
impl TrapEntry for {{name}} {
18-
fn extract_id(&mut self) -> TrapId {
19-
std::mem::replace(&mut self.id, TrapId::Star)
17+
impl trap::TrapEntry for {{name}} {
18+
fn extract_id(&mut self) -> trap::TrapId<Self> {
19+
std::mem::replace(&mut self.id, trap::TrapId::Star)
2020
}
2121

22-
fn emit(self, id: trap::Label, out: &mut trap::Writer) {
22+
fn emit(self, id: trap::Label<Self>, out: &mut trap::Writer) {
2323
{{#single_field_entries}}
24-
out.add_tuple("{{table_name}}", vec![trap::Arg::Label(id){{#fields}}, self.{{field_name}}.into(){{/fields}}]);
24+
out.add_tuple("{{entry_table}}", vec![id.into(){{#fields}}, self.{{field_name}}.into(){{/fields}}]);
2525
{{/single_field_entries}}
2626
{{#fields}}
2727
{{#is_predicate}}
2828
if self.{{field_name}} {
29-
out.add_tuple("{{table_name}}", vec![trap::Arg::Label(id)]);
29+
out.add_tuple("{{table_name}}", vec![id.into()]);
3030
}
3131
{{/is_predicate}}
3232
{{#is_optional}}
3333
{{^is_repeated}}
3434
if let Some(v) = self.{{field_name}} {
35-
out.add_tuple("{{table_name}}", vec![trap::Arg::Label(id), v.into()]);
35+
out.add_tuple("{{table_name}}", vec![id.into(), v.into()]);
3636
}
3737
{{/is_repeated}}
3838
{{/is_optional}}
3939
{{#is_repeated}}
4040
for (i, v) in self.{{field_name}}.into_iter().enumerate() {
4141
{{^is_optional}}
42-
out.add_tuple("{{table_name}}", vec![trap::Arg::Label(id){{^is_unordered}}, i.into(){{/is_unordered}}, v.into()]);
42+
out.add_tuple("{{table_name}}", vec![id.into(){{^is_unordered}}, i.into(){{/is_unordered}}, v.into()]);
4343
{{/is_optional}}
4444
{{#is_optional}}
4545
if let Some(v) = v {
46-
out.add_tuple("{{table_name}}", vec![trap::Arg::Label(id){{^is_unordered}}, i.into(){{/is_unordered}}, v.into()]);
46+
out.add_tuple("{{table_name}}", vec![id.into(){{^is_unordered}}, i.into(){{/is_unordered}}, v.into()]);
4747
}
4848
{{/is_optional}}
4949
}
5050
{{/is_repeated}}
5151
{{/fields}}
5252
}
5353
}
54+
{{/is_entry}}
55+
{{^is_entry}}
56+
{{! virtual class, make it unbuildable }}
57+
#[derive(Debug)]
58+
pub struct {{name}} {
59+
_unused: ()
60+
}
61+
{{/is_entry}}
62+
63+
impl trap::TrapClass for {{name}} {
64+
fn class_name() -> &'static str { "{{name}}" }
65+
}
66+
{{#ancestors}}
67+
68+
impl From<trap::Label<{{name}}>> for trap::Label<{{.}}> {
69+
fn from(value: trap::Label<{{name}}>) -> Self {
70+
// SAFETY: this is safe because in the dbscheme {{name}} is a subclass of {{.}}
71+
unsafe {
72+
Self::from_untyped(value.as_untyped())
73+
}
74+
}
75+
}
76+
{{/ancestors}}
5477
{{/classes}}

rust/extractor/src/generated/.generated.list

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)