Skip to content

Commit 611697b

Browse files
committed
Codegen: Add Raw::getImmediateChild and Synth::getSynthParent
1 parent 000f33f commit 611697b

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

misc/codegen/templates/ql_db.mustache

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,79 @@ module Raw {
2929
{{type}} {{getter}}({{#is_indexed}}int index{{/is_indexed}}) {
3030
{{tablename}}({{#tableparams}}{{^first}}, {{/first}}{{param}}{{/tableparams}})
3131
}
32+
{{#is_indexed}}
33+
/**
34+
* Gets the number of {{doc_plural}}.
35+
{{#internal}}
36+
* INTERNAL: Do not use.
37+
{{/internal}}
38+
*/
39+
int getNumberOf{{plural}}() {
40+
result = count(int i | {{tablename}}(this, i, _))
41+
}
42+
{{/is_indexed}}
3243
{{/synth}}
3344
{{/properties}}
3445
}
3546

47+
48+
{{#final}}
49+
private Element getImmediateChildOf{{name}}({{name}} e, int index) {
50+
{{^has_children}}none(){{/has_children}}
51+
{{#has_children}}
52+
{{! n is the base offset 0, for ease of generation }}
53+
{{! n<child> is constructed to be strictly greater than the indexes for <child> children }}
54+
exists(int n{{#all_children}}, int n{{property.singular}}{{/all_children}} |
55+
n = 0
56+
{{#all_children}}
57+
{{#property}}
58+
{{#synth}}
59+
and
60+
n{{singular}} = n{{prev}}
61+
{{/synth}}
62+
{{^synth}}
63+
{{! n<child> is defined on top of the previous definition }}
64+
{{! for single and optional properties it adds 1 (regardless of whether the optional property exists) }}
65+
{{! for repeated it adds 1 + the maximum index (which works for repeated optional as well) }}
66+
and
67+
n{{singular}} = n{{prev}} {{^is_repeated}}+ 1{{/is_repeated}}{{#is_repeated}}+ e.getNumberOf{{plural}}(){{/is_repeated}}
68+
{{/synth}}
69+
{{/property}}
70+
{{/all_children}} and (
71+
none()
72+
{{#all_children}}
73+
{{#property}}
74+
{{^synth}}
75+
or
76+
{{#is_repeated}}
77+
result = e.get{{singular}}(index - n{{prev}})
78+
{{/is_repeated}}
79+
{{^is_repeated}}
80+
index = n{{prev}} and result = e.get{{singular}}()
81+
{{/is_repeated}}
82+
{{/synth}}
83+
{{/property}}
84+
{{/all_children}}
85+
))
86+
{{/has_children}}
87+
}
88+
{{/final}}
89+
3690
{{/classes}}
91+
92+
/**
93+
* Gets the immediate child indexed at `index`. Indexes are not guaranteed to be contiguous, but are guaranteed to be distinct.
94+
*/
95+
pragma[nomagic]
96+
Element getImmediateChild(Element e, int index) {
97+
// why does this look more complicated than it should?
98+
// * none() simplifies generation, as we can append `or ...` without a special case for the first item
99+
none()
100+
{{#classes}}
101+
{{#final}}
102+
or
103+
result = getImmediateChildOf{{name}}(e, index)
104+
{{/final}}
105+
{{/classes}}
106+
}
37107
}

misc/codegen/templates/ql_parent.mustache

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ private module Impl {
2323
{{! for single and optional properties it adds 1 (regardless of whether the optional property exists) }}
2424
{{! for repeated it adds 1 + the maximum index (which works for repeated optional as well) }}
2525
and
26-
n{{singular}} = n{{prev}} + 1{{#is_repeated}}+ max(int i | i = -1 or exists(e.get{{#type_is_hideable}}Immediate{{/type_is_hideable}}{{singular}}(i)) | i){{/is_repeated}}
26+
{{#type_is_hideable}}
27+
n{{singular}} = n{{prev}} + 1{{#is_repeated}}+ max(int i | i = -1 or exists(e.getImmediate{{singular}}(i)) | i){{/is_repeated}}
28+
{{/type_is_hideable}}
29+
{{^type_is_hideable}}
30+
n{{singular}} = n{{prev}} {{^is_repeated}}+ 1{{/is_repeated}}{{#is_repeated}}+ e.getNumberOf{{plural}}(){{/is_repeated}}
31+
{{/type_is_hideable}}
2732
{{/property}}
2833
{{/all_children}} and (
2934
none()

misc/codegen/templates/ql_synth_types.mustache

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,28 @@ module Synth {
3232
{{/root}}
3333
{{/non_final_classes}}
3434

35+
/**
36+
* INTERNAL: Do not use.
37+
*
38+
* Gets the parent of synthetic element `e`.
39+
*/
40+
Raw::{{root}} getSynthParent(T{{root}} e) {
41+
none()
42+
{{#final_classes}}
43+
{{#is_fresh_synth}}
44+
{{#has_params}}
45+
or
46+
e = T{{name}}({{#params}}{{#first}}result{{/first}}{{^first}}, _{{/first}}{{/params}})
47+
{{/has_params}}
48+
{{/is_fresh_synth}}
49+
{{/final_classes}}
50+
}
51+
52+
3553
{{#final_classes}}
3654
/**
3755
* INTERNAL: Do not use.
56+
*
3857
* Converts a raw element to a synthesized `T{{name}}`, if possible.
3958
*/
4059
T{{name}} convert{{name}}FromRaw(Raw::Element e) {

0 commit comments

Comments
 (0)