Skip to content

Commit cdc99b5

Browse files
committed
Swift: simplify pragma definition
1 parent 51bd1ef commit cdc99b5

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

swift/codegen/lib/schema/defs.py

+26-24
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ def include(source: str):
3535
"__includes", []).append(source)
3636

3737

38+
class _Namespace:
39+
""" simple namespacing mechanism """
40+
41+
def __init__(self, **kwargs):
42+
self.__dict__.update(kwargs)
43+
44+
45+
qltest = _Namespace()
46+
ql = _Namespace()
47+
cpp = _Namespace()
48+
synth = _Namespace()
49+
50+
3851
@_dataclass
3952
class _Pragma(_schema.PropertyModifier):
4053
""" A class or property pragma.
@@ -43,6 +56,10 @@ class _Pragma(_schema.PropertyModifier):
4356
"""
4457
pragma: str
4558

59+
def __post_init__(self):
60+
namespace, _, name = self.pragma.partition('_')
61+
setattr(globals()[namespace], name, self)
62+
4663
def modify(self, prop: _schema.Property):
4764
prop.pragmas.append(self.pragma)
4865

@@ -86,13 +103,6 @@ def __getitem__(self, item):
86103
return item | self.modifier
87104

88105

89-
class _Namespace:
90-
""" simple namespacing mechanism """
91-
92-
def __init__(self, **kwargs):
93-
self.__dict__.update(kwargs)
94-
95-
96106
_ClassDecorator = _Callable[[type], type]
97107

98108

@@ -119,28 +129,20 @@ def f(cls: type) -> type:
119129

120130
use_for_null = _annotate(null=True)
121131

122-
qltest = _Namespace(
123-
skip=_Pragma("qltest_skip"),
124-
collapse_hierarchy=_Pragma("qltest_collapse_hierarchy"),
125-
uncollapse_hierarchy=_Pragma("qltest_uncollapse_hierarchy"),
126-
)
132+
_Pragma("qltest_skip")
133+
_Pragma("qltest_collapse_hierarchy")
134+
_Pragma("qltest_uncollapse_hierarchy")
127135

128-
ql = _Namespace(
129-
default_doc_name=lambda doc: _annotate(doc_name=doc)
130-
)
136+
ql.default_doc_name = lambda doc: _annotate(doc_name=doc)
131137

132-
cpp = _Namespace(
133-
skip=_Pragma("cpp_skip"),
134-
)
138+
_Pragma("cpp_skip")
135139

136140

137141
def group(name: str = "") -> _ClassDecorator:
138142
return _annotate(group=name)
139143

140144

141-
synth = _Namespace(
142-
from_class=lambda ref: _annotate(ipa=_schema.IpaInfo(
143-
from_class=_schema.get_type_name(ref))),
144-
on_arguments=lambda **kwargs: _annotate(
145-
ipa=_schema.IpaInfo(on_arguments={k: _schema.get_type_name(t) for k, t in kwargs.items()}))
146-
)
145+
synth.from_class = lambda ref: _annotate(ipa=_schema.IpaInfo(
146+
from_class=_schema.get_type_name(ref)))
147+
synth.on_arguments = lambda **kwargs: _annotate(
148+
ipa=_schema.IpaInfo(on_arguments={k: _schema.get_type_name(t) for k, t in kwargs.items()}))

0 commit comments

Comments
 (0)