diff --git a/misc/codegen/lib/ql.py b/misc/codegen/lib/ql.py index b9362a556ef..b9d5f9b8027 100644 --- a/misc/codegen/lib/ql.py +++ b/misc/codegen/lib/ql.py @@ -45,6 +45,7 @@ class Property: synth: bool = False type_is_hideable: bool = False type_is_codegen_class: bool = False + type_is_self: bool = False internal: bool = False cfg: bool = False @@ -83,6 +84,10 @@ class Property: def is_indexed(self) -> bool: 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 class Base: @@ -116,11 +121,18 @@ class Class: self.bases_impl = get_bases(self.bases_impl) if self.properties: self.properties[0].first = True + for prop in self.properties: + if prop.type is self.name: + prop.type_is_self = True @property def root(self) -> bool: 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 def path(self) -> pathlib.Path: return self.dir / self.name diff --git a/misc/codegen/templates/ql_class.mustache b/misc/codegen/templates/ql_class.mustache index d39238ff9ba..cb30fc791c7 100644 --- a/misc/codegen/templates/ql_class.mustache +++ b/misc/codegen/templates/ql_class.mustache @@ -9,9 +9,9 @@ private import {{import_prefix}}.Raw {{#imports}} import {{.}} {{/imports}} -{{#root}} +{{#needs_self_alias}} private class {{name}}Alias = {{name}}; -{{/root}} +{{/needs_self_alias}} /** * INTERNAL: This module contains the fully generated definition of `{{name}}` and should not * be referenced directly. @@ -77,7 +77,7 @@ module Generated { * INTERNAL: Do not use. {{/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}} result = Synth::convert{{type}}FromRaw(Synth::convert{{name}}ToRaw(this){{^root}}.(Raw::{{name}}){{/root}}.{{getter}}({{#is_indexed}}index{{/is_indexed}})) {{/synth}} @@ -95,7 +95,7 @@ module Generated { * INTERNAL: Do not use. {{/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 {{#hideable}}if exists(this.getResolveStep()) then result = immediate else {{/hideable}}result = immediate.resolve()) } @@ -111,7 +111,7 @@ module Generated { * INTERNAL: Do not use. {{/internal}} */ - {{type}} {{getter}}({{#is_indexed}}int index{{/is_indexed}}) { + {{type_alias}} {{getter}}({{#is_indexed}}int index{{/is_indexed}}) { {{^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}} {{/synth}} @@ -140,7 +140,7 @@ module Generated { * INTERNAL: Do not use. {{/internal}} */ - final {{type}} {{indefinite_getter}}() { + final {{type_alias}} {{indefinite_getter}}() { result = this.{{getter}}(_) } {{^is_optional}}