|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +import enum |
| 4 | +from textwrap import dedent |
| 5 | +from projects.jdwp.codegen.types import python_type_for |
| 6 | +import typing |
| 7 | + |
| 8 | +from projects.jdwp.defs.schema import ( |
| 9 | + Array, |
| 10 | + ArrayLength, |
| 11 | + Command, |
| 12 | + CommandSet, |
| 13 | + Field, |
| 14 | + Struct, |
| 15 | + TaggedUnion, |
| 16 | + UnionTag, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +StructLink = typing.Tuple[Struct, Field, Struct] |
| 21 | + |
| 22 | + |
| 23 | +class StructGenerator: |
| 24 | + def __init__(self, root: Struct, name: str): |
| 25 | + self.__root = root |
| 26 | + self.__struct_to_name = compute_struct_names(root, name) |
| 27 | + |
| 28 | + def __get_python_type_for(self, struct: Struct, field: Field) -> str: |
| 29 | + type = field.type |
| 30 | + match type: |
| 31 | + case Struct(): |
| 32 | + return self.__struct_to_name[type] |
| 33 | + case Array(): |
| 34 | + array_type = typing.cast(Array, type) |
| 35 | + return f"typing.List[{self.__struct_to_name[array_type.element_type]}]" |
| 36 | + case TaggedUnion(): |
| 37 | + tagged_union_type = typing.cast(TaggedUnion, type) |
| 38 | + union_types = [ |
| 39 | + self.__struct_to_name[case_struct] |
| 40 | + for (_, case_struct) in tagged_union_type.cases |
| 41 | + ] |
| 42 | + union_types_str = ", ".join(union_types) |
| 43 | + return f"typing.Union[{union_types_str}]" |
| 44 | + case _: |
| 45 | + return python_type_for(type) |
| 46 | + |
| 47 | + def __is_explicit_field(self, field: Field) -> bool: |
| 48 | + return not isinstance(field.type, (ArrayLength, UnionTag)) |
| 49 | + |
| 50 | + def __get_field_name(self, field: Field) -> str: |
| 51 | + words = field.name.split(" ") |
| 52 | + words = [words[0]] + [word.capitalize() for word in words[1:]] |
| 53 | + return "".join(words) |
| 54 | + |
| 55 | + def __generate_dataclass(self, struct: Struct) -> str: |
| 56 | + name = self.__struct_to_name[struct] |
| 57 | + fields_def = "\n".join( |
| 58 | + f" {self.__get_field_name(field)}: {self.__get_python_type_for(struct, field)}" |
| 59 | + for field in struct.fields |
| 60 | + if self.__is_explicit_field(field) |
| 61 | + ) |
| 62 | + class_def = f"@dataclasses.dataclass(frozen=True)\nclass {name}:\n{fields_def}" |
| 63 | + return dedent(class_def) |
| 64 | + |
| 65 | + def generate(self) -> typing.Generator[str, None, None]: |
| 66 | + for _, _, nested in reversed(list(nested_structs(self.__root))): |
| 67 | + yield self.__generate_dataclass(nested) |
| 68 | + yield self.__generate_dataclass(self.__root) |
| 69 | + |
| 70 | + |
| 71 | +def format_enum_name(enum_value: enum.Enum) -> str: |
| 72 | + words = enum_value.name.split("_") |
| 73 | + formatted_name = "".join(word.capitalize() for word in words) |
| 74 | + return f"{formatted_name}Type" |
| 75 | + |
| 76 | + |
| 77 | +def nested_structs(root: Struct) -> typing.Generator[StructLink, None, None]: |
| 78 | + for field in root.fields: |
| 79 | + field_type = field.type |
| 80 | + match field_type: |
| 81 | + case Array(): |
| 82 | + array_type = typing.cast(Array, field_type) |
| 83 | + yield root, field, array_type.element_type |
| 84 | + yield from nested_structs(array_type.element_type) |
| 85 | + case TaggedUnion(): |
| 86 | + tagged_union_type = typing.cast(TaggedUnion, field_type) |
| 87 | + for _, struct in tagged_union_type.cases: |
| 88 | + yield root, field, struct |
| 89 | + yield from nested_structs(struct) |
| 90 | + case Struct(): |
| 91 | + yield root, field, field_type |
| 92 | + yield from nested_structs(field_type) |
| 93 | + |
| 94 | + |
| 95 | +def compute_struct_names(root: Struct, name: str) -> typing.Mapping[Struct, str]: |
| 96 | + names = {root: name} |
| 97 | + for parent, field, nested in nested_structs(root): |
| 98 | + sanitized_field_name = "".join( |
| 99 | + word.capitalize() for word in field.name.split(" ") |
| 100 | + ) |
| 101 | + type = field.type |
| 102 | + match type: |
| 103 | + case Struct(): |
| 104 | + names[nested] = f"{names[parent]}{sanitized_field_name}" |
| 105 | + case Array(): |
| 106 | + names[nested] = f"{names[parent]}{sanitized_field_name}Element" |
| 107 | + case TaggedUnion(): |
| 108 | + tagged_union_type = typing.cast(TaggedUnion, type) |
| 109 | + for case_value, case_struct in tagged_union_type.cases: |
| 110 | + case_name = format_enum_name(case_value) |
| 111 | + names[ |
| 112 | + case_struct |
| 113 | + ] = f"{names[parent]}{sanitized_field_name}Case{case_name}" |
| 114 | + return names |
| 115 | + |
| 116 | + |
| 117 | +def generate_for_command(command: Command) -> typing.Generator[str, None, None]: |
| 118 | + if command.out: |
| 119 | + yield from StructGenerator(command.out, f"{command.name}Out").generate() |
| 120 | + if command.reply: |
| 121 | + yield from StructGenerator(command.reply, f"{command.name}Reply").generate() |
| 122 | + |
| 123 | + |
| 124 | +def generate_for_command_set( |
| 125 | + command_set: CommandSet, |
| 126 | +) -> typing.Generator[str, None, None]: |
| 127 | + for command in command_set.commands: |
| 128 | + yield from generate_for_command(command) |
| 129 | + |
| 130 | + |
| 131 | +def generate_for_all_command_sets() -> typing.Generator[str, None, None]: |
| 132 | + # TODO: refactor this once PR90 is merged |
| 133 | + from projects.jdwp.defs.command_sets.virtual_machine import VirtualMachine |
| 134 | + from projects.jdwp.defs.command_sets.reference_type import ReferenceType |
| 135 | + from projects.jdwp.defs.command_sets.event_request import EventRequest |
| 136 | + |
| 137 | + yield from generate_for_command_set(VirtualMachine) |
| 138 | + yield from generate_for_command_set(ReferenceType) |
| 139 | + yield from generate_for_command_set(EventRequest) |
| 140 | + |
| 141 | + |
| 142 | +def main(): |
| 143 | + print("import dataclasses") |
| 144 | + print("import typing") |
| 145 | + print("from projects.jdwp.runtime.type_aliases import *") |
| 146 | + |
| 147 | + for struct_definition in generate_for_all_command_sets(): |
| 148 | + print() |
| 149 | + print(struct_definition) |
| 150 | + |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + main() |
0 commit comments