Skip to content

Commit

Permalink
Simplify code generation caused by generic buffer type
Browse files Browse the repository at this point in the history
Ref. #659
  • Loading branch information
treiher authored and Isabell Zorr committed Jun 29, 2021
1 parent 354a1a6 commit f2e0aeb
Show file tree
Hide file tree
Showing 76 changed files with 15,548 additions and 14,492 deletions.
12 changes: 9 additions & 3 deletions rflx/ada.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,9 +943,6 @@ def __init__(
self.generic_identifier = ID(generic_identifier)
self.associations = list(map(str, associations or []))

def __hash__(self) -> int:
return hash(self.identifier)

def __str__(self) -> str:
associations = ", ".join(map(str, self.associations)) if self.associations else "<>"
return f"with package {self.identifier} is new {self.generic_identifier} ({associations});"
Expand Down Expand Up @@ -1012,6 +1009,15 @@ def __str__(self) -> str:
return f"package {self.identifier} is new {self.generic_package}{associations};\n"


class PackageRenamingDeclaration(Declaration):
def __init__(self, identifier: StrID, package_identifier: StrID) -> None:
self.identifier = ID(identifier)
self.package_identifier = ID(package_identifier)

def __str__(self) -> str:
return f"package {self.identifier} renames {self.package_identifier};"


class ObjectDeclaration(Declaration):
def __init__(
self,
Expand Down
37 changes: 1 addition & 36 deletions rflx/generator/common.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Callable, List, Mapping, Optional, Sequence, Tuple

from rflx import ada, expression as expr, model
from rflx.common import unique
from rflx.const import BUILTINS_PACKAGE

from . import const
Expand Down Expand Up @@ -637,10 +636,6 @@ def prefixed_type_identifier(type_identifier: ada.ID, prefix: str) -> ada.ID:
return prefix * type_identifier


def generic_name(identifier: ada.ID) -> ada.ID:
return ada.ID([*identifier.parts[:-1], f"Generic_{identifier.parts[-1]}"])


def base_type_name(scalar_type: model.Scalar) -> ada.ID:
if isinstance(scalar_type, model.ModularInteger):
return ada.ID(scalar_type.name)
Expand All @@ -667,7 +662,7 @@ def full_enum_name(enum_type: model.Enumeration) -> ada.ID:


def sequence_name(message: model.Message, field: model.Field) -> ada.ID:
return ada.ID(message.types[field].name + "_Sequence")
return ada.ID(message.types[field].identifier)


def size_dependent_condition(message: model.Message) -> bool:
Expand All @@ -679,36 +674,8 @@ def size_dependent_condition(message: model.Message) -> bool:
)


def create_message_instantiation(
message: model.Message, types_package: ada.ID, prefix: str = ""
) -> Tuple[List[ada.ContextItem], ada.GenericPackageInstantiation]:
if isinstance(message, model.DerivedMessage):
name = generic_name(prefix * ada.ID(message.base.identifier))
else:
name = generic_name(prefix * ada.ID(message.identifier))

context: List[ada.ContextItem] = [
ada.WithClause(name),
]

sequences = list(
unique(
prefix * ada.ID(t.identifier)
for t in message.types.values()
if isinstance(t, model.Sequence)
)
)
context.extend(ada.WithClause(sequence) for sequence in sequences)
package = ada.GenericPackageInstantiation(
prefix * ada.ID(message.identifier), name, [types_package] + sequences
)

return (context, package)


def create_sequence_instantiation(
sequence_type: model.Sequence,
types_package: ada.ID,
prefix: str = "",
flat: bool = False,
) -> Tuple[List[ada.ContextItem], ada.GenericPackageInstantiation]:
Expand All @@ -729,7 +696,6 @@ def create_sequence_instantiation(
ada.ID(sequence_type.identifier.flat if flat else prefix * sequence_type.identifier),
prefix * const.MESSAGE_SEQUENCE_PACKAGE,
[
types_package,
element_type_identifier * "Context",
element_type_identifier * "Initialize",
element_type_identifier * "Take_Buffer",
Expand All @@ -749,7 +715,6 @@ def create_sequence_instantiation(
ada.ID(sequence_type.identifier.flat if flat else prefix * sequence_type.identifier),
prefix * const.SCALAR_SEQUENCE_PACKAGE,
[
types_package,
element_type_identifier,
prefix
* element_type_package
Expand Down
2 changes: 1 addition & 1 deletion rflx/generator/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

TEMPLATE_DIR = ("rflx", "templates/")

TYPES = ada.ID("Types")
TYPES = TYPES_PACKAGE
TYPES_BYTE = TYPES * "Byte"
TYPES_BYTES = TYPES * "Bytes"
TYPES_BYTES_PTR = TYPES * "Bytes_Ptr"
Expand Down
Loading

0 comments on commit f2e0aeb

Please sign in to comment.