Codegen: Improve return type of self-typed properties

This commit is contained in:
Tom Hvitved
2025-01-30 13:47:58 +01:00
parent 180782d863
commit ec33457ad4
2 changed files with 18 additions and 6 deletions

View File

@@ -45,6 +45,7 @@ class Property:
synth: bool = False synth: bool = False
type_is_hideable: bool = False type_is_hideable: bool = False
type_is_codegen_class: bool = False type_is_codegen_class: bool = False
type_is_self: bool = False
internal: bool = False internal: bool = False
cfg: bool = False cfg: bool = False
@@ -83,6 +84,10 @@ class Property:
def is_indexed(self) -> bool: def is_indexed(self) -> bool:
return self.is_repeated and not self.is_unordered return self.is_repeated and not self.is_unordered
@property
def type_alias(self) -> Optional[str]:
return self.type + "Alias" if self.type_is_self else self.type
@dataclass @dataclass
class Base: class Base:
@@ -116,11 +121,18 @@ class Class:
self.bases_impl = get_bases(self.bases_impl) self.bases_impl = get_bases(self.bases_impl)
if self.properties: if self.properties:
self.properties[0].first = True self.properties[0].first = True
for prop in self.properties:
if prop.type is self.name:
prop.type_is_self = True
@property @property
def root(self) -> bool: def root(self) -> bool:
return not self.bases return not self.bases
@property
def needs_self_alias(self) -> bool:
return self.root or any(p.type_is_self for p in self.properties)
@property @property
def path(self) -> pathlib.Path: def path(self) -> pathlib.Path:
return self.dir / self.name return self.dir / self.name

View File

@@ -9,9 +9,9 @@ private import {{import_prefix}}.Raw
{{#imports}} {{#imports}}
import {{.}} import {{.}}
{{/imports}} {{/imports}}
{{#root}} {{#needs_self_alias}}
private class {{name}}Alias = {{name}}; private class {{name}}Alias = {{name}};
{{/root}} {{/needs_self_alias}}
/** /**
* INTERNAL: This module contains the fully generated definition of `{{name}}` and should not * INTERNAL: This module contains the fully generated definition of `{{name}}` and should not
* be referenced directly. * be referenced directly.
@@ -77,7 +77,7 @@ module Generated {
* INTERNAL: Do not use. * INTERNAL: Do not use.
{{/internal}} {{/internal}}
*/ */
{{type}} get{{#is_unordered}}An{{/is_unordered}}Immediate{{singular}}({{#is_indexed}}int index{{/is_indexed}}) { {{type_alias}} get{{#is_unordered}}An{{/is_unordered}}Immediate{{singular}}({{#is_indexed}}int index{{/is_indexed}}) {
{{^synth}} {{^synth}}
result = Synth::convert{{type}}FromRaw(Synth::convert{{name}}ToRaw(this){{^root}}.(Raw::{{name}}){{/root}}.{{getter}}({{#is_indexed}}index{{/is_indexed}})) result = Synth::convert{{type}}FromRaw(Synth::convert{{name}}ToRaw(this){{^root}}.(Raw::{{name}}){{/root}}.{{getter}}({{#is_indexed}}index{{/is_indexed}}))
{{/synth}} {{/synth}}
@@ -95,7 +95,7 @@ module Generated {
* INTERNAL: Do not use. * INTERNAL: Do not use.
{{/internal}} {{/internal}}
*/ */
final {{type}} {{getter}}({{#is_indexed}}int index{{/is_indexed}}) { final {{type_alias}} {{getter}}({{#is_indexed}}int index{{/is_indexed}}) {
exists({{type}} immediate | immediate = this.get{{#is_unordered}}An{{/is_unordered}}Immediate{{singular}}({{#is_indexed}}index{{/is_indexed}}) and exists({{type}} immediate | immediate = this.get{{#is_unordered}}An{{/is_unordered}}Immediate{{singular}}({{#is_indexed}}index{{/is_indexed}}) and
{{#hideable}}if exists(this.getResolveStep()) then result = immediate else {{/hideable}}result = immediate.resolve()) {{#hideable}}if exists(this.getResolveStep()) then result = immediate else {{/hideable}}result = immediate.resolve())
} }
@@ -111,7 +111,7 @@ module Generated {
* INTERNAL: Do not use. * INTERNAL: Do not use.
{{/internal}} {{/internal}}
*/ */
{{type}} {{getter}}({{#is_indexed}}int index{{/is_indexed}}) { {{type_alias}} {{getter}}({{#is_indexed}}int index{{/is_indexed}}) {
{{^synth}} {{^synth}}
{{^is_predicate}}result = {{/is_predicate}}{{#type_is_codegen_class}}Synth::convert{{type}}FromRaw({{/type_is_codegen_class}}Synth::convert{{name}}ToRaw(this){{^root}}.(Raw::{{name}}){{/root}}.{{getter}}({{#is_indexed}}index{{/is_indexed}}){{#type_is_codegen_class}}){{/type_is_codegen_class}} {{^is_predicate}}result = {{/is_predicate}}{{#type_is_codegen_class}}Synth::convert{{type}}FromRaw({{/type_is_codegen_class}}Synth::convert{{name}}ToRaw(this){{^root}}.(Raw::{{name}}){{/root}}.{{getter}}({{#is_indexed}}index{{/is_indexed}}){{#type_is_codegen_class}}){{/type_is_codegen_class}}
{{/synth}} {{/synth}}
@@ -140,7 +140,7 @@ module Generated {
* INTERNAL: Do not use. * INTERNAL: Do not use.
{{/internal}} {{/internal}}
*/ */
final {{type}} {{indefinite_getter}}() { final {{type_alias}} {{indefinite_getter}}() {
result = this.{{getter}}(_) result = this.{{getter}}(_)
} }
{{^is_optional}} {{^is_optional}}