| 253 | # Convert a JIT default into C++ expression representing the default |
| 254 | # flake8: noqa: C901 |
| 255 | def default_expr(d: str, t: Type) -> str: |
| 256 | if d == "None" and str(t) == "Tensor?": |
| 257 | return "{}" |
| 258 | if isinstance(t, BaseType) and t.name is BaseTy.str: |
| 259 | # Schema allows single quotes but C++ needs double |
| 260 | if len(d) >= 2 and d[0] == "'" and d[-1] == "'": |
| 261 | s = "" |
| 262 | i = 1 |
| 263 | while i + 1 < len(d): |
| 264 | if d[i] != "\\": |
| 265 | if d[i] == '"': |
| 266 | s += '\\"' |
| 267 | else: |
| 268 | s += d[i] |
| 269 | i += 1 |
| 270 | else: |
| 271 | if d[i + 1] == "'": |
| 272 | s += "'" |
| 273 | else: |
| 274 | s += d[i : i + 2] |
| 275 | i += 2 |
| 276 | |
| 277 | return f'"{s}"' |
| 278 | |
| 279 | if isinstance(t, OptionalType): |
| 280 | if d == "None": |
| 281 | return "torch::executor::nullopt" |
| 282 | |
| 283 | return default_expr(d, t.elem) |
| 284 | |
| 285 | if isinstance(t, ListType): |
| 286 | if d.startswith("[") and d.endswith("]"): |
| 287 | return "{" + d[1:-1] + "}" |
| 288 | elif t.size is None: |
| 289 | # NOTE: Sized lists can have scalar defaults |
| 290 | raise ValueError(f"Expected a list default '[...]' but found: '{d}'") |
| 291 | |
| 292 | return JIT_TO_CPP_DEFAULT.get(d, d) |
| 293 | |
| 294 | |
| 295 | # Convert an argument into its C++ API form |