Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for --exclude-declaration to python and cxx generator #102

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 7 additions & 24 deletions pdl-compiler/scripts/generate_cxx_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1658,7 +1658,7 @@ class {struct_name} : public pdl::packet::Builder {{


def run(input: argparse.FileType, output: argparse.FileType, namespace: Optional[str], include_header: List[str],
using_namespace: List[str]):
using_namespace: List[str], exclude_declaration: List[str]):

file = ast.File.from_json(json.load(input))
core.desugar(file)
Expand All @@ -1668,28 +1668,6 @@ def run(input: argparse.FileType, output: argparse.FileType, namespace: Optional
open_namespace = f"namespace {namespace} {{" if namespace else ""
close_namespace = f"}} // {namespace}" if namespace else ""

# Disable unsupported features in the canonical test suite.
skipped_decls = [
'Packet_Custom_Field_ConstantSize',
'Packet_Custom_Field_VariableSize',
'Packet_Checksum_Field_FromStart',
'Packet_Checksum_Field_FromEnd',
'Struct_Custom_Field_ConstantSize',
'Struct_Custom_Field_VariableSize',
'Struct_Checksum_Field_FromStart',
'Struct_Checksum_Field_FromEnd',
'Struct_Custom_Field_ConstantSize_',
'Struct_Custom_Field_VariableSize_',
'Struct_Checksum_Field_FromStart_',
'Struct_Checksum_Field_FromEnd_',
'PartialParent5',
'PartialChild5_A',
'PartialChild5_B',
'PartialParent12',
'PartialChild12_A',
'PartialChild12_B',
]

output.write(
dedent("""\
// File generated from {input_name}, with the command:
Expand Down Expand Up @@ -1732,7 +1710,7 @@ def run(input: argparse.FileType, output: argparse.FileType, namespace: Optional
output.write(f"class {d.id}View;\n")

for d in file.declarations:
if d.id in skipped_decls:
if d.id in exclude_declaration:
continue

if isinstance(d, ast.EnumDeclaration):
Expand All @@ -1759,6 +1737,11 @@ def main() -> int:
default=[],
action='append',
help='Added using namespace statements')
parser.add_argument('--exclude-declaration',
type=str,
default=[],
action='append',
help='Exclude declaration from the generated output')
return run(**vars(parser.parse_args()))


Expand Down
13 changes: 12 additions & 1 deletion pdl-compiler/scripts/generate_python_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,13 +1157,16 @@ def generate_checksum_declaration_check(decl: ast.ChecksumDeclaration) -> str:
""").format(checksum_name=decl.id)


def run(input: argparse.FileType, output: argparse.FileType, custom_type_location: Optional[str]):
def run(input: argparse.FileType, output: argparse.FileType, custom_type_location: Optional[str], exclude_declaration: List[str]):
file = ast.File.from_json(json.load(input))
core.desugar(file)

custom_types = []
custom_type_checks = ""
for d in file.declarations:
if d.id in exclude_declaration:
continue

if isinstance(d, ast.CustomFieldDeclaration):
custom_types.append(d.id)
custom_type_checks += generate_custom_field_declaration_check(d)
Expand All @@ -1180,6 +1183,9 @@ def run(input: argparse.FileType, output: argparse.FileType, custom_type_locatio
output.write(custom_type_checks)

for d in file.declarations:
if d.id in exclude_declaration:
continue

if isinstance(d, ast.EnumDeclaration):
output.write(generate_enum_declaration(d))
elif isinstance(d, (ast.PacketDeclaration, ast.StructDeclaration)):
Expand All @@ -1195,6 +1201,11 @@ def main() -> int:
type=str,
required=False,
help='Module of declaration of custom types')
parser.add_argument('--exclude-declaration',
type=str,
default=[],
action='append',
help='Exclude declaration from the generated output')
return run(**vars(parser.parse_args()))


Expand Down
9 changes: 4 additions & 5 deletions pdl-compiler/tests/python_generator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import enum
import json
import typing
import typing_extensions
import unittest
from importlib import resources

Expand Down Expand Up @@ -57,12 +56,12 @@ def create_object(typ, value):
field_type = field_types[f]
values[f] = create_object(field_type, v)
return typ(**values)
elif typing_extensions.get_origin(typ) is list:
typ = typing_extensions.get_args(typ)[0]
elif typing.get_origin(typ) is list:
typ = typing.get_args(typ)[0]
return [create_object(typ, v) for v in value]
elif typing_extensions.get_origin(typ) is typing.Union:
elif typing.get_origin(typ) is typing.Union:
# typing.Optional[int] expands to typing.Union[int, None]
typ = typing_extensions.get_args(typ)[0]
typ = typing.get_args(typ)[0]
return create_object(typ, value) if value is not None else None
elif typ is bytes:
return bytes(value)
Expand Down
36 changes: 36 additions & 0 deletions pdl-compiler/tests/run_cxx_generator_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,46 @@ pdlc "$OUT_DIR"/be_test_file.pdl > "$OUT_DIR"/be_test_file.json
python3 scripts/generate_cxx_backend.py \
--input "$OUT_DIR"/le_test_file.json \
--output "$OUT_DIR"/le_backend.h \
--exclude-declaration Packet_Custom_Field_ConstantSize \
--exclude-declaration Packet_Custom_Field_VariableSize \
--exclude-declaration Packet_Checksum_Field_FromStart \
--exclude-declaration Packet_Checksum_Field_FromEnd \
--exclude-declaration Struct_Custom_Field_ConstantSize \
--exclude-declaration Struct_Custom_Field_VariableSize \
--exclude-declaration Struct_Checksum_Field_FromStart \
--exclude-declaration Struct_Checksum_Field_FromEnd \
--exclude-declaration Struct_Custom_Field_ConstantSize_ \
--exclude-declaration Struct_Custom_Field_VariableSize_ \
--exclude-declaration Struct_Checksum_Field_FromStart_ \
--exclude-declaration Struct_Checksum_Field_FromEnd_ \
--exclude-declaration PartialParent5 \
--exclude-declaration PartialChild5_A \
--exclude-declaration PartialChild5_B \
--exclude-declaration PartialParent12 \
--exclude-declaration PartialChild12_A \
--exclude-declaration PartialChild12_B \
--namespace le_backend
python3 scripts/generate_cxx_backend.py \
--input "$OUT_DIR"/be_test_file.json \
--output "$OUT_DIR"/be_backend.h \
--exclude-declaration Packet_Custom_Field_ConstantSize \
--exclude-declaration Packet_Custom_Field_VariableSize \
--exclude-declaration Packet_Checksum_Field_FromStart \
--exclude-declaration Packet_Checksum_Field_FromEnd \
--exclude-declaration Struct_Custom_Field_ConstantSize \
--exclude-declaration Struct_Custom_Field_VariableSize \
--exclude-declaration Struct_Checksum_Field_FromStart \
--exclude-declaration Struct_Checksum_Field_FromEnd \
--exclude-declaration Struct_Custom_Field_ConstantSize_ \
--exclude-declaration Struct_Custom_Field_VariableSize_ \
--exclude-declaration Struct_Checksum_Field_FromStart_ \
--exclude-declaration Struct_Checksum_Field_FromEnd_ \
--exclude-declaration PartialParent5 \
--exclude-declaration PartialChild5_A \
--exclude-declaration PartialChild5_B \
--exclude-declaration PartialParent12 \
--exclude-declaration PartialChild12_A \
--exclude-declaration PartialChild12_B \
--namespace be_backend

python3 scripts/generate_cxx_backend_tests.py \
Expand Down
Loading