diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 76ef9d26..6b009bf7 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -232,3 +232,6 @@ non-constant expressions (fixes SIMICS-13113). - `release 6 6363` - `release 7 7064` +- `note 6` Added the `stringify_list` provisional feature, which extends + `stringify` to support compile-time lists. This is primarily meant to provide + a means of expressing lists within documentation strings. diff --git a/py/dml/codegen.py b/py/dml/codegen.py index f5b32794..30ce45fe 100644 --- a/py/dml/codegen.py +++ b/py/dml/codegen.py @@ -12,7 +12,7 @@ import math from . import objects, crep, ctree, ast, int_register, logging, serialize -from . import dmlparse, output, compat +from . import dmlparse, output, compat, provisional from .logging import * from .expr import * from .ctree import * @@ -1138,6 +1138,13 @@ def expr_unop(tree, location, scope): if not func.independent: mark_method_statically_exported(func) return ctree.AddressOfMethod(tree.site, func) + if (tree.site.provisional_enabled(provisional.stringify_list) + and op == 'stringify' and isinstance(rh, List)): + if not rh.constant: + raise ENCONST(rh, rh) + + return mkStringConstant(tree.site, str(rh)) + raise rh.exc() if op == '!': if compat.dml12_not in dml.globals.enabled_compat: diff --git a/py/dml/provisional.py b/py/dml/provisional.py index 06dc6788..c060226b 100644 --- a/py/dml/provisional.py +++ b/py/dml/provisional.py @@ -71,6 +71,29 @@ class explicit_param_decls(ProvisionalFeature): short = "Require := syntax for defining new params" stable = True +@feature +class stringify_list(ProvisionalFeature): + '''This feature extends upon `stringify`, making it able to stringify + compile-time lists that feature stringifiable items. As with all other + valid uses of `stringify`, the resulting string will be considered + constant. + + This is primarily meant as a stop-gap solution for expressing lists of + items in documentation strings, which would not be possible otherwise as + e.g. the `documentation` parameter requires the definition to be a constant + string. `stringify_list` is provisional rather than built-in as it's a + sub-par solution to the problem, while also making `stringify` more open to + abuse. + + Don't take advantage of this feature so that you may use `stringify` as a + means of determining whether or not something is a compile-time list, as + that is not considered a valid, supported use-case. You are only meant + to use `stringify` to produce user-facing strings (e.g. for documentation + or logging), or for anonymization (the `name` param). + ''' + short = "Extend 'stringify' to support compile-time lists" + stable = True + def parse_provisional( provs: list[("Site", str)]) -> dict[ProvisionalFeature, "Site"]: ret = {} diff --git a/test/1.4/provisional/T_stringify_list.dml b/test/1.4/provisional/T_stringify_list.dml new file mode 100644 index 00000000..65393f55 --- /dev/null +++ b/test/1.4/provisional/T_stringify_list.dml @@ -0,0 +1,15 @@ +/* + © 2025 Intel Corporation + SPDX-License-Identifier: MPL-2.0 +*/ + +dml 1.4; + +provisional stringify_list; + +device test; + +/// COMPILE-ONLY + +#if (stringify([["a",0],["b",1],["c",false]]) + != "[[\"a\",0],[\"b\",1],[\"c\",false]]") { error; } diff --git a/test/1.4/provisional/T_stringify_list_ENCONST.dml b/test/1.4/provisional/T_stringify_list_ENCONST.dml new file mode 100644 index 00000000..b05b0dbc --- /dev/null +++ b/test/1.4/provisional/T_stringify_list_ENCONST.dml @@ -0,0 +1,15 @@ +/* + © 2025 Intel Corporation + SPDX-License-Identifier: MPL-2.0 +*/ + +dml 1.4; + +provisional stringify_list; + +device test; + +/// COMPILE-ONLY + +/// ERROR ENCONST +param p = stringify([["a",0],["b",1],["c",dev]]);