20
20
from collections .abc import Sequence
21
21
import dataclasses
22
22
import subprocess
23
+ from typing import Optional
23
24
24
25
from absl import app
25
26
from absl import flags
@@ -90,7 +91,7 @@ class XlsParam:
90
91
name : str
91
92
packed_type : str
92
93
unpacked_type : str
93
- specialized_type : str | None
94
+ specialized_type : Optional [ str ]
94
95
95
96
@property
96
97
def value_arg (self ):
@@ -135,41 +136,46 @@ def params_and_result(self):
135
136
136
137
137
138
def to_packed (t : type_pb2 .TypeProto ) -> str :
138
- match t .type_enum :
139
- case type_pb2 .TypeProto .BITS :
140
- return f"xls::PackedBitsView<{ t .bit_count } >"
141
- case type_pb2 .TypeProto .TUPLE :
142
- inner = ", " .join (to_packed (e ) for e in t .tuple_elements )
143
- return f"xls::PackedTupleView<{ inner } >"
144
- case type_pb2 .TypeProto .ARRAY :
145
- return (
146
- f"xls::PackedArrayView<{ to_packed (t .array_element )} , { t .array_size } >"
147
- )
148
- case _:
149
- raise app .UsageError (
150
- "Incompatible with argument of type:"
151
- f" { type_pb2 .TypeProto .TypeEnum .Name (t .type_enum )} "
152
- )
139
+ the_type = t .type_enum
140
+ if the_type == type_pb2 .TypeProto .BITS :
141
+ return f"xls::PackedBitsView<{ t .bit_count } >"
142
+ elif the_type == type_pb2 .TypeProto .TUPLE :
143
+ inner = ", " .join (to_packed (e ) for e in t .tuple_elements )
144
+ return f"xls::PackedTupleView<{ inner } >"
145
+ elif the_type == type_pb2 .TypeProto .ARRAY :
146
+ return f"xls::PackedArrayView<{ to_packed (t .array_element )} , { t .array_size } >"
147
+ raise app .UsageError (
148
+ "Incompatible with argument of type:"
149
+ f" { type_pb2 .TypeProto .TypeEnum .Name (t .type_enum )} "
150
+ )
153
151
154
152
155
153
def to_unpacked (t : type_pb2 .TypeProto , mutable : bool = False ) -> str :
154
+ """Get the unpacked c++ view type.
155
+
156
+ Args:
157
+ t: The xls type
158
+ mutable: Whether the type is mutable.
159
+
160
+ Returns:
161
+ the C++ unpacked view type
162
+ """
156
163
mutable_str = "Mutable" if mutable else ""
157
- match t .type_enum :
158
- case type_pb2 .TypeProto .BITS :
159
- return f"xls::{ mutable_str } BitsView<{ t .bit_count } >"
160
- case type_pb2 .TypeProto .TUPLE :
161
- inner = ", " .join (to_unpacked (e , mutable ) for e in t .tuple_elements )
162
- return f"xls::{ mutable_str } TupleView<{ inner } >"
163
- case type_pb2 .TypeProto .ARRAY :
164
- return (
165
- f"xls::{ mutable_str } ArrayView<{ to_unpacked (t .array_element , mutable )} ,"
166
- f" { t .array_size } >"
167
- )
168
- case _:
169
- raise app .UsageError (
170
- "Incompatible with argument of type:"
171
- f" { type_pb2 .TypeProto .TypeEnum .Name (t .type_enum )} "
172
- )
164
+ the_type = t .type_enum
165
+ if the_type == type_pb2 .TypeProto .BITS :
166
+ return f"xls::{ mutable_str } BitsView<{ t .bit_count } >"
167
+ elif the_type == type_pb2 .TypeProto .TUPLE :
168
+ inner = ", " .join (to_unpacked (e , mutable ) for e in t .tuple_elements )
169
+ return f"xls::{ mutable_str } TupleView<{ inner } >"
170
+ elif the_type == type_pb2 .TypeProto .ARRAY :
171
+ return (
172
+ f"xls::{ mutable_str } ArrayView<{ to_unpacked (t .array_element , mutable )} ,"
173
+ f" { t .array_size } >"
174
+ )
175
+ raise app .UsageError (
176
+ "Incompatible with argument of type:"
177
+ f" { type_pb2 .TypeProto .TypeEnum .Name (t .type_enum )} "
178
+ )
173
179
174
180
175
181
def is_floating_point (
@@ -195,23 +201,30 @@ def is_float_tuple(t: type_pb2.TypeProto) -> bool:
195
201
return is_floating_point (t , 8 , 23 )
196
202
197
203
198
- def to_specialized (t : type_pb2 .TypeProto ) -> str | None :
199
- match t .type_enum :
200
- case type_pb2 .TypeProto .BITS :
201
- if t .bit_count <= 8 :
202
- return "uint8_t"
203
- elif t .bit_count <= 16 :
204
- return "uint16_t"
205
- elif t .bit_count <= 32 :
206
- return "uint32_t"
207
- elif t .bit_count <= 64 :
208
- return "uint64_t"
209
- case type_pb2 .TypeProto .TUPLE if is_double_tuple (t ):
210
- return "double"
211
- case type_pb2 .TypeProto .TUPLE if is_float_tuple (t ):
212
- return "float"
213
- case _:
214
- return None
204
+ def to_specialized (t : type_pb2 .TypeProto ) -> Optional [str ]:
205
+ """Get the specialized c++ type.
206
+
207
+ Args:
208
+ t: The xls type
209
+
210
+ Returns:
211
+ the C++ type
212
+ """
213
+ the_type = t .type_enum
214
+ if the_type == type_pb2 .TypeProto .BITS :
215
+ if t .bit_count <= 8 :
216
+ return "uint8_t"
217
+ elif t .bit_count <= 16 :
218
+ return "uint16_t"
219
+ elif t .bit_count <= 32 :
220
+ return "uint32_t"
221
+ elif t .bit_count <= 64 :
222
+ return "uint64_t"
223
+ elif is_double_tuple (t ):
224
+ return "double"
225
+ elif is_float_tuple (t ):
226
+ return "float"
227
+ return None
215
228
216
229
217
230
def to_param (p : ir_interface_pb2 .PackageInterfaceProto .NamedValue ) -> XlsParam :
0 commit comments