Skip to content

Commit 1d10f14

Browse files
authored
Merge pull request #9100 from redsun82/swift-tbd-rework
Swift: changes required for TBD node rework
2 parents 2b6e0cf + bf71e4c commit 1d10f14

File tree

246 files changed

+355
-263
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

246 files changed

+355
-263
lines changed

swift/codegen/cppgen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def _get_field(cls: schema.Class, p: schema.Property, trap_affix: str) -> cpp.Fi
2727
if not p.is_predicate:
2828
trap_name = inflection.pluralize(trap_name)
2929
args = dict(
30-
name=p.name + ("_" if p.name in cpp.cpp_keywords else ""),
30+
field_name=p.name + ("_" if p.name in cpp.cpp_keywords else ""),
3131
type=_get_type(p.type, trap_affix),
3232
is_optional=p.is_optional,
3333
is_repeated=p.is_repeated,

swift/codegen/lib/cpp.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
_field_overrides = [
1919
(re.compile(r"(start|end)_(line|column)|index|width|num_.*"), {"type": "unsigned"}),
20-
(re.compile(r"(.*)_"), lambda m: {"name": m[1]}),
20+
(re.compile(r"(.*)_"), lambda m: {"field_name": m[1]}),
2121
]
2222

2323

@@ -31,7 +31,7 @@ def get_field_override(field: str):
3131

3232
@dataclass
3333
class Field:
34-
name: str
34+
field_name: str
3535
type: str
3636
is_optional: bool = False
3737
is_repeated: bool = False
@@ -44,12 +44,8 @@ def __post_init__(self):
4444
self.type = f"std::optional<{self.type}>"
4545
if self.is_repeated:
4646
self.type = f"std::vector<{self.type}>"
47-
48-
@property
49-
def cpp_name(self):
50-
if self.name in cpp_keywords:
51-
return self.name + "_"
52-
return self.name
47+
if self.field_name in cpp_keywords:
48+
self.field_name += "_"
5349

5450
# using @property breaks pystache internals here
5551
def get_streamer(self):
@@ -65,8 +61,6 @@ def is_single(self):
6561
return not (self.is_optional or self.is_repeated or self.is_predicate)
6662

6763

68-
69-
7064
@dataclass
7165
class Trap:
7266
table_name: str

swift/codegen/lib/schema.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ def load(path):
9292
data = yaml.load(input, Loader=yaml.SafeLoader)
9393
grouper = _DirSelector(data.get("_directories", {}).items())
9494
classes = {root_class_name: Class(root_class_name)}
95-
assert root_class_name not in data
9695
classes.update((cls, Class(cls, dir=grouper.get(cls))) for cls in data if not cls.startswith("_"))
9796
for name, info in data.items():
9897
if name.startswith("_"):
@@ -110,7 +109,7 @@ def load(path):
110109
classes[base].derived.add(name)
111110
elif k == "_dir":
112111
cls.dir = pathlib.Path(v)
113-
if not cls.bases:
112+
if not cls.bases and cls.name != root_class_name:
114113
cls.bases.add(root_class_name)
115114
classes[root_class_name].derived.add(name)
116115

swift/codegen/templates/cpp_classes.mustache

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace {{namespace}} {
1414

1515
struct {{name}}{{#final}} : Binding<{{name}}Tag>{{#bases}}, {{ref.name}}{{/bases}}{{/final}}{{^final}}{{#has_bases}}: {{#bases}}{{^first}}, {{/first}}{{ref.name}}{{/bases}}{{/has_bases}}{{/final}} {
1616
{{#fields}}
17-
{{type}} {{name}}{};
17+
{{type}} {{field_name}}{};
1818
{{/fields}}
1919
{{#final}}
2020

@@ -27,27 +27,27 @@ struct {{name}}{{#final}} : Binding<{{name}}Tag>{{#bases}}, {{ref.name}}{{/bases
2727
protected:
2828
void emit({{^final}}{{trap_affix}}Label<{{name}}Tag> id, {{/final}}std::ostream& out) const {
2929
{{#trap_name}}
30-
out << {{.}}{{trap_affix}}{id{{#single_fields}}, {{name}}{{/single_fields}}} << '\n';
30+
out << {{.}}{{trap_affix}}{id{{#single_fields}}, {{field_name}}{{/single_fields}}} << '\n';
3131
{{/trap_name}}
3232
{{#bases}}
3333
{{ref.name}}::emit(id, out);
3434
{{/bases}}
3535
{{#fields}}
3636
{{#is_predicate}}
37-
if ({{name}}) out << {{trap_name}}{{trap_affix}}{id} << '\n';
37+
if ({{field_name}}) out << {{trap_name}}{{trap_affix}}{id} << '\n';
3838
{{/is_predicate}}
3939
{{#is_optional}}
4040
{{^is_repeated}}
41-
if ({{name}}) out << {{trap_name}}{{trap_affix}}{id, *{{name}}} << '\n';
41+
if ({{field_name}}) out << {{trap_name}}{{trap_affix}}{id, *{{field_name}}} << '\n';
4242
{{/is_repeated}}
4343
{{/is_optional}}
4444
{{#is_repeated}}
45-
for (auto i = 0u; i < {{name}}.size(); ++i) {
45+
for (auto i = 0u; i < {{field_name}}.size(); ++i) {
4646
{{^is_optional}}
47-
out << {{trap_name}}{{trap_affix}}{id, i, {{name}}[i]};
47+
out << {{trap_name}}{{trap_affix}}{id, i, {{field_name}}[i]};
4848
{{/is_optional}}
4949
{{#is_optional}}
50-
if ({{name}}[i]) out << {{trap_name}}{{trap_affix}}{id, i, *{{name}}[i]};
50+
if ({{field_name}}[i]) out << {{trap_name}}{{trap_affix}}{id, i, *{{field_name}}[i]};
5151
{{/is_optional}}
5252
}
5353
{{/is_repeated}}

swift/codegen/templates/ql_class.mustache

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ class {{name}}Base extends {{db_id}}{{#bases}}, {{.}}{{/bases}} {
88
{{#root}}
99
string toString() { none() } // overridden by subclasses
1010

11+
string getAPrimaryQlClass() { none() } // overridden by subclasses
12+
13+
final string getPrimaryQlClasses() { result = concat(this.getAPrimaryQlClass(), ",") }
14+
1115
{{name}}Base getResolveStep() { none() } // overridden by subclasses
1216

1317
{{name}}Base resolve() {
@@ -17,7 +21,7 @@ class {{name}}Base extends {{db_id}}{{#bases}}, {{.}}{{/bases}} {
1721
}
1822
{{/root}}
1923
{{#final}}
20-
override string toString() { result = "{{name}}" }
24+
override string getAPrimaryQlClass() { result = "{{name}}" }
2125
{{/final}}
2226
{{#properties}}
2327

@@ -32,6 +36,12 @@ class {{name}}Base extends {{db_id}}{{#bases}}, {{.}}{{/bases}} {
3236
{{tablename}}({{#tableparams}}{{^first}}, {{/first}}{{param}}{{/tableparams}})
3337
{{/type_is_class}}
3438
}
39+
{{#is_optional}}
40+
41+
predicate has{{singular}}({{#is_repeated}}int index{{/is_repeated}}) {
42+
exists({{getter}}({{#is_repeated}}index{{/is_repeated}}))
43+
}
44+
{{/is_optional}}
3545
{{#is_repeated}}
3646

3747
{{type}} {{indefinite_getter}}() {

swift/codegen/templates/trap_traps.mustache

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <string>
77

88
#include "{{include_dir}}/{{trap_affix}}Label.h"
9+
#include "{{include_dir}}/{{trap_affix}}TagTraits.h"
910
#include "./{{trap_affix}}Tags.h"
1011

1112
namespace {{namespace}} {
@@ -15,19 +16,25 @@ namespace {{namespace}} {
1516
struct {{name}}{{trap_affix}} {
1617
static constexpr bool is_binding = {{#id}}true{{/id}}{{^id}}false{{/id}};
1718
{{#id}}
18-
{{type}} getBoundLabel() const { return {{cpp_name}}; }
19+
{{type}} getBoundLabel() const { return {{field_name}}; }
1920
{{/id}}
2021

2122
{{#fields}}
22-
{{type}} {{cpp_name}}{};
23+
{{type}} {{field_name}}{};
2324
{{/fields}}
2425
};
2526

2627
inline std::ostream &operator<<(std::ostream &out, const {{name}}{{trap_affix}} &e) {
2728
out << "{{table_name}}("{{#fields}}{{^first}} << ", "{{/first}}
28-
<< {{#get_streamer}}e.{{cpp_name}}{{/get_streamer}}{{/fields}} << ")";
29+
<< {{#get_streamer}}e.{{field_name}}{{/get_streamer}}{{/fields}} << ")";
2930
return out;
3031
}
31-
{{/traps}}
32+
{{#id}}
3233

34+
template <>
35+
struct TagToBindingTrapFunctor<typename {{type}}::Tag> {
36+
using type = {{name}}{{trap_affix}};
37+
};
38+
{{/id}}
39+
{{/traps}}
3340
}

swift/codegen/test/test_cpp.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88

99
@pytest.mark.parametrize("keyword", cpp.cpp_keywords)
10-
def test_field_keyword_cpp_name(keyword):
10+
def test_field_keyword_name(keyword):
1111
f = cpp.Field(keyword, "int")
12-
assert f.cpp_name == keyword + "_"
12+
assert f.field_name == keyword + "_"
1313

1414

15-
def test_field_cpp_name():
15+
def test_field_name():
1616
f = cpp.Field("foo", "int")
17-
assert f.cpp_name == "foo"
17+
assert f.field_name == "foo"
1818

1919

2020
@pytest.mark.parametrize("type,expected", [

swift/codegen/test/test_schema.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,5 +155,17 @@ def test_properties(load):
155155
]
156156

157157

158+
def test_element_properties(load):
159+
ret = load("""
160+
Element:
161+
x: string
162+
""")
163+
assert ret.classes == [
164+
schema.Class(root_name, properties=[
165+
schema.SingleProperty('x', 'string'),
166+
]),
167+
]
168+
169+
158170
if __name__ == '__main__':
159171
sys.exit(pytest.main([__file__] + sys.argv[1:]))

swift/codegen/trapgen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def get_cpp_type(schema_type: str, trap_affix: str):
2828

2929
def get_field(c: dbscheme.Column, trap_affix: str):
3030
args = {
31-
"name": c.schema_name,
31+
"field_name": c.schema_name,
3232
"type": c.type,
3333
}
3434
args.update(cpp.get_field_override(c.schema_name))

swift/extractor/trap/TrapLabel.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ class UntypedTrapLabel {
2727
friend bool operator==(UntypedTrapLabel lhs, UntypedTrapLabel rhs) { return lhs.id_ == rhs.id_; }
2828
};
2929

30-
template <typename Tag>
30+
template <typename TagParam>
3131
class TrapLabel : public UntypedTrapLabel {
3232
template <typename OtherTag>
3333
friend class TrapLabel;
3434

3535
using UntypedTrapLabel::UntypedTrapLabel;
3636

3737
public:
38+
using Tag = TagParam;
39+
3840
TrapLabel() = default;
3941

4042
template <typename OtherTag>

0 commit comments

Comments
 (0)