|
8 | 8 | from ..hdl.ast import Shape, ShapeCastable, Const, Signal, Value, ValueCastable
|
9 | 9 | from ..hdl.ir import Elaboratable
|
10 | 10 | from .._utils import final
|
| 11 | +from .meta import Annotation |
11 | 12 |
|
12 | 13 |
|
13 | 14 | __all__ = ["In", "Out", "Signature", "Interface", "connect", "flipped", "Component"]
|
@@ -359,6 +360,10 @@ def members(self, new_members):
|
359 | 360 | if new_members is not self.__members:
|
360 | 361 | raise AttributeError("property 'members' of 'Signature' object cannot be set")
|
361 | 362 |
|
| 363 | + @property |
| 364 | + def annotations(self): |
| 365 | + return () |
| 366 | + |
362 | 367 | def __eq__(self, other):
|
363 | 368 | other_unflipped = other.flip() if type(other) is FlippedSignature else other
|
364 | 369 | if type(self) is type(other_unflipped) is Signature:
|
@@ -891,3 +896,175 @@ def signature(self):
|
891 | 896 | f"Component '{cls.__module__}.{cls.__qualname__}' does not have signature member "
|
892 | 897 | f"annotations")
|
893 | 898 | return signature
|
| 899 | + |
| 900 | + @property |
| 901 | + def metadata(self): |
| 902 | + return ComponentMetadata(self) |
| 903 | + |
| 904 | + |
| 905 | +class ComponentMetadata(Annotation): |
| 906 | + name = "org.amaranth-lang.amaranth.component" |
| 907 | + schema = { |
| 908 | + "$schema": "https://json-schema.org/draft/2020-12/schema", |
| 909 | + "$id": "https://amaranth-lang.org/schema/amaranth/0.5/component.json", |
| 910 | + "type": "object", |
| 911 | + "properties": { |
| 912 | + "interface": { |
| 913 | + "type": "object", |
| 914 | + "properties": { |
| 915 | + "members": { |
| 916 | + "type": "object", |
| 917 | + "patternProperties": { |
| 918 | + "^[A-Za-z][A-Za-z0-9_]*$": { |
| 919 | + "oneOf": [ |
| 920 | + { |
| 921 | + "type": "object", |
| 922 | + "properties": { |
| 923 | + "type": { |
| 924 | + "enum": ["port"], |
| 925 | + }, |
| 926 | + "name": { |
| 927 | + "type": "string", |
| 928 | + "pattern": "^[A-Za-z][A-Za-z0-9_]*$", |
| 929 | + }, |
| 930 | + "dir": { |
| 931 | + "enum": ["in", "out"], |
| 932 | + }, |
| 933 | + "width": { |
| 934 | + "type": "integer", |
| 935 | + "minimum": 0, |
| 936 | + }, |
| 937 | + "signed": { |
| 938 | + "type": "boolean", |
| 939 | + }, |
| 940 | + "reset": { |
| 941 | + "type": "string", |
| 942 | + "pattern": "^[+-]?[0-9]+$", |
| 943 | + }, |
| 944 | + }, |
| 945 | + "additionalProperties": False, |
| 946 | + "required": [ |
| 947 | + "type", |
| 948 | + "name", |
| 949 | + "dir", |
| 950 | + "width", |
| 951 | + "signed", |
| 952 | + "reset", |
| 953 | + ], |
| 954 | + }, |
| 955 | + { |
| 956 | + "type": "object", |
| 957 | + "properties": { |
| 958 | + "type": { |
| 959 | + "enum": ["interface"], |
| 960 | + }, |
| 961 | + "members": { |
| 962 | + "$ref": "#/properties/interface/properties/members", |
| 963 | + }, |
| 964 | + "annotations": { |
| 965 | + "type": "object", |
| 966 | + }, |
| 967 | + }, |
| 968 | + "additionalProperties": False, |
| 969 | + "required": [ |
| 970 | + "type", |
| 971 | + "members", |
| 972 | + "annotations", |
| 973 | + ], |
| 974 | + }, |
| 975 | + ], |
| 976 | + }, |
| 977 | + }, |
| 978 | + "additionalProperties": False, |
| 979 | + }, |
| 980 | + "annotations": { |
| 981 | + "type": "object", |
| 982 | + }, |
| 983 | + }, |
| 984 | + "additionalProperties": False, |
| 985 | + "required": [ |
| 986 | + "members", |
| 987 | + "annotations", |
| 988 | + ], |
| 989 | + }, |
| 990 | + }, |
| 991 | + "additionalProperties": False, |
| 992 | + "required": [ |
| 993 | + "interface", |
| 994 | + ] |
| 995 | + } |
| 996 | + |
| 997 | + """Component metadata. |
| 998 | +
|
| 999 | + A description of the interface and annotations of a :class:`Component`, which can be exported |
| 1000 | + as a JSON object. |
| 1001 | +
|
| 1002 | + Parameters |
| 1003 | + ---------- |
| 1004 | + origin : :class:`Component` |
| 1005 | + The component described by this metadata instance. |
| 1006 | +
|
| 1007 | + Raises |
| 1008 | + ------ |
| 1009 | + :exc:`TypeError` |
| 1010 | + If ``origin`` is not a :class:`Component`. |
| 1011 | + """ |
| 1012 | + def __init__(self, origin): |
| 1013 | + if not isinstance(origin, Component): |
| 1014 | + raise TypeError(f"Origin must be a Component object, not {origin!r}") |
| 1015 | + self._origin = origin |
| 1016 | + |
| 1017 | + @property |
| 1018 | + def origin(self): |
| 1019 | + return self._origin |
| 1020 | + |
| 1021 | + def as_json(self): |
| 1022 | + """Translate to JSON. |
| 1023 | +
|
| 1024 | + Returns |
| 1025 | + ------- |
| 1026 | + :class:`Mapping` |
| 1027 | + A JSON representation of :attr:`ComponentMetadata.origin`, with a hierarchical |
| 1028 | + description of its interface ports and annotations. |
| 1029 | + """ |
| 1030 | + def describe_member(member, *, path): |
| 1031 | + assert isinstance(member, Member) |
| 1032 | + if member.is_port: |
| 1033 | + cast_shape = Shape.cast(member.shape) |
| 1034 | + return { |
| 1035 | + "type": "port", |
| 1036 | + "name": "__".join(path), |
| 1037 | + "dir": "in" if member.flow == In else "out", |
| 1038 | + "width": cast_shape.width, |
| 1039 | + "signed": cast_shape.signed, |
| 1040 | + "reset": str(member._reset_as_const.value), |
| 1041 | + } |
| 1042 | + elif member.is_signature: |
| 1043 | + return { |
| 1044 | + "type": "interface", |
| 1045 | + "members": { |
| 1046 | + name: describe_member(sub_member, path=(*path, name)) |
| 1047 | + for name, sub_member in member.signature.members.items() |
| 1048 | + }, |
| 1049 | + "annotations": { |
| 1050 | + annotation.name: annotation.as_json() |
| 1051 | + for annotation in member.signature.annotations |
| 1052 | + }, |
| 1053 | + } |
| 1054 | + else: |
| 1055 | + assert False # :nocov: |
| 1056 | + |
| 1057 | + instance = { |
| 1058 | + "interface": { |
| 1059 | + "members": { |
| 1060 | + name: describe_member(member, path=(name,)) |
| 1061 | + for name, member in self.origin.signature.members.items() |
| 1062 | + }, |
| 1063 | + "annotations": { |
| 1064 | + annotation.name: annotation.as_json() |
| 1065 | + for annotation in self.origin.signature.annotations |
| 1066 | + }, |
| 1067 | + }, |
| 1068 | + } |
| 1069 | + self.validate(instance) |
| 1070 | + return instance |
0 commit comments