From 85b9100d39fcecbfadcbbb815ebbd854e4cd7483 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 00:27:53 +0800 Subject: [PATCH 01/27] Resolve empty timeframe arguments to the chart period --- CHANGELOG.md | 4 + pineforge_codegen/codegen/helpers.py | 1 + pineforge_codegen/codegen/visit_call.py | 39 ++-- tests/fixtures/empty_timeframe/arguments.pine | 186 +++++++++++++++ .../empty_timeframe/reporter-change.pine | 10 + .../empty_timeframe/reporter-seconds.pine | 10 + tests/test_e2e_empty_timeframe.py | 219 ++++++++++++++++++ tests/test_e2e_temporary_name_hygiene.py | 10 +- 8 files changed, 464 insertions(+), 15 deletions(-) create mode 100644 tests/fixtures/empty_timeframe/arguments.pine create mode 100644 tests/fixtures/empty_timeframe/reporter-change.pine create mode 100644 tests/fixtures/empty_timeframe/reporter-seconds.pine create mode 100644 tests/test_e2e_empty_timeframe.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 83af3833..bd73cb2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,10 @@ supported as exact pairs; on the 0.x line they are independent. See the ## Unreleased (next release) +- `timeframe.in_seconds` and `timeframe.change` resolve an evaluated empty + string to the chart period, including input, variable, function-return and + named arguments. The argument is evaluated once, and nonempty strings keep + their explicit period. (#394) - A zone name the engine rejects now makes calendar-form `timestamp()` stop the run at its first evaluation, instead of silently calculating a UTC value. The paired engine reports a typed timezone error identifying the failed bar. diff --git a/pineforge_codegen/codegen/helpers.py b/pineforge_codegen/codegen/helpers.py index f292996f..aacef1e4 100644 --- a/pineforge_codegen/codegen/helpers.py +++ b/pineforge_codegen/codegen/helpers.py @@ -533,6 +533,7 @@ def cpp_code_only(cpp: str) -> str: _pf_series_raw _pf_raw _pf_val_t _pf_out _pf_prior _pf_hist_offset_raw _pf_hist_offset_numeric _pf_hist_offset _pf_hist_bar _pf_calendar_ts _pf_calendar_tz + _pf_timeframe_arg _tz _yr _mo _dy _hr _min _sc _res _old _old_tz _had_old _secs _last_tz _last_yr _last_mo _last_dy _last_hr _last_min _last_sc _last_res diff --git a/pineforge_codegen/codegen/visit_call.py b/pineforge_codegen/codegen/visit_call.py index efb19c5c..fb34cb61 100644 --- a/pineforge_codegen/codegen/visit_call.py +++ b/pineforge_codegen/codegen/visit_call.py @@ -3211,22 +3211,35 @@ def _visit_func_call(self, node: FuncCall) -> str: return f'{log_funcs[func_name]}({self._visit_expr(message)})' return '"" /* unsupported log */' - # timeframe.* calls (e.g., timeframe.change) — not supported in single-TF backtest + # An empty timeframe argument denotes the chart period, whether it + # comes from a literal, an input or a user call. Bind by parameter + # name and resolve the evaluated string, not the argument's AST form. if namespace == "timeframe": - if func_name == "change": - tf_arg = self._visit_expr(node.args[0]) if node.args else 'script_tf_' - # Symbol-clock overload: D/W/M boundaries fall on the SYMBOL's - # session day (17:00 ET on OANDA forex, 09:30 ET RTH on - # equities), intraday buckets on the session-open grid — the - # same clock request.security aggregation uses. UTC/24x7 - # syminfo reduces to the tz-less epoch math bit-for-bit. + if func_name in ("change", "in_seconds"): + argument = self._resolve_func_args( + node, f"timeframe.{func_name}", + ).get("timeframe") + tf_arg = ( + "script_tf_" if argument is None else + "(_pf_timeframe_arg.empty() ? script_tf_ : _pf_timeframe_arg)" + ) + if func_name == "change": + # Keep the symbol-clock overload for calendar and + # session-anchored intraday boundaries. + call = ( + f'tf_change(prev_bar_timestamp_, current_bar_.timestamp, {tf_arg}, ' + 'syminfo_.timezone, syminfo_.session)' + ) + else: + call = f'tf_to_seconds({tf_arg})' + if argument is None: + return call + # A function-return argument can carry state. Evaluate it + # once before testing emptiness and passing the chosen value. return ( - f'tf_change(prev_bar_timestamp_, current_bar_.timestamp, {tf_arg}, ' - 'syminfo_.timezone, syminfo_.session)' + '([&]() { const std::string _pf_timeframe_arg = ' + f'({self._visit_expr(argument)}); return {call}; }}())' ) - if func_name == "in_seconds": - tf_arg = self._visit_expr(node.args[0]) if node.args else 'script_tf_' - return f'tf_to_seconds({tf_arg})' # Defensive: support_checker.NOT_YET_FUNC should already have rejected # any unhandled timeframe.* call. Reaching here implies the checker was # bypassed. diff --git a/tests/fixtures/empty_timeframe/arguments.pine b/tests/fixtures/empty_timeframe/arguments.pine new file mode 100644 index 00000000..99afec55 --- /dev/null +++ b/tests/fixtures/empty_timeframe/arguments.pine @@ -0,0 +1,186 @@ +//@version=6 +strategy("Empty timeframe arguments", currency = currency.NONE) +frameInput = input.timeframe("", "Frame input") +frameString = input.string("", "String input") +frameVariable = frameString +_pf_timeframe_arg = frameString +pf_safe__pf_timeframe_arg = "240" +plainVariable = "" +var string persistentFrame = "" +emptyFrame() => "" +returnedFrame(string frameArg) => frameArg +secondsPos(string frameArg) => timeframe.in_seconds(frameArg) +secondsNamed(string frameArg) => timeframe.in_seconds(timeframe = frameArg) +changePos(string frameArg) => timeframe.change(frameArg) +changeNamed(string frameArg) => timeframe.change(timeframe = frameArg) +secondsCapturedPos(string _pf_timeframe_arg) => timeframe.in_seconds(_pf_timeframe_arg) +secondsCapturedNamed(string _pf_timeframe_arg) => timeframe.in_seconds(timeframe = _pf_timeframe_arg) +changeCapturedPos(string _pf_timeframe_arg) => timeframe.change(_pf_timeframe_arg) +changeCapturedNamed(string _pf_timeframe_arg) => timeframe.change(timeframe = _pf_timeframe_arg) +secondsStatefulPos(string frameArg) => + var string savedFrame = frameArg + timeframe.in_seconds(savedFrame) +secondsStatefulNamed(string frameArg) => + var string savedFrame = frameArg + timeframe.in_seconds(timeframe = savedFrame) +changeStatefulPos(string frameArg) => + var string savedFrame = frameArg + timeframe.change(savedFrame) +changeStatefulNamed(string frameArg) => + var string savedFrame = frameArg + timeframe.change(timeframe = savedFrame) +countedFrame(array counter, string frameArg) => + array.set(counter, 0, array.get(counter, 0) + 1) + frameArg +S_literal_pos = timeframe.in_seconds("") +S_literal_named = timeframe.in_seconds(timeframe = "") +S_input_pos = timeframe.in_seconds(frameInput) +S_input_named = timeframe.in_seconds(timeframe = frameInput) +S_string_input_pos = timeframe.in_seconds(frameString) +S_string_input_named = timeframe.in_seconds(timeframe = frameString) +S_variable_pos = timeframe.in_seconds(frameVariable) +S_variable_named = timeframe.in_seconds(timeframe = frameVariable) +S_temporary_name_pos = timeframe.in_seconds(_pf_timeframe_arg) +S_temporary_name_named = timeframe.in_seconds(timeframe = _pf_timeframe_arg) +S_parameter_name_pos = secondsCapturedPos(frameString) +S_parameter_name_named = secondsCapturedNamed(frameString) +S_occupied_escape_pos = timeframe.in_seconds(pf_safe__pf_timeframe_arg) +S_occupied_escape_named = timeframe.in_seconds(timeframe = pf_safe__pf_timeframe_arg) +S_plain_variable_pos = timeframe.in_seconds(plainVariable) +S_plain_variable_named = timeframe.in_seconds(timeframe = plainVariable) +S_persistent_pos = timeframe.in_seconds(persistentFrame) +S_persistent_named = timeframe.in_seconds(timeframe = persistentFrame) +S_return_pos = timeframe.in_seconds(emptyFrame()) +S_return_named = timeframe.in_seconds(timeframe = emptyFrame()) +S_return_input_pos = timeframe.in_seconds(returnedFrame(frameInput)) +S_return_input_named = timeframe.in_seconds(timeframe = returnedFrame(frameInput)) +S_period_pos = timeframe.in_seconds(timeframe.period) +S_period_named = timeframe.in_seconds(timeframe = timeframe.period) +S_explicit_pos = timeframe.in_seconds("240") +S_explicit_named = timeframe.in_seconds(timeframe = "240") +S_helper_empty_pos = secondsPos("") +S_helper_explicit_pos = secondsPos("240") +S_helper_empty_named = secondsNamed("") +S_helper_explicit_named = secondsNamed("240") +S_stateful_empty_pos = secondsStatefulPos("") +S_stateful_explicit_pos = secondsStatefulPos("240") +S_stateful_empty_named = secondsStatefulNamed("") +S_stateful_explicit_named = secondsStatefulNamed("240") +S_omitted = timeframe.in_seconds() +C_literal_pos = timeframe.change("") ? 1 : 0 +C_literal_named = timeframe.change(timeframe = "") ? 1 : 0 +C_input_pos = timeframe.change(frameInput) ? 1 : 0 +C_input_named = timeframe.change(timeframe = frameInput) ? 1 : 0 +C_string_input_pos = timeframe.change(frameString) ? 1 : 0 +C_string_input_named = timeframe.change(timeframe = frameString) ? 1 : 0 +C_variable_pos = timeframe.change(frameVariable) ? 1 : 0 +C_variable_named = timeframe.change(timeframe = frameVariable) ? 1 : 0 +C_temporary_name_pos = timeframe.change(_pf_timeframe_arg) ? 1 : 0 +C_temporary_name_named = timeframe.change(timeframe = _pf_timeframe_arg) ? 1 : 0 +C_parameter_name_pos = changeCapturedPos(frameString) ? 1 : 0 +C_parameter_name_named = changeCapturedNamed(frameString) ? 1 : 0 +C_occupied_escape_pos = timeframe.change(pf_safe__pf_timeframe_arg) ? 1 : 0 +C_occupied_escape_named = timeframe.change(timeframe = pf_safe__pf_timeframe_arg) ? 1 : 0 +C_plain_variable_pos = timeframe.change(plainVariable) ? 1 : 0 +C_plain_variable_named = timeframe.change(timeframe = plainVariable) ? 1 : 0 +C_persistent_pos = timeframe.change(persistentFrame) ? 1 : 0 +C_persistent_named = timeframe.change(timeframe = persistentFrame) ? 1 : 0 +C_return_pos = timeframe.change(emptyFrame()) ? 1 : 0 +C_return_named = timeframe.change(timeframe = emptyFrame()) ? 1 : 0 +C_return_input_pos = timeframe.change(returnedFrame(frameInput)) ? 1 : 0 +C_return_input_named = timeframe.change(timeframe = returnedFrame(frameInput)) ? 1 : 0 +C_period_pos = timeframe.change(timeframe.period) ? 1 : 0 +C_period_named = timeframe.change(timeframe = timeframe.period) ? 1 : 0 +C_explicit_pos = timeframe.change("240") ? 1 : 0 +C_explicit_named = timeframe.change(timeframe = "240") ? 1 : 0 +C_helper_empty_pos = changePos("") ? 1 : 0 +C_helper_explicit_pos = changePos("240") ? 1 : 0 +C_helper_empty_named = changeNamed("") ? 1 : 0 +C_helper_explicit_named = changeNamed("240") ? 1 : 0 +C_stateful_empty_pos = changeStatefulPos("") ? 1 : 0 +C_stateful_explicit_pos = changeStatefulPos("240") ? 1 : 0 +C_stateful_empty_named = changeStatefulNamed("") ? 1 : 0 +C_stateful_explicit_named = changeStatefulNamed("240") ? 1 : 0 +C_omitted = timeframe.change() ? 1 : 0 +dynamicFrame = bar_index % 2 == 0 ? "" : "240" +C_dynamic_pos = timeframe.change(dynamicFrame) ? 1 : 0 +C_dynamic_named = timeframe.change(timeframe = dynamicFrame) ? 1 : 0 +var calls = array.new_int(1, 0) +C_once_pos = timeframe.change(countedFrame(calls, frameInput)) ? 1 : 0 +C_once_named = timeframe.change(timeframe = countedFrame(calls, frameInput)) ? 1 : 0 +callCount = array.get(calls, 0) +// @pf-trace S_literal_pos=S_literal_pos +// @pf-trace S_literal_named=S_literal_named +// @pf-trace S_input_pos=S_input_pos +// @pf-trace S_input_named=S_input_named +// @pf-trace S_string_input_pos=S_string_input_pos +// @pf-trace S_string_input_named=S_string_input_named +// @pf-trace S_variable_pos=S_variable_pos +// @pf-trace S_variable_named=S_variable_named +// @pf-trace S_temporary_name_pos=S_temporary_name_pos +// @pf-trace S_temporary_name_named=S_temporary_name_named +// @pf-trace S_parameter_name_pos=S_parameter_name_pos +// @pf-trace S_parameter_name_named=S_parameter_name_named +// @pf-trace S_occupied_escape_pos=S_occupied_escape_pos +// @pf-trace S_occupied_escape_named=S_occupied_escape_named +// @pf-trace S_plain_variable_pos=S_plain_variable_pos +// @pf-trace S_plain_variable_named=S_plain_variable_named +// @pf-trace S_persistent_pos=S_persistent_pos +// @pf-trace S_persistent_named=S_persistent_named +// @pf-trace S_return_pos=S_return_pos +// @pf-trace S_return_named=S_return_named +// @pf-trace S_return_input_pos=S_return_input_pos +// @pf-trace S_return_input_named=S_return_input_named +// @pf-trace S_period_pos=S_period_pos +// @pf-trace S_period_named=S_period_named +// @pf-trace S_explicit_pos=S_explicit_pos +// @pf-trace S_explicit_named=S_explicit_named +// @pf-trace S_helper_empty_pos=S_helper_empty_pos +// @pf-trace S_helper_explicit_pos=S_helper_explicit_pos +// @pf-trace S_helper_empty_named=S_helper_empty_named +// @pf-trace S_helper_explicit_named=S_helper_explicit_named +// @pf-trace S_stateful_empty_pos=S_stateful_empty_pos +// @pf-trace S_stateful_explicit_pos=S_stateful_explicit_pos +// @pf-trace S_stateful_empty_named=S_stateful_empty_named +// @pf-trace S_stateful_explicit_named=S_stateful_explicit_named +// @pf-trace S_omitted=S_omitted +// @pf-trace C_literal_pos=C_literal_pos +// @pf-trace C_literal_named=C_literal_named +// @pf-trace C_input_pos=C_input_pos +// @pf-trace C_input_named=C_input_named +// @pf-trace C_string_input_pos=C_string_input_pos +// @pf-trace C_string_input_named=C_string_input_named +// @pf-trace C_variable_pos=C_variable_pos +// @pf-trace C_variable_named=C_variable_named +// @pf-trace C_temporary_name_pos=C_temporary_name_pos +// @pf-trace C_temporary_name_named=C_temporary_name_named +// @pf-trace C_parameter_name_pos=C_parameter_name_pos +// @pf-trace C_parameter_name_named=C_parameter_name_named +// @pf-trace C_occupied_escape_pos=C_occupied_escape_pos +// @pf-trace C_occupied_escape_named=C_occupied_escape_named +// @pf-trace C_plain_variable_pos=C_plain_variable_pos +// @pf-trace C_plain_variable_named=C_plain_variable_named +// @pf-trace C_persistent_pos=C_persistent_pos +// @pf-trace C_persistent_named=C_persistent_named +// @pf-trace C_return_pos=C_return_pos +// @pf-trace C_return_named=C_return_named +// @pf-trace C_return_input_pos=C_return_input_pos +// @pf-trace C_return_input_named=C_return_input_named +// @pf-trace C_period_pos=C_period_pos +// @pf-trace C_period_named=C_period_named +// @pf-trace C_explicit_pos=C_explicit_pos +// @pf-trace C_explicit_named=C_explicit_named +// @pf-trace C_helper_empty_pos=C_helper_empty_pos +// @pf-trace C_helper_explicit_pos=C_helper_explicit_pos +// @pf-trace C_helper_empty_named=C_helper_empty_named +// @pf-trace C_helper_explicit_named=C_helper_explicit_named +// @pf-trace C_stateful_empty_pos=C_stateful_empty_pos +// @pf-trace C_stateful_explicit_pos=C_stateful_explicit_pos +// @pf-trace C_stateful_empty_named=C_stateful_empty_named +// @pf-trace C_stateful_explicit_named=C_stateful_explicit_named +// @pf-trace C_omitted=C_omitted +// @pf-trace C_dynamic_pos=C_dynamic_pos +// @pf-trace C_dynamic_named=C_dynamic_named +// @pf-trace C_once_pos=C_once_pos +// @pf-trace C_once_named=C_once_named +// @pf-trace callCount=callCount diff --git a/tests/fixtures/empty_timeframe/reporter-change.pine b/tests/fixtures/empty_timeframe/reporter-change.pine new file mode 100644 index 00000000..7b69fac4 --- /dev/null +++ b/tests/fixtures/empty_timeframe/reporter-change.pine @@ -0,0 +1,10 @@ +//@version=6 +strategy("timeframe.change empty string", overlay = true, initial_capital = 10000) +fast = ta.ema(close, 10) +slow = ta.ema(close, 30) +chg = timeframe.change("") ? 1 : 0 +// @pf-trace chg=chg +if ta.crossover(fast, slow) + strategy.entry("Long", strategy.long, qty = 0.01) +if ta.crossunder(fast, slow) + strategy.close("Long") diff --git a/tests/fixtures/empty_timeframe/reporter-seconds.pine b/tests/fixtures/empty_timeframe/reporter-seconds.pine new file mode 100644 index 00000000..e9667c37 --- /dev/null +++ b/tests/fixtures/empty_timeframe/reporter-seconds.pine @@ -0,0 +1,10 @@ +//@version=6 +strategy("timeframe.in_seconds empty string", overlay = true, initial_capital = 10000) +fast = ta.ema(close, 10) +slow = ta.ema(close, 30) +secs = timeframe.in_seconds("") +// @pf-trace secs=secs +if ta.crossover(fast, slow) + strategy.entry("Long", strategy.long, qty = 0.01) +if ta.crossunder(fast, slow) + strategy.close("Long") diff --git a/tests/test_e2e_empty_timeframe.py b/tests/test_e2e_empty_timeframe.py new file mode 100644 index 00000000..f7670011 --- /dev/null +++ b/tests/test_e2e_empty_timeframe.py @@ -0,0 +1,219 @@ +"""Empty timeframe strings resolve after argument evaluation. + +The complete reporter strategies pin the two original defects. The argument +matrix observes native values for inputs, variables, returns and helpers on +several chart periods, and counts evaluations of a stateful string argument. +""" + +from __future__ import annotations + +from datetime import datetime, timezone +import math +from pathlib import Path + +import pytest + +from tests._e2e import ( + Build, assert_same_runs, build_strategy_library, chart_feed_head, + execute_all, ok, run_strategy, skip_unless_e2e_env, transpile_json, +) +from tests._requests import Feed + + +FIXTURES = Path(__file__).parent / "fixtures" / "empty_timeframe" +REPORTER_BARS = 1441 +FIXED_SECONDS = {"1S": 1, "5": 300, "60": 3600, "D": 86400, "W": 604800} +ARGUMENTS = { + "literal": "chart", + "input": "input", + "string_input": "input", + "variable": "input", + "temporary_name": "input", + "parameter_name": "input", + "occupied_escape": "explicit", + "plain_variable": "chart", + "persistent": "chart", + "return": "chart", + "return_input": "input", + "period": "chart", + "explicit": "explicit", +} +BUILTINS = {"S": "in_seconds", "C": "change"} +STYLES = ("pos", "named") + + +@pytest.fixture(scope="module") +def reporter_runs(tmp_path_factory): + engine = skip_unless_e2e_env() + base = tmp_path_factory.mktemp("reporter_empty_timeframe") + # A public corpus replay, distinct from the reporter's BTC date window. + input_bars = REPORTER_BARS * 4 + expanded = chart_feed_head(engine, base, input_bars + 4) + rows = expanded.read_text().splitlines() + first = next(index for index, line in enumerate(rows[1:], 1) + if int(line.split(",", 1)[0]) % 3_600_000 == 0) + selected = rows[first:first + input_bars] + assert len(selected) == input_bars, "public feed has too few hourly chart inputs" + feed = base / "aligned_15m.csv" + feed.write_text("\n".join([rows[0], *selected]) + "\n") + builds = {} + for kind, function in (("seconds", "in_seconds"), ("change", "change")): + source = (FIXTURES / f"reporter-{kind}.pine").read_text() + original = f'timeframe.{function}("")' + assert source.count(original) == 1 + for variant, argument in (("empty", '""'), ("period", "timeframe.period"), + ("omitted", "")): + builds[f"{kind}_{variant}"] = Build( + source.replace(original, f"timeframe.{function}({argument})", 1), + overrides={ + "input_tf": "15", "script_tf": "60", + "runtime_overrides": {"currency": "USDT", "timezone": "UTC"}, + }, + trace=True, + ) + return execute_all(engine, feed, base, builds) + + +@pytest.mark.parametrize("kind", ("seconds", "change"), ids=("in_seconds", "change")) +def test_reporter_empty_matches_chart_controls(kind, reporter_runs): + subject = ok(reporter_runs, f"{kind}_empty") + period = ok(reporter_runs, f"{kind}_period") + omitted = ok(reporter_runs, f"{kind}_omitted") + label = "secs" if kind == "seconds" else "chg" + for tag, count, seconds in (("default", REPORTER_BARS * 4, 900.0), + ("override", REPORTER_BARS, 3600.0)): + control = period.traces[tag] + assert len(control) == count + assert {row["name"] for row in control} == {label} + assert [row["bar_index"] for row in control] == list(range(count)) + assert all(b["timestamp"] - a["timestamp"] == seconds * 1000 + for a, b in zip(control, control[1:])) + expected = ([seconds] * count if kind == "seconds" + else [0.0] + [1.0] * (count - 1)) + assert [row["value"] for row in control] == expected + assert_same_runs(omitted, period) + assert_same_runs(subject, period) + + +def _clock_stamps(period): + epoch = int(datetime(2024, 1, 1, tzinfo=timezone.utc).timestamp() * 1000) + if period == "M": + return [int(datetime(2024, month, 1, tzinfo=timezone.utc).timestamp() * 1000) + for month in range(1, 6)] + return [epoch + index * FIXED_SECONDS[period] * 1000 for index in range(33)] + + +@pytest.fixture(scope="module") +def argument_runs(tmp_path_factory): + engine = skip_unless_e2e_env() + base = tmp_path_factory.mktemp("empty_timeframe_arguments") + pine = FIXTURES / "arguments.pine" + source = pine.read_text() + result = transpile_json(pine) + assert result.get("ok"), result.get("diagnostics") + build_strategy_library(result["cpp"], base) + expected_names = {line.split()[2].split("=", 1)[0] + for line in source.splitlines() if line.startswith("// @pf-trace ")} + outcomes = {} + for period in (*FIXED_SECONDS, "M"): + stamps = _clock_stamps(period) + last_close = ( + int(datetime(2024, 6, 1, tzinfo=timezone.utc).timestamp() * 1000) + if period == "M" else stamps[-1] + stamps[-1] - stamps[-2] + ) + bars = [(stamp, close_stamp, 100.0 + index) + for index, (stamp, close_stamp) + in enumerate(zip(stamps, [*stamps[1:], last_close]))] + feed = base / f"clock_{period}.csv" + feed.write_bytes(Feed("BINANCE:BTCUSDT", period, bars).csv()) + for argument in ("", "240"): + tag = f"{period}_{'empty' if not argument else 'explicit'}" + overrides = { + "input_tf": period, "script_tf": period, + "Frame input": argument, "String input": argument, + "runtime_overrides": { + "currency": "USDT", "timezone": "UTC", "session": "24x7", + }, + } + _trades, records, stderr = run_strategy( + engine, base, feed, overrides, tag, trace=True, + ) + (base / f"run_{tag}.stderr").write_text(stderr) + columns = {} + for row in records: + columns.setdefault(row["name"], []).append(row) + assert set(columns) == expected_names, f"{tag}: missing native trace names" + for name, series in columns.items(): + assert len(series) == len(stamps), (tag, name, "missing native calls") + assert [row["bar_index"] for row in series] == list(range(len(stamps))) + assert [row["timestamp"] for row in series] == stamps + outcomes[period, argument] = { + name: [row["value"] for row in series] for name, series in columns.items() + } + return outcomes + + +def _expected(frame, stamps, chart_seconds): + if frame == "chart": + return [chart_seconds] * len(stamps), [0.0] + [1.0] * (len(stamps) - 1) + assert frame == "explicit" + size = 240 * 60 * 1000 + return ([14400.0] * len(stamps), + [0.0] + [float(now // size != before // size) + for before, now in zip(stamps, stamps[1:])]) + + +@pytest.mark.parametrize("builtin", BUILTINS, ids=("in_seconds", "change")) +@pytest.mark.parametrize("name", ARGUMENTS) +@pytest.mark.parametrize("style", STYLES) +def test_empty_argument_source_uses_the_evaluated_string(builtin, name, style, argument_runs): + for (period, argument), columns in argument_runs.items(): + stamps = _clock_stamps(period) + chart_seconds = columns["S_period_pos"][0] + assert math.isfinite(chart_seconds) + if period in FIXED_SECONDS: + assert chart_seconds == FIXED_SECONDS[period] + # Calendar-only engine periods retain their existing conversion; + # equality to the period control is the empty-argument contract. + assert columns["S_period_pos"] == [chart_seconds] * len(stamps) + assert columns["C_period_pos"] == [0.0] + [1.0] * (len(stamps) - 1) + resolution = ARGUMENTS[name] + frame = ("explicit" if resolution == "explicit" + or (resolution == "input" and argument) else "chart") + seconds, changes = _expected(frame, stamps, chart_seconds) + assert columns[f"{builtin}_{name}_{style}"] == ( + seconds if builtin == "S" else changes + ), (period, argument, builtin, name, style) + + +@pytest.mark.parametrize("builtin", BUILTINS, ids=("in_seconds", "change")) +@pytest.mark.parametrize("style", STYLES) +@pytest.mark.parametrize("body", ("helper", "stateful")) +def test_each_helper_body_preserves_empty_and_explicit_arguments(builtin, style, body, argument_runs): + for (period, argument), columns in argument_runs.items(): + stamps = _clock_stamps(period) + for suffix, frame in (("empty", "chart"), ("explicit", "explicit")): + seconds, changes = _expected(frame, stamps, columns["S_period_pos"][0]) + assert columns[f"{builtin}_{body}_{suffix}_{style}"] == ( + seconds if builtin == "S" else changes + ), (period, argument, builtin, style, body, suffix) + + +def test_omitted_arguments_keep_the_existing_chart_resolution(argument_runs): + for columns in argument_runs.values(): + for builtin in BUILTINS: + assert columns[f"{builtin}_omitted"] == columns[f"{builtin}_period_pos"] + + +def test_change_evaluates_each_string_once_and_can_switch_to_explicit(argument_runs): + for (period, argument), columns in argument_runs.items(): + stamps = _clock_stamps(period) + _, chart = _expected("chart", stamps, columns["S_period_pos"][0]) + _, explicit = _expected("explicit", stamps, columns["S_period_pos"][0]) + dynamic = [chart[index] if index % 2 == 0 else explicit[index] + for index in range(len(stamps))] + assert columns["C_dynamic_pos"] == dynamic + assert columns["C_dynamic_named"] == dynamic + assert columns["C_once_pos"] == (explicit if argument else chart) + assert columns["C_once_named"] == (explicit if argument else chart) + assert columns["callCount"] == [2.0 * (index + 1) for index in range(len(stamps))] diff --git a/tests/test_e2e_temporary_name_hygiene.py b/tests/test_e2e_temporary_name_hygiene.py index e9295016..2e3ec38f 100644 --- a/tests/test_e2e_temporary_name_hygiene.py +++ b/tests/test_e2e_temporary_name_hygiene.py @@ -117,8 +117,14 @@ def test_every_temporary_the_emitter_declares_is_reserved(): # Temporaries derived from emitted C++: every underscore-prefixed local # declared inside a generated lambda that is not a name of the script. names = set() - for name in TAPES: - source = (FIXTURES / f"{name}.pine").read_text() + sources = [(FIXTURES / f"{name}.pine").read_text() for name in TAPES] + sources.append( + HEAD + 'frameChoice = input.timeframe("")\n' + 'secondsValue = timeframe.in_seconds(frameChoice)\n' + 'changedValue = timeframe.change(timeframe = frameChoice)\n' + 'plot(secondsValue)\nplot(changedValue ? 1 : 0)\n' + ) + for source in sources: authored = {t.value for t in Lexer(source).tokenize() if t.type.name == "IDENT"} for line in transpile(source).splitlines(): if "[&]" in line: From 6453949ced44ed9da25d6c29c24b87648e5294f7 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 03:24:39 +0800 Subject: [PATCH 02/27] Preserve timeframe arguments and generated-name safety Resolve dotted builtin signatures consistently and preserve explicit strings, chart-period fallback and single evaluation. Reserve generated conversion names. --- CHANGELOG.md | 6 ++- pineforge_codegen/codegen/helpers.py | 6 ++- pineforge_codegen/codegen/visit_call.py | 56 +++++++++++++++---------- 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57f4e358..fb497781 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,11 @@ supported as exact pairs; on the 0.x line they are independent. See the - `timeframe.in_seconds` and `timeframe.change` resolve an evaluated empty string to the chart period, including input, variable, function-return and named arguments. The argument is evaluated once, and nonempty strings keep - their explicit period. (#394) + their explicit period. Dotted builtin signatures retain supplied arguments, + including escaped string literals and VWAP anchors. (#394) +- Script identifiers spelled like integer-to-float conversion temporaries, + including the underscore suffixes of nested conversions, now use the + generator's reserved-name escaping. - A zone name the engine rejects now makes calendar-form `timestamp()` stop the run at its first evaluation, instead of silently calculating a UTC value. The paired engine reports a typed timezone error identifying the failed bar. diff --git a/pineforge_codegen/codegen/helpers.py b/pineforge_codegen/codegen/helpers.py index 9b5236cc..40a79b20 100644 --- a/pineforge_codegen/codegen/helpers.py +++ b/pineforge_codegen/codegen/helpers.py @@ -543,7 +543,7 @@ def cpp_code_only(cpp: str) -> str: _pf_number_text _pf_substring_text _pf_substring_begin _pf_substring_end _pf_substring_error _pf_array_new_size _pf_array_new_value _pf_array_new_size_type _pf_array_new_error - _pf_read _pf_v5_l _pf_v5_r _pf_wide_l _pf_wide_r + _pf_read _pf_v5_l _pf_v5_r _pf_wide_l _pf_wide_r _pf_w _pf_symbol _pf_colon _pf_color _pf_bar_back _pf_ema_seeding _pf_history_array _pf_history_offset _pf_matrix_target _pf_array_index_type @@ -551,7 +551,8 @@ def cpp_code_only(cpp: str) -> str: _pf_sort_left _pf_sort_right _pf_sort_left_na _pf_sort_right_na """.split()) # These numbered members and locals keep their source-order spelling rather -# than allocating against authored names. Reserve their complete spellings, +# than allocating against authored names. Double coercions append underscores +# to _pf_w to distinguish nested temporaries. Reserve their complete spellings, # just as for fixed temporaries, without reserving ordinary underscore names. _CPP_TEMPORARY_PATTERN = re.compile( r"_v\d+|_secval_\d+(?:_v\d+)?|_tuple_result_\d+(?:_elem_\d+)?|_tuple_unused_\d+|__switch_val_\d+" @@ -561,6 +562,7 @@ def cpp_code_only(cpp: str) -> str: r"|_pf_every_bar_ta_\d+|_pf_shared_\d+_\d+|__pf_array\w*|__pf_raw_\w+" r"|_pf_collection_hist_\w+" r"|_pf_substring_arg\d+" + r"|_pf_w_+" r"|_req_sec_(?:lower_tf_)?\d+(?:_\d+)?|_precalc_\w+|_sec\d+_\w+" r"|_pf_(?:sec|rec)_missing_\d+|_eval_security_\d+" r"|_RuntimeSec\d+|_runtime_(?:feed|request)_\d+" diff --git a/pineforge_codegen/codegen/visit_call.py b/pineforge_codegen/codegen/visit_call.py index fb34cb61..eb97f7bd 100644 --- a/pineforge_codegen/codegen/visit_call.py +++ b/pineforge_codegen/codegen/visit_call.py @@ -3219,26 +3219,36 @@ def _visit_func_call(self, node: FuncCall) -> str: argument = self._resolve_func_args( node, f"timeframe.{func_name}", ).get("timeframe") - tf_arg = ( - "script_tf_" if argument is None else - "(_pf_timeframe_arg.empty() ? script_tf_ : _pf_timeframe_arg)" - ) - if func_name == "change": - # Keep the symbol-clock overload for calendar and - # session-anchored intraday boundaries. - call = ( - f'tf_change(prev_bar_timestamp_, current_bar_.timestamp, {tf_arg}, ' - 'syminfo_.timezone, syminfo_.session)' - ) - else: - call = f'tf_to_seconds({tf_arg})' + + def timeframe_call(tf_arg: str) -> str: + if func_name == "change": + # Keep the symbol-clock overload for calendar and + # session-anchored intraday boundaries. + return ( + f'tf_change(prev_bar_timestamp_, current_bar_.timestamp, {tf_arg}, ' + 'syminfo_.timezone, syminfo_.session)' + ) + return f'tf_to_seconds({tf_arg})' + if argument is None: - return call + return timeframe_call("script_tf_") + argument_cpp = self._visit_expr(argument) + if cpp_is_plain_read(argument_cpp): + # Keep resolved locals and parameters as direct engine + # arguments when nonempty. Stable reads can be checked + # without introducing another binding. + return ( + f'(({argument_cpp}).empty() ? {timeframe_call("script_tf_")} : ' + f'{timeframe_call(argument_cpp)})' + ) # A function-return argument can carry state. Evaluate it # once before testing emptiness and passing the chosen value. + call = timeframe_call( + "(_pf_timeframe_arg.empty() ? script_tf_ : _pf_timeframe_arg)" + ) return ( '([&]() { const std::string _pf_timeframe_arg = ' - f'({self._visit_expr(argument)}); return {call}; }}())' + f'({argument_cpp}); return {call}; }}())' ) # Defensive: support_checker.NOT_YET_FUNC should already have rejected # any unhandled timeframe.* call. Reaching here implies the checker was @@ -4418,18 +4428,20 @@ def _resolve_func_args(self, node: FuncCall, sig_key: str) -> dict: Uses the PineScript parameter ordering from signatures registry. """ - # sig_key is like "strategy.entry" -> namespace="strategy", func_name="entry" + # Namespace records use their short name; dotted builtin records use + # the complete key in BUILTIN_FUNCTIONS (e.g. timeframe.change). parts = sig_key.split(".", 1) + intrinsic = ( + sigs.get_intrinsic(parts[0], parts[1]) if len(parts) == 2 else None + ) + if intrinsic is None: + intrinsic = sigs.get_intrinsic(None, sig_key) validate_intrinsic_keywords( - sigs.get_intrinsic(parts[0], parts[1]) if len(parts) == 2 - else sigs.get_intrinsic(None, sig_key), + intrinsic, node.kwargs, pine_version=self.ctx.ast.version, ) - if len(parts) == 2: - param_names = sigs.get_param_names(parts[0], parts[1]) or [] - else: - param_names = sigs.get_param_names(None, sig_key) or [] + param_names = intrinsic.param_names if intrinsic is not None else [] result: dict = {} # Map positional args by parameter name for i, arg in enumerate(node.args): From cd3b13f43b7f5da4660c9cb5725792f5333900ea Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 06:03:06 +0800 Subject: [PATCH 03/27] Update anchor test expectations for conditional timeframe calls The existing symbol-clock anchor tests now check both calls in the emitted empty-argument conditional. Add pure-Python checks of branch direction and single evaluation, and make the temporary-name sweep reach the argument lambda. Product code is unchanged by this test update. --- tests/test_anchor_tf_change.py | 25 +++++++++++- tests/test_anchor_vwap.py | 8 +++- tests/test_e2e_temporary_name_hygiene.py | 6 ++- tests/test_empty_timeframe_emission.py | 48 ++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 tests/test_empty_timeframe_emission.py diff --git a/tests/test_anchor_tf_change.py b/tests/test_anchor_tf_change.py index d8e08b23..9a0f2730 100644 --- a/tests/test_anchor_tf_change.py +++ b/tests/test_anchor_tf_change.py @@ -26,7 +26,30 @@ def _pine(body: str) -> str: def _calls(cpp: str, symbol: str) -> list[str]: """Every `symbol(...)` call expression in the generated C++ (one line each).""" - return [m.group(0) for m in re.finditer(rf"\b{re.escape(symbol)}\([^;]*", cpp)] + calls = [] + for match in re.finditer(rf"\b{re.escape(symbol)}\(", cpp): + depth, quote, escaped = 0, None, False + for index in range(match.end() - 1, len(cpp)): + char = cpp[index] + if quote: + if escaped: + escaped = False + elif char == "\\": + escaped = True + elif char == quote: + quote = None + elif char in ("\"", "'"): + quote = char + elif char == "(": + depth += 1 + elif char == ")": + depth -= 1 + if depth == 0: + calls.append(cpp[match.start():index + 1]) + break + else: + raise AssertionError("Unclosed generated call: " + symbol) + return calls def test_timeframe_change_uses_symbol_clock_overload(): diff --git a/tests/test_anchor_vwap.py b/tests/test_anchor_vwap.py index a97291c6..8376915d 100644 --- a/tests/test_anchor_vwap.py +++ b/tests/test_anchor_vwap.py @@ -67,7 +67,13 @@ def test_ta_vwap_bands_form_threads_symbol_clock(): assert calls, cpp for call in calls: assert ("current_bar_.timestamp, syminfo_.timezone, syminfo_.session, " - "tf_change(") in call, call + "((std::string(\"D\")).empty() ? tf_change(") in call, call + assert ( + 'tf_change(prev_bar_timestamp_, current_bar_.timestamp, script_tf_, ' + 'syminfo_.timezone, syminfo_.session) : ' + 'tf_change(prev_bar_timestamp_, current_bar_.timestamp, std::string("D"), ' + 'syminfo_.timezone, syminfo_.session)' + ) in call, call def test_vwap_prelude_shim_gated_on_usage(): diff --git a/tests/test_e2e_temporary_name_hygiene.py b/tests/test_e2e_temporary_name_hygiene.py index 2e3ec38f..7f80f775 100644 --- a/tests/test_e2e_temporary_name_hygiene.py +++ b/tests/test_e2e_temporary_name_hygiene.py @@ -120,8 +120,9 @@ def test_every_temporary_the_emitter_declares_is_reserved(): sources = [(FIXTURES / f"{name}.pine").read_text() for name in TAPES] sources.append( HEAD + 'frameChoice = input.timeframe("")\n' - 'secondsValue = timeframe.in_seconds(frameChoice)\n' - 'changedValue = timeframe.change(timeframe = frameChoice)\n' + 'chosenFrame() => frameChoice\n' + 'secondsValue = timeframe.in_seconds(chosenFrame())\n' + 'changedValue = timeframe.change(timeframe = chosenFrame())\n' 'plot(secondsValue)\nplot(changedValue ? 1 : 0)\n' ) for source in sources: @@ -131,4 +132,5 @@ def test_every_temporary_the_emitter_declares_is_reserved(): names |= {n for n in DECL.findall(line) if n not in authored and not n.startswith("pf_safe_")} assert names, "the tapes' C++ declares no temporary" + assert "_pf_timeframe_arg" in names assert sorted(n for n in names if not is_emitter_temporary(n)) == [] diff --git a/tests/test_empty_timeframe_emission.py b/tests/test_empty_timeframe_emission.py new file mode 100644 index 00000000..5ca2a65b --- /dev/null +++ b/tests/test_empty_timeframe_emission.py @@ -0,0 +1,48 @@ +"""Empty argument selection is covered without native engine inputs.""" + +import pytest + +from pineforge_codegen import transpile + + +def engine_call(function, argument): + if function == "change": + return ( + "tf_change(prev_bar_timestamp_, current_bar_.timestamp, " + + argument + ", syminfo_.timezone, syminfo_.session)" + ) + return "tf_to_seconds(" + argument + ")" + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("argument,lowered", [ + ('""', 'std::string("")'), + ('"240"', 'std::string("240")'), + ("frameChoice", "frameChoice"), +]) +def test_empty_plain_argument_selects_chart_and_nonempty_selects_argument(function, argument, lowered): + cpp = transpile( + '//@version=6\nstrategy("Chart timeframe selection")\n' + 'frameChoice = input.timeframe("")\n' + f"value = timeframe.{function}({argument})\n" + ) + expected = ( + "((" + lowered + ").empty() ? " + + engine_call(function, "script_tf_") + " : " + + engine_call(function, lowered) + ")" + ) + assert expected in cpp + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +def test_call_argument_is_evaluated_once_before_chart_selection(function): + cpp = transpile( + '//@version=6\nstrategy("Chart timeframe selection")\n' + 'frameChoice = input.timeframe("")\n' + 'chosenFrame() => frameChoice\n' + f"value = timeframe.{function}(chosenFrame())\n" + ) + assert "const std::string _pf_timeframe_arg = " in cpp + selected = "(_pf_timeframe_arg.empty() ? script_tf_ : _pf_timeframe_arg)" + assert engine_call(function, selected) in cpp + assert cpp.count("const std::string _pf_timeframe_arg = (chosenFrame());") == 1 From 909baafd51aa1a093a0359f998c5edded19ebb0c Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 07:06:49 +0800 Subject: [PATCH 04/27] Preserve reference types across fields and calls Lower missing reference values to their destination storage and retain drawing handle types through user functions and tuple returns. Support declared polyline types, fields and deletion arguments. --- CHANGELOG.md | 6 + docs/reference/support-contracts.md | 6 + pineforge_codegen/analyzer/types.py | 9 +- pineforge_codegen/codegen/base.py | 43 +++---- pineforge_codegen/codegen/drawing.py | 139 ++++++++++++++++++---- pineforge_codegen/codegen/emit_top.py | 11 +- pineforge_codegen/codegen/helpers.py | 1 + pineforge_codegen/codegen/tables.py | 10 +- pineforge_codegen/codegen/types.py | 17 +++ pineforge_codegen/codegen/visit_call.py | 24 +--- pineforge_codegen/codegen/visit_expr.py | 9 +- pineforge_codegen/codegen/visit_stmt.py | 20 ++-- pineforge_codegen/reference_casts.py | 38 ++++++ tests/test_codegen_drawing_data.py | 2 +- tests/test_drawing_type_casts.py | 11 +- tests/test_reference_handle_lowering.py | 152 ++++++++++++++++++++++++ tests/test_udt_drawing_field_cleanup.py | 3 +- 17 files changed, 414 insertions(+), 87 deletions(-) create mode 100644 pineforge_codegen/reference_casts.py create mode 100644 tests/test_reference_handle_lowering.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 83af3833..63e30b05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,12 @@ supported as exact pairs; on the 0.x line they are independent. See the ## Unreleased (next release) +- Reference-typed UDT fields keep their type when initialized or reassigned + with `na`, including array defaults. Drawing handles cross user-function + and tuple boundaries by value, and `label(na)` produces a missing label + handle. Polyline locals, arrays, fields and delete methods retain opaque + handles and argument evaluation while rendering remains a visual no-op. + - A zone name the engine rejects now makes calendar-form `timestamp()` stop the run at its first evaluation, instead of silently calculating a UTC value. The paired engine reports a typed timezone error identifying the failed bar. diff --git a/docs/reference/support-contracts.md b/docs/reference/support-contracts.md index 9406140d..48681138 100644 --- a/docs/reference/support-contracts.md +++ b/docs/reference/support-contracts.md @@ -20,6 +20,12 @@ lowering. Structural mutation, collection reassignment/escape, history and pair-binder iteration retain PF-E5086 pending TV evidence/provenance support; declaration tails yielding collections as callable/selection values are refused. `polyline.all` retains PF-E5085 until the engine has a Polyline handle/arena. +Polyline locals, array elements and UDT fields retain opaque typed handles; +default construction and bare `na` use a missing handle, and copies retain +its identity. `polyline.new` and functional or method-form `delete` keep the +existing visual-only rendering contract, while evaluating their arguments +once in parameter order. Opaque ID allocation joins script reset and rollback +state. This does not supply the live arena required by `polyline.all`. `var`/`varip` aliases retain PF-E5086 until a TradingView capture establishes their lifetime semantics. Whether a later creation in the same bar is visible through the alias is not yet measured against TradingView. diff --git a/pineforge_codegen/analyzer/types.py b/pineforge_codegen/analyzer/types.py index 6c8fb0be..806c717e 100644 --- a/pineforge_codegen/analyzer/types.py +++ b/pineforge_codegen/analyzer/types.py @@ -48,6 +48,7 @@ from ..errors import Phase from ..limits import iter_ast_nodes from ..matrix_overloads import matrix_sum_has_rhs +from ..reference_casts import na_reference_cast_kind from ..symbols import ( PineType, TypeSpec, @@ -59,7 +60,9 @@ # Drawing-objects-as-data type names (spec §4.1). Defined locally — the # analyzer must not import from ``codegen`` (codegen imports analyzer, so the # reverse would be a cycle). Mirrors codegen.tables.DRAWING_TYPE_TO_CPP keys. -_DRAWING_TYPE_NAMES = frozenset({"line", "box", "label", "linefill", "chart.point"}) +_DRAWING_TYPE_NAMES = frozenset({ + "line", "box", "label", "linefill", "chart.point", "polyline", +}) _DRAWING_NS = frozenset({"line", "box", "label", "linefill"}) _DIRECT_ARRAY_VALUE_PRODUCERS = frozenset({ "from", @@ -588,6 +591,10 @@ def direct_user_udt_ctor_name(node: ASTNode) -> str | None: return typed_receiver_spec # Drawing-objects-as-data return typing: *.new / *.copy -> handle of # the self-type; linefill.get_line* -> line; chart.point.* -> point. + if cast_kind := na_reference_cast_kind(value): + return TypeSpec.udt(cast_kind) + if ns == "polyline" and func == "new": + return TypeSpec.udt("polyline") if ns in _DRAWING_NS: if func in ("new", "copy"): return TypeSpec.udt(ns) diff --git a/pineforge_codegen/codegen/base.py b/pineforge_codegen/codegen/base.py index 6bd0a8ad..4995469d 100644 --- a/pineforge_codegen/codegen/base.py +++ b/pineforge_codegen/codegen/base.py @@ -32,6 +32,7 @@ from ..errors import CompileError, Diagnostic, Level, Phase, SourceLocation from ..limits import TimeBudget, iter_ast_nodes, syntax_children from ..matrix_overloads import matrix_sum_has_rhs +from ..reference_casts import na_reference_cast_kind from . import graph_journal from .capacities import CapacityPolicy, validate_capacity_options from ..session_reads import emitted_session_reads @@ -1110,9 +1111,8 @@ def func_var_storage(owner: str, raw_name: str) -> str: # ``_active_var_remap`` while emitting the owning block's statements. self._block_var_renames: dict[int, dict[str, str]] = getattr(ctx, "block_var_renames", {}) or {} self._udt_field_type_specs: dict[str, dict[str, TypeSpec]] = getattr(ctx, "udt_field_type_specs", {}) or {} - # Map UDT struct name -> set of field names that were dropped from the - # emitted C++ struct because they had drawing-only types (label, line, - # box, linefill, polyline, table, chart.point). Populated eagerly + # Map UDT struct name -> visual-only table fields omitted from the + # emitted record. Populated eagerly # here from ``self._udt_defs`` so downstream visitors (visit_expr / # visit_stmt) can consult it before ``generate()`` runs. The struct # emission loop later asserts/syncs against this same map. Used to @@ -1120,9 +1120,9 @@ def func_var_storage(owner: str, raw_name: str) -> str: # generated C++ never references a member that doesn't exist on the # emitted struct. See: pineforge-codegen issue #10. # Drawing-objects-as-data: line/box/label/linefill/chart.point are now - # REAL data (un-dropped from UDT structs). Only table/polyline stay - # dropped (no C++ representation). See drawing-objects-as-data.md §4.2. - _DRAWING_TYPES_INIT = {"table", "polyline"} + # REAL data (un-dropped from UDT structs). Polyline retains an opaque + # handle while its geometry remains visual-only. Only table is dropped. + _DRAWING_TYPES_INIT = {"table"} self._udt_omitted_fields: dict[str, set[str]] = {} for _type_name, _fields in self._udt_defs.items(): _omitted = set() @@ -1198,14 +1198,14 @@ def func_var_storage(owner: str, raw_name: str) -> str: self._map_vars.add(_name) elif _spec.kind == "udt" and _spec.name: self._udt_var_types.setdefault(_name, _spec.name) - # Table / polyline variables and params have NO C++ representation + # Table variables and params have NO C++ representation # (SKIP_VAR_TYPES). A *method* call on such a receiver # (``panel.cell(...)``, ``dash.merge_cells(...)``) is a visual no-op # that must be dropped — but unlike the namespace form # (``table.cell(...)``) the receiver is a bare var/param the # namespace-based skip cannot see. Collect those names so # ``_is_skip_expr`` can drop their method calls. - _SKIP_DECL_TYPES = set(SKIP_VAR_TYPES) | {"polyline"} + _SKIP_DECL_TYPES = set(SKIP_VAR_TYPES) self._visual_drop_vars: set[str] = set() for _node in self._walk_ast(self.ctx.ast): if isinstance(_node, VarDecl): @@ -1359,7 +1359,10 @@ def func_var_storage(owner: str, raw_name: str) -> str: # Drawing-objects-as-data: gate all new emission (drawing.hpp include + # the per-type arenas) on this flag so non-drawing strategies stay # byte-identical. Caps come from the strategy() header max_*_count. - self._uses_drawing = self._detect_drawing_usage() + self._uses_polyline = self._detect_drawing_usage({"polyline"}) + self._uses_drawing = self._detect_drawing_usage( + set(DRAWING_TYPE_TO_CPP) - {"polyline"}, + ) self._drawing_caps = self._compute_drawing_caps() if self._uses_drawing else {} # Pine history directives retain their existing global-maximum rule. @@ -1451,14 +1454,7 @@ def _is_default_drawing_initializer(self, node) -> bool: """Whether a drawing RHS is the constant missing handle.""" return ( self._is_na_expr(node) - or ( - isinstance(node, FuncCall) - and isinstance(node.callee, Identifier) - and node.callee.name in ("line", "box") - and len(node.args) == 1 - and not node.kwargs - and self._is_na_expr(node.args[0]) - ) + or na_reference_cast_kind(node) is not None ) def _is_runtime_scalar_var_initializer( @@ -6155,6 +6151,8 @@ def _generate_cpp(self) -> str: lines.append(" DrawingArena _pf_boxes_{_PF_DRAWING_UNBOUNDED};") lines.append(" DrawingArena _pf_labels_{_PF_DRAWING_UNBOUNDED};") lines.append(" DrawingArena _pf_linefills_{_PF_DRAWING_UNBOUNDED};") + if self._uses_polyline: + lines.append(" uint64_t _pf_polyline_next_id_ = 0;") # 8e. The copies an array or a matrix variable's executions left # (pineforge_codegen/collection_history.py). @@ -6564,13 +6562,17 @@ def _is_skip_expr(self, node) -> bool: if self._is_chart_point_callee(node.callee): return False func_name, namespace = self._resolve_callee(node.callee) + if namespace == "polyline" and func_name in {"new", "delete"}: + # The geometry is visual-only; its handle and argument + # evaluations must still flow through declarations and calls. + return False if namespace is None and func_name in SKIP_FUNC_NAMES: return True if namespace in SKIP_NAMESPACES: return True if namespace in SKIP_VAR_TYPES: return True - # Method call on a table/polyline-typed receiver var/param + # Method call on a table-typed receiver var/param # (``panel.cell(...)``). These types have no C++ representation, so # the call is a visual no-op — drop it (mirrors the namespace form). if namespace in self._visual_drop_vars: @@ -6586,9 +6588,8 @@ def _is_skip_expr(self, node) -> bool: def _is_omitted_udt_field(self, node) -> bool: """True when ``node`` is a ``MemberAccess`` on a UDT variable and the - member name was dropped from the emitted struct because it had a - drawing-only type (label, line, box, linefill, polyline, table, - chart.point). Callers use this to rewrite reads and strip writes so + member name was dropped from the emitted struct because it had the + visual-only table type. Callers rewrite reads and strip writes so the generated C++ never references a non-existent struct member. See: pineforge-codegen issue #10. """ diff --git a/pineforge_codegen/codegen/drawing.py b/pineforge_codegen/codegen/drawing.py index e64fc480..9ea6fb39 100644 --- a/pineforge_codegen/codegen/drawing.py +++ b/pineforge_codegen/codegen/drawing.py @@ -1,15 +1,15 @@ """Drawing-objects-as-data dispatch for the codegen. Spec: ``docs/drawing-objects-as-data.md`` §4.3 (the 4-form call dispatch), -§L (handle lifecycle) and §U (UDT × drawing). Everything here only ever -runs for a strategy whose ``self._uses_drawing`` is True (the dispatch is -reached solely when a ``line``/``box``/``label``/``linefill``/``chart.point`` -call or a drawing-typed receiver is present), so non-drawing strategies emit -byte-identical C++. +§L (handle lifecycle) and §U (UDT × drawing). Engine-backed drawing support is +emitted when ``self._uses_drawing`` is True; opaque polyline value support is +emitted when that type is used. Non-drawing strategies need neither block. The geometry of a drawing object becomes REAL C++ state (per-type arena in ``pineforge/drawing.hpp``); every VISUAL kwarg/method (color / style / width / bgcolor / text_* / extend-as-visual / force_overlay) is accepted-and-DROPPED. +Polyline geometry remains visual-only; its typed IDs and argument effects +survive assignments, collections and calls. ``DrawingVisitor`` is mixed into ``CodeGen``; it owns no state of its own and reads only attributes established by ``CodeGen.__init__`` (``_uses_drawing``, @@ -23,6 +23,7 @@ from .. import signatures as sigs from ..ast_nodes import FuncCall, Identifier, MemberAccess +from ..errors import CodegenInvariantError from ..symbols import method_receiver_type_name from .tables import ( ARRAY_VOID_METHODS as _ARRAY_VOID_METHODS, @@ -33,6 +34,30 @@ _MAP_VOID_METHODS = frozenset({"clear", "put_all"}) +POLYLINE_HANDLE_CPP = """\ +// Polyline rendering remains visual-only. Its opaque ID is still a value: +// copies preserve identity, default construction is na, and no numeric +// conversion exists. IDs are allocated by checkpointed per-strategy state. +struct _PFPolyline { + uint64_t id = 0; + bool is_na() const { return id == 0; } + friend bool operator==(const _PFPolyline& _pf_left, const _PFPolyline& _pf_right) { + return _pf_left.id == _pf_right.id; + } + friend bool operator!=(const _PFPolyline& _pf_left, const _PFPolyline& _pf_right) { + return !(_pf_left == _pf_right); + } +}; +""" + +_POLYLINE_PARAMS = { + "new": ( + "points", "curved", "closed", "xloc", "line_color", "fill_color", + "line_style", "line_width", "force_overlay", + ), + "delete": ("id",), +} + # Largest declared line, box or label count the native arena factory accepts: its int32 # domain minus the observed-slack it adds (drawing_bound_from_declared_count in # pineforge/drawing.hpp: INT32_MAX - kDrawingObservedSlack, with the slack 5). @@ -323,6 +348,39 @@ def _collected(dtype: str, handle: str) -> str: def _emit_drawing_ctor(self, dtype: str, node: FuncCall) -> str: return self._collected(dtype, self._emit_drawing_alloc(dtype, node)) + def _emit_polyline_call(self, method: str, node: FuncCall, receiver=None) -> str: + """Keep opaque handle flow and evaluate every supplied argument once. + + These calls have no renderer or live arena. Binding by parameter order + still matters: points/style expressions can call user functions or + mutate collections, and delete's receiver can itself shift an array. + """ + params = _POLYLINE_PARAMS.get(method) + if params is None: + raise CodegenInvariantError(node) + args = ([receiver] if receiver is not None else []) + list(node.args) + if (len(args) > len(params) + or any(name not in params or name in params[:len(args)] + for name in node.kwargs)): + raise CodegenInvariantError(node) + values = dict(zip(params, args)) + values.update(node.kwargs) + if params[0] not in values: + raise CodegenInvariantError(node) + if method == "delete": + value = self._visit_rhs_value( + values["id"], target_cpp_type=DRAWING_TYPE_TO_CPP["polyline"], + ) + return f"static_cast({value})" + statements = " ".join( + f"static_cast({self._visit_expr(values[name])});" + for name in params if name in values + ) + return ( + f"([&]() {{ {statements} " + "return _PFPolyline{++this->_pf_polyline_next_id_}; }())" + ) + def _emit_drawing_alloc(self, dtype: str, node: FuncCall) -> str: arena = DRAWING_ARENA[dtype] if dtype == "linefill": @@ -447,6 +505,8 @@ def _emit_drawing_method(self, dtype: str, method: str, recv_node, arg_nodes: li ``recv_node`` is the handle receiver; ``arg_nodes`` the remaining args. """ + if dtype == "polyline": + return self._emit_polyline_call(method, node, receiver=recv_node) arena = DRAWING_ARENA[dtype] recv = self._visit_expr(recv_node) geometry, noop = DRAWING_METHODS_BY_TYPE[dtype] @@ -572,7 +632,7 @@ def _drawing_call_return_cpp(self, node: FuncCall) -> str | None: _fn, ns = self._resolve_callee(callee) from .tables import DRAWING_NS dtype = None - if ns in DRAWING_NS: + if ns in DRAWING_NS or ns == "polyline": dtype = ns else: recv_spec = self._type_spec_from_expr(callee.object) @@ -597,7 +657,7 @@ def _drawing_call_is_void(self, node) -> bool: _fn, ns = self._resolve_callee(node.callee) from .tables import DRAWING_NS dtype = None - if ns in DRAWING_NS: + if ns in DRAWING_NS or ns == "polyline": dtype = ns else: recv_spec = self._type_spec_from_expr(node.callee.object) @@ -606,6 +666,8 @@ def _drawing_call_is_void(self, node) -> bool: dtype = recv_spec.name if dtype is None: return False + if dtype == "polyline": + return method == "delete" if method in ("new", "copy"): return False # constructors / copy return a handle if (dtype, method) in _DRAWING_GETTER_RET: @@ -792,57 +854,82 @@ def _emit_drawing_collectors(self, lines: list[str]) -> None: # ------------------------------------------------------------------ # _uses_drawing detection + arena caps # ------------------------------------------------------------------ - def _spec_mentions_drawing(self, spec) -> bool: + def _spec_mentions_drawing(self, spec, kinds=None) -> bool: + kinds = DRAWING_TYPE_TO_CPP if kinds is None else kinds if spec is None: return False - if spec.kind == "udt" and spec.name in DRAWING_TYPE_TO_CPP: + if spec.kind == "udt" and spec.name in kinds: return True if spec.kind == "array": - return self._spec_mentions_drawing(spec.element) + return self._spec_mentions_drawing(spec.element, kinds) if spec.kind == "map": - return self._spec_mentions_drawing(spec.key) or self._spec_mentions_drawing(spec.value) + return (self._spec_mentions_drawing(spec.key, kinds) + or self._spec_mentions_drawing(spec.value, kinds)) if spec.kind == "matrix": - return self._spec_mentions_drawing(spec.element) + return self._spec_mentions_drawing(spec.element, kinds) return False - def _detect_drawing_usage(self) -> bool: - """True iff emitted C++ needs pineforge/drawing.hpp + the arenas. + def _detect_drawing_usage(self, kinds=None) -> bool: + """Whether declarations or calls require any of the given handle kinds. - Mirrors ``_detect_matrix_usage``: any var/field/array of a drawing type - OR any line/box/label/linefill namespace call OR any chart.point call. + The same type walk selects real engine drawing support and opaque + polyline support independently, including unused typed parameters. """ + kinds = DRAWING_TYPE_TO_CPP if kinds is None else kinds for t in self._udt_var_types.values(): - if t in DRAWING_TYPE_TO_CPP: + if t in kinds: return True for spec in self._collection_types.values(): - if self._spec_mentions_drawing(spec): + if self._spec_mentions_drawing(spec, kinds): return True for specs in self._func_collection_types.values(): for spec in specs.values(): - if self._spec_mentions_drawing(spec): + if self._spec_mentions_drawing(spec, kinds): return True for specs in self._block_collection_types.values(): for spec in specs.values(): - if self._spec_mentions_drawing(spec): + if self._spec_mentions_drawing(spec, kinds): return True for fields in self._udt_field_type_specs.values(): for spec in fields.values(): - if self._spec_mentions_drawing(spec): + if self._spec_mentions_drawing(spec, kinds): return True for _tname, _fields in self._udt_defs.items(): for f in _fields: - if self._spec_mentions_drawing(self._type_spec_from_hint_name(f.type_name)): + if self._spec_mentions_drawing( + self._type_spec_from_hint_name(f.type_name), kinds, + ): return True - from .tables import DRAWING_NS from ..drawing_all import drawing_all_kind for node in self._walk_ast(self.ctx.ast): - if drawing_all_kind(node) in DRAWING_NS: + if drawing_all_kind(node) in kinds: + return True + hint = getattr(node, "type_hint", None) + if hint and self._spec_mentions_drawing( + self._type_spec_from_hint_name(hint), kinds, + ): return True + for hint in ((getattr(node, "annotations", None) or {}).get( + "param_type_hints", + ) or ()): + if hint and self._spec_mentions_drawing( + self._type_spec_from_hint_name(hint), kinds, + ): + return True if isinstance(node, FuncCall): + for hint in self._template_args_from_call(node): + if self._spec_mentions_drawing( + self._type_spec_from_hint_name(hint), kinds, + ): + return True _fn, ns = self._resolve_callee(node.callee) - if ns in DRAWING_NS: + if ns in kinds: + return True + if (isinstance(node.callee, Identifier) + and node.callee.name in kinds): return True - if self._is_chart_point_callee(node.callee): + if ("chart.point" in kinds + and self._is_chart_point_callee(node.callee)): return True return False diff --git a/pineforge_codegen/codegen/emit_top.py b/pineforge_codegen/codegen/emit_top.py index d9c67b33..9da6a3e5 100644 --- a/pineforge_codegen/codegen/emit_top.py +++ b/pineforge_codegen/codegen/emit_top.py @@ -107,7 +107,7 @@ RUNTIME_REGISTER_SECURITY_LOWER_TF_EVAL_FN, ) from ..limits import iter_ast_nodes -from .drawing import DRAWING_LIFETIME_CPP +from .drawing import DRAWING_LIFETIME_CPP, POLYLINE_HANDLE_CPP from .tv_number_format import TV_NUMBER_FORMAT_CPP from .tv_string_units import TV_STRING_UNITS_CPP, TV_STRING_UNITS_MARKER @@ -265,6 +265,8 @@ def _emit_includes(self, lines: list[str]) -> None: self._emit_ta_compat_shims(lines) if getattr(self, "_uses_drawing", False): lines.append(DRAWING_LIFETIME_CPP) + if self._uses_polyline: + lines.append(POLYLINE_HANDLE_CPP) if self._uses_tv_number_format: lines.append(TV_NUMBER_FORMAT_CPP) lines.append("") @@ -2522,6 +2524,7 @@ def _emit_func_def_body(self, fi: FuncInfo, lines: list[str], or ( recv_spec.kind == "udt" and recv_spec.name not in self._udt_defs + and recv_spec.name not in DRAWING_TYPE_TO_CPP ) ): cpp_t = f"{cpp_t}&" @@ -2537,7 +2540,8 @@ def _emit_func_def_body(self, fi: FuncInfo, lines: list[str], ) cpp_t = ( recv_cpp - if fi.udt_type_name in self._udt_defs + if (fi.udt_type_name in self._udt_defs + or fi.udt_type_name in DRAWING_TYPE_TO_CPP) else f"{recv_cpp}&" ) safe_p = self._safe_name(p) @@ -2596,7 +2600,8 @@ def _emit_func_def_body(self, fi: FuncInfo, lines: list[str], if spec.kind == "udt": self._udt_param_udt[p] = spec.name self._udt_param_udt[self._safe_name(p)] = spec.name - if spec.name not in self._udt_defs: + if (spec.name not in self._udt_defs + and spec.name not in DRAWING_TYPE_TO_CPP): cpp_t = f"{cpp_t}&" elif spec.kind in ("array", "map"): elem = spec.element if spec.kind == "array" else spec.value diff --git a/pineforge_codegen/codegen/helpers.py b/pineforge_codegen/codegen/helpers.py index f292996f..96b1d3de 100644 --- a/pineforge_codegen/codegen/helpers.py +++ b/pineforge_codegen/codegen/helpers.py @@ -464,6 +464,7 @@ def cpp_code_only(cpp: str) -> str: _security_helper_series_ _security_helper_series_str_ _use_precalc _var_initialized _ta_initialized_ _inputs_initialized_ _pf_lines_ _pf_boxes_ _pf_labels_ _pf_linefills_ + _PFPolyline _pf_polyline_next_id_ _pf_script_state_checkpoint_ _PFScriptState _PFForeignEmaSeeding _PFALMA _PFKC _PFKCW _PFAnchoredVWAP _PFAnchoredVWAPBands _PFPivotPointLevels _PFArrayField _PFTvFormatValue diff --git a/pineforge_codegen/codegen/tables.py b/pineforge_codegen/codegen/tables.py index f0323763..a3c784ed 100644 --- a/pineforge_codegen/codegen/tables.py +++ b/pineforge_codegen/codegen/tables.py @@ -480,12 +480,13 @@ def tz_time_field_lambda(field_expr: str, ts_arg: str, tz_arg: str) -> str: # --------------------------------------------------------------------------- # Drawing-objects-as-data (spec §4.1) # --------------------------------------------------------------------------- -# Pine drawing type name -> C++ handle struct (pineforge/drawing.hpp). -# ``table`` / ``polyline`` are DELIBERATELY absent — they stay 100% no-op -# (kept in SKIP_NAMESPACES / SKIP_VAR_TYPES / _DRAWING_TYPES_INIT). +# Pine drawing type name -> C++ value type. +# Polyline values have an opaque generated handle; their geometry remains a +# visual no-op. Only the engine-backed kinds below have drawing arenas. DRAWING_TYPE_TO_CPP = { "line": "Line", "box": "Box", "label": "Label", "linefill": "Linefill", "chart.point": "ChartPoint", + "polyline": "_PFPolyline", } # Function-call namespaces routed to the per-type arena dispatch. DRAWING_NS = {"line", "box", "label", "linefill"} @@ -526,7 +527,8 @@ def tz_time_field_lambda(field_expr: str, ts_arg: str, tz_arg: str) -> str: } # line / box / label / linefill REMOVED — they now route to real drawing # codegen (gated on _uses_drawing). table / polyline / chart / display / size -# / position stay no-op. +# / position stay visual-only. Polyline creation/deletion is intercepted before +# the generic skip to retain its typed handle and argument evaluations. SKIP_NAMESPACES = { "table", "polyline", "chart", "display", "size", "position", diff --git a/pineforge_codegen/codegen/types.py b/pineforge_codegen/codegen/types.py index b6023534..d342fa13 100644 --- a/pineforge_codegen/codegen/types.py +++ b/pineforge_codegen/codegen/types.py @@ -53,6 +53,7 @@ from ..limits import iter_ast_nodes from ..loop_types import FLOAT_REASSIGNMENT, LoopTypeSources, for_counter_type from ..matrix_overloads import matrix_sum_has_rhs +from ..reference_casts import na_reference_cast_kind from ..symbols import ( PineType, TypeSpec, @@ -3141,6 +3142,11 @@ def _type_spec_from_expr_raw( targs = self._template_args_from_call(node) # Drawing-objects-as-data return typing (spec §4.5 DRAWING_RETURN_SPECS): # *.new / *.copy -> handle of the self-type; linefill.get_line* -> line. + if (func_name not in self._func_info_map + and (cast_kind := na_reference_cast_kind(node))): + return TypeSpec.udt(cast_kind) + if namespace == "polyline" and func_name == "new": + return TypeSpec.udt("polyline") if namespace in DRAWING_NS: if func_name in ("new", "copy"): return TypeSpec.udt(namespace) @@ -5304,6 +5310,12 @@ def _slot_scalar_cpp_type(self, name: str) -> str | None: primitive_global = self._primitive_block_shadow_globals.get(name) if primitive_global is not None: return self._type_for_decl(primitive_global) + drawing_cpp_type = self._drawing_target_cpp_type(name, None) + if drawing_cpp_type is not None: + # The coarse PineType of an ID is not numeric storage. In + # particular a tuple's drawing value must never enter a double + # conversion merely because the destination is a var member. + return drawing_cpp_type for vname, ptype, _init in self.ctx.var_members: if vname == name: if self._numeric_na_member(name): @@ -6338,6 +6350,11 @@ def _infer_type( return "double" if isinstance(node, FuncCall): func_name, namespace = self._resolve_callee(node.callee) + if (func_name not in self._func_info_map + and (cast_kind := na_reference_cast_kind(node))): + return DRAWING_TYPE_TO_CPP[cast_kind] + if namespace == "polyline" and func_name == "new": + return DRAWING_TYPE_TO_CPP["polyline"] if namespace == "color": return "int64_t" if func_name in {"new", "rgb", "from_gradient"} else "int" if (namespace == "request" and func_name == "security" diff --git a/pineforge_codegen/codegen/visit_call.py b/pineforge_codegen/codegen/visit_call.py index efb19c5c..248cd5e4 100644 --- a/pineforge_codegen/codegen/visit_call.py +++ b/pineforge_codegen/codegen/visit_call.py @@ -157,6 +157,7 @@ from ..external_requests import RECORDED_KEY_ANNOTATION, request_stop_marker from ..intrinsic_binding import bind_intrinsic_arguments, validate_intrinsic_keywords from ..limits import iter_ast_nodes +from ..reference_casts import na_reference_cast_kind from ..security_contexts import ticker_symbol_arg from ..symbols import PineType, TypeSpec, method_receiver_type_name from ..method_binding import ( @@ -2573,6 +2574,8 @@ def _visit_func_call(self, node: FuncCall) -> str: return self._emit_drawing_namespace_call(namespace, func_name, node) if self._is_chart_point_callee(callee): return self._emit_chart_point(func_name, node) + if namespace == "polyline" and func_name in {"new", "delete"}: + return self._emit_polyline_call(func_name, node) # Skip visual/unsupported namespace calls if namespace in SKIP_NAMESPACES or namespace in SKIP_VAR_TYPES: @@ -2990,10 +2993,8 @@ def _visit_func_call(self, node: FuncCall) -> str: ) # Drawing casts of na use the same missing handle as a typed binding. - if (namespace is None and func_name in ("line", "box") - and len(node.args) == 1 and not node.kwargs - and self._is_na_expr(node.args[0])): - return f"{DRAWING_TYPE_TO_CPP[func_name]}{{}}" + if cast_kind := na_reference_cast_kind(node): + return f"{DRAWING_TYPE_TO_CPP[cast_kind]}{{}}" # Type cast functions: int(x), float(x), bool(x), string(x) if func_name == "int" and namespace is None and node.args: @@ -3277,20 +3278,7 @@ def _visit_func_call(self, node: FuncCall) -> str: try: val = self._visit_rhs_value( value_node, - target_cpp_type=( - f_cpp_type - if (self._is_nullable_collection_cpp_type(f_cpp_type) - or (f_cpp_type == "std::vector" - and ("field", namespace, f.name) - in self._array_widths.wide) - or f_cpp_type in self._udt_defs - or f_cpp_type in DRAWING_TYPE_TO_CPP.values() - # A bare na for a string field is the typed - # string na, not the numeric ``na()``. - or (f_cpp_type == "std::string" - and self._is_na_expr(value_node))) - else None - ), + target_cpp_type=f_cpp_type, ) finally: self._array_ctor_expected = previous_expected diff --git a/pineforge_codegen/codegen/visit_expr.py b/pineforge_codegen/codegen/visit_expr.py index da1f99a1..3139d9d5 100644 --- a/pineforge_codegen/codegen/visit_expr.py +++ b/pineforge_codegen/codegen/visit_expr.py @@ -506,6 +506,8 @@ def _drawing_target_cpp_type( self, target_name: str | None, target_cpp_type: str | None, + *, + target_node=None, ) -> str | None: """Resolve a drawing target without leaking a shadowed global type. @@ -520,6 +522,11 @@ def _drawing_target_cpp_type( if target_cpp_type in DRAWING_TYPE_TO_CPP.values() else None ) + if target_node is not None: + spec = self._type_spec_from_expr(target_node) + if (spec is not None and spec.kind == "udt" + and spec.name in DRAWING_TYPE_TO_CPP): + return DRAWING_TYPE_TO_CPP[spec.name] if not target_name: return None if target_name in self._lexical_drawing_types: @@ -547,7 +554,7 @@ def _drawing_target_cpp_type( # later local declaration pre-shadow earlier statements. Exact # top-level types are captured separately because the analyzer's flat # raw-name registry can be overwritten by a same-named local. - return self._global_drawing_cpp_types.get(target_name) + return getattr(self, "_global_drawing_cpp_types", {}).get(target_name) def _udt_target_cpp_type( self, diff --git a/pineforge_codegen/codegen/visit_stmt.py b/pineforge_codegen/codegen/visit_stmt.py index ffdb77e0..5ec6915d 100644 --- a/pineforge_codegen/codegen/visit_stmt.py +++ b/pineforge_codegen/codegen/visit_stmt.py @@ -1297,6 +1297,7 @@ def _visit_assignment(self, node: Assignment, lines: list[str], pad: str) -> Non selection_cpp_type = self._drawing_target_cpp_type( target_name, None, + target_node=node.target if target_name is None else None, ) if selection_cpp_type is None: selection_cpp_type = self._int_slot_cpp_type(target_name) @@ -1325,14 +1326,9 @@ def _visit_assignment(self, node: Assignment, lines: list[str], pad: str) -> Non # Get target name if target_name is None: - # Assignment to a UDT field that was dropped from the emitted - # struct because it had a drawing-only type (label/line/box/ - # linefill/polyline/table/chart.point). The struct has no such - # member, so emit a placeholder comment instead of a real C++ - # assignment. We intentionally do NOT visit the RHS here: drawing - # constructors (label.new / line.new / ...) live in - # SKIP_NAMESPACES, so they have no observable side effects in - # the backtest runtime. See: pineforge-codegen issue #10. + # Retain the existing omission of visual-only table fields. + # Drawing and opaque polyline handles are real record members and + # follow the typed assignment path below. if self._is_omitted_udt_field(node.target): recv = self._visit_expr(node.target.object) omitted = self._cpp_comment_escape(f"{recv}.{node.target.member} {node.op}") @@ -1347,6 +1343,14 @@ def _visit_assignment(self, node: Assignment, lines: list[str], pad: str) -> Non target_cpp_type = self._udt_target_cpp_type( target_node=node.target, ) + if target_cpp_type is None: + target_cpp_type = self._drawing_target_cpp_type( + None, None, target_node=node.target, + ) + if target_cpp_type is None: + field_spec = self._type_spec_from_expr(node.target) + if field_spec is not None and field_spec.kind == "array": + target_cpp_type = self._type_spec_to_cpp(field_spec) if (target_cpp_type is None and self._is_na_expr(node.value) and self._udt_field_is_string(node.target)): # A bare na assigned to a string field is the typed string na, diff --git a/pineforge_codegen/reference_casts.py b/pineforge_codegen/reference_casts.py new file mode 100644 index 00000000..a9c44e97 --- /dev/null +++ b/pineforge_codegen/reference_casts.py @@ -0,0 +1,38 @@ +"""Shared argument binding for missing drawing-reference casts.""" + +from .ast_nodes import FuncCall, Identifier, NaLiteral +from .signatures import FuncSig, Param, merge_kwargs_to_positional +from .symbols import PineType + + +_REFERENCE_CAST_KINDS = frozenset({"line", "box", "label", "linefill", "polyline"}) +_REFERENCE_CAST_SIGNATURE = FuncSig( + params=[Param("x", PineType.NA)], return_type=PineType.UNKNOWN, +) + + +def na_reference_cast_kind(node) -> str | None: + """Return the reference kind for a valid ``kind(na)`` / ``kind(x=na)``. + + Bind the same one-slot signature in analysis, default initialization and + expression emission. Malformed or non-NA calls retain normal diagnostics. + """ + if (not isinstance(node, FuncCall) + or not isinstance(node.callee, Identifier) + or node.callee.name not in _REFERENCE_CAST_KINDS): + return None + names = [param.name for param in _REFERENCE_CAST_SIGNATURE.params] + if (len(node.args) > len(names) + or set(node.kwargs) - set(names) + or any(name in node.kwargs for name in names[:len(node.args)])): + return None + arguments = merge_kwargs_to_positional( + _REFERENCE_CAST_SIGNATURE, node.args, node.kwargs, + ) + if len(arguments) != len(names): + return None + value = arguments[0] + if (isinstance(value, NaLiteral) + or (isinstance(value, Identifier) and value.name == "na")): + return node.callee.name + return None diff --git a/tests/test_codegen_drawing_data.py b/tests/test_codegen_drawing_data.py index e349dcc9..303859fd 100644 --- a/tests/test_codegen_drawing_data.py +++ b/tests/test_codegen_drawing_data.py @@ -302,7 +302,7 @@ def test_drawing_handle_ternary_na_local_reassignment_compile(): strategy.entry("L", strategy.long) ''' cpp = transpile(src) - body = cpp.split("Line update(Line& initial) {", 1)[1].split(" }", 1)[0] + body = cpp.split("Line update(Line initial) {", 1)[1].split(" }", 1)[0] assert "h = ((cond) ? (_pf_collect_lines_(pf_line_new" in body assert ": (Line{}));" in body assert "na()" not in body diff --git a/tests/test_drawing_type_casts.py b/tests/test_drawing_type_casts.py index 533b2516..d84c0084 100644 --- a/tests/test_drawing_type_casts.py +++ b/tests/test_drawing_type_casts.py @@ -5,9 +5,13 @@ from pineforge_codegen import transpile -@pytest.mark.parametrize("kind,cpp_type", [("line", "Line"), ("box", "Box")]) +@pytest.mark.parametrize( + "kind,cpp_type", + [("line", "Line"), ("box", "Box"), ("label", "Label"), ("linefill", "Linefill")], +) @pytest.mark.parametrize("name", ["handle", "guide"]) -def test_na_drawing_cast_matches_typed_na_declaration(kind, cpp_type, name): +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +def test_na_drawing_cast_matches_typed_na_declaration(kind, cpp_type, name, named): def source(value): return ( '//@version=6\nstrategy("Drawing casts", overlay=true)\n' @@ -15,7 +19,8 @@ def source(value): f'{kind}.delete({name})\nplot(close)\n' ) - cpp = transpile(source(f"{kind}(na)")) + argument = "x=na" if named else "na" + cpp = transpile(source(f"{kind}({argument})")) control = transpile(source("na")) initializers = lambda text: [ line.strip() for line in text.splitlines() diff --git a/tests/test_reference_handle_lowering.py b/tests/test_reference_handle_lowering.py new file mode 100644 index 00000000..6a539ff1 --- /dev/null +++ b/tests/test_reference_handle_lowering.py @@ -0,0 +1,152 @@ +"""Reference values keep their storage types across fields and user calls.""" + +import re + +import pytest + +from pineforge_codegen import transpile +from tests._compile import compile_cpp + + +@pytest.mark.parametrize( + "kind,cpp_type", + [ + ("line", "Line"), + ("box", "Box"), + ("label", "Label"), + ("linefill", "Linefill"), + ("chart.point", "ChartPoint"), + ("polyline", "_PFPolyline"), + ], +) +def test_reference_field_na_uses_the_field_type(kind, cpp_type): + source = f'''//@version=6 +strategy("Reference field") +type Holder + {kind} drawingRef = na +var holderRef = Holder.new() +holderRef.drawingRef := na +''' + cpp = transpile(source) + assert f"{cpp_type} drawingRef" in cpp + assert f"_pf_udt_Holder.get(holderRef).drawingRef = {cpp_type}{{}};" in cpp + compile_cpp(cpp, label=f"reference-field-na-{kind}") + + +@pytest.mark.parametrize("element", ["float", "int", "string", "line", "polyline"]) +def test_array_field_na_default_has_collection_storage(element): + source = f'''//@version=6 +strategy("Collection field") +type Bag + array<{element}> xs = na +var bagRef = Bag.new() +bagRef.xs := array.new<{element}>() +plot(bagRef.xs.size()) +bagRef.xs := na +''' + cpp = transpile(source) + assert ".xs = na()" not in cpp + assert "_PFUdtRecord_Bag{" in cpp + compile_cpp(cpp, label=f"collection-field-na-{element}") + + +@pytest.mark.parametrize( + "kind,cpp_type,factory", + [ + ("line", "Line", "line.new(bar_index, low, bar_index, high)"), + ("box", "Box", "box.new(bar_index, high, bar_index, low)"), + ("label", "Label", 'label.new(bar_index, high, "marker")'), + ], +) +@pytest.mark.parametrize("typed_parameter", [True, False], ids=["typed", "untyped"]) +def test_handle_parameter_alias_and_tuple_are_values_at_each_call( + kind, cpp_type, factory, typed_parameter +): + hint = f"{kind} " if typed_parameter else "" + source = f'''//@version=6 +strategy("Handle values") +mark({hint}initialHandle, bool replaceHandle) => + aliasHandle = initialHandle + if replaceHandle + aliasHandle := {factory} + [aliasHandle, bar_index] +var {kind} storedHandleA = na +var {kind} storedHandleB = na +[returnedA, indexA] = mark(storedHandleA, close > open) +[returnedB, indexB] = mark(storedHandleB, close < open) +storedHandleA := returnedA +storedHandleB := returnedB +''' + cpp = transpile(source) + definitions = re.findall( + rf"std::tuple<{cpp_type}, int> mark(?:_cs\d+)?" + rf"\({cpp_type} initialHandle, bool replaceHandle\)", + cpp, + ) + assert definitions + assert f"std::tuple<{cpp_type}&" not in cpp + assert "storedHandleA = returnedA;" in cpp + assert "storedHandleB = returnedB;" in cpp + compile_cpp(cpp, label=f"handle-value-boundaries-{kind}-{typed_parameter}") + + +def test_box_field_can_bind_directly_to_a_user_parameter(): + source = '''//@version=6 +strategy("Field argument") +type Holder + box drawingRef = na +extend(box drawingRef) => + box.set_right(drawingRef, bar_index) +var holderRef = Holder.new() +if barstate.isfirst + holderRef.drawingRef := box.new(bar_index, high, bar_index, low) +extend(holderRef.drawingRef) +''' + cpp = transpile(source) + assert "extend(Box drawingRef)" in cpp + assert "extend(_pf_udt_Holder.read(holderRef).drawingRef)" in cpp + compile_cpp(cpp, label="box-field-user-parameter") + + +@pytest.mark.parametrize( + "deletion", + [ + "polyline priorHandle = done.shift()\npriorHandle.delete()", + "priorHandle = done.shift()\npriorHandle.delete()", + "done.shift().delete()", + ], + ids=["typed", "untyped", "chained"], +) +def test_polyline_array_element_keeps_its_handle_type(deletion): + source = '''//@version=6 +strategy("Polyline element") +var array done = array.new() +points = array.from(chart.point.from_index(bar_index, low), chart.point.from_index(bar_index, high)) +done.push(polyline.new(points)) +''' + deletion + "\n" + cpp = transpile(source) + assert "std::vector<_PFPolyline>" in cpp + assert "None(" not in cpp + assert "polyline priorHandle" not in cpp + compile_cpp(cpp, label="polyline-array-element") + + +def test_polyline_field_and_cast_preserve_typed_reads_and_delete(): + source = '''//@version=6 +strategy("Polyline field") +type Holder + polyline drawingRef = na +var holderRef = Holder.new() +points = array.from(chart.point.from_index(bar_index, low), chart.point.from_index(bar_index, high)) +holderRef.drawingRef := polyline.new(points = points) +polyline heldHandle = holderRef.drawingRef +heldHandle.delete() +polyline.delete(id = holderRef.drawingRef) +holderRef.drawingRef := polyline(x = na) +''' + cpp = transpile(source) + assert "_PFPolyline drawingRef" in cpp + assert "_pf_udt_Holder.read(holderRef).drawingRef" in cpp + assert "_pf_udt_Holder.get(holderRef).drawingRef = _PFPolyline{};" in cpp + assert "None(" not in cpp + compile_cpp(cpp, label="polyline-field-read-delete") diff --git a/tests/test_udt_drawing_field_cleanup.py b/tests/test_udt_drawing_field_cleanup.py index 6818324b..f14f94a8 100644 --- a/tests/test_udt_drawing_field_cleanup.py +++ b/tests/test_udt_drawing_field_cleanup.py @@ -6,7 +6,8 @@ ``std::vector upln;``), and ``m.tag := label.new(...)`` / ``x = m.tag`` become real field read/writes against the shared per-type arena. -(``table``/``polyline`` fields are still dropped — they have no C++ type.) +``polyline`` fields retain opaque handles without rendering; ``table`` fields +are still dropped. """ from pineforge_codegen import transpile From cb93e2d14ffd34c9b15773c81a886efe3b0ac4f0 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 07:07:59 +0800 Subject: [PATCH 05/27] Handle emitters without function metadata Read optional function metadata safely when inferring reference casts, so bare emitters retain matrix element types without raising an AttributeError. --- pineforge_codegen/codegen/types.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pineforge_codegen/codegen/types.py b/pineforge_codegen/codegen/types.py index cbc517c0..f7f69bc0 100644 --- a/pineforge_codegen/codegen/types.py +++ b/pineforge_codegen/codegen/types.py @@ -3144,7 +3144,7 @@ def _type_spec_from_expr_raw( targs = self._template_args_from_call(node) # Drawing-objects-as-data return typing (spec §4.5 DRAWING_RETURN_SPECS): # *.new / *.copy -> handle of the self-type; linefill.get_line* -> line. - if (func_name not in self._func_info_map + if (func_name not in getattr(self, "_func_info_map", {}) and (cast_kind := na_reference_cast_kind(node))): return TypeSpec.udt(cast_kind) if namespace == "polyline" and func_name == "new": @@ -6352,7 +6352,7 @@ def _infer_type( return "double" if isinstance(node, FuncCall): func_name, namespace = self._resolve_callee(node.callee) - if (func_name not in self._func_info_map + if (func_name not in getattr(self, "_func_info_map", {}) and (cast_kind := na_reference_cast_kind(node))): return DRAWING_TYPE_TO_CPP[cast_kind] if namespace == "polyline" and func_name == "new": From 96297f9d97c71ff581f57a41049640e19c6ed3da Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 07:28:04 +0800 Subject: [PATCH 06/27] Retain structured value types across user function calls --- CHANGELOG.md | 5 + pineforge_codegen/analyzer/base.py | 179 ++++++++++++++++-- pineforge_codegen/analyzer/call_handlers.py | 33 +++- pineforge_codegen/analyzer/contracts.py | 6 + pineforge_codegen/analyzer/types.py | 120 +++++++++++- pineforge_codegen/codegen/base.py | 31 +++- pineforge_codegen/codegen/emit_top.py | 38 +++- pineforge_codegen/codegen/types.py | 9 + tests/test_callable_compound_type_flow.py | 195 ++++++++++++++++++++ 9 files changed, 594 insertions(+), 22 deletions(-) create mode 100644 tests/test_callable_compound_type_flow.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 83af3833..cc444443 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,11 @@ supported as exact pairs; on the 0.x line they are independent. See the in the same lambda; on ENOMEM the environment stays unchanged and the conversion silently uses the previous zone. +- User functions retain array, object and string result types through branch + returns and history clones. Untyped arguments retain collection element + types through nested calls and tuple bindings, and reassigned matrix locals + keep their initializer's matrix type. (#391) + ## 1.12.0 — 2026-11-12 - Pine v6 reports located errors for a missing `color.new` transparency, diff --git a/pineforge_codegen/analyzer/base.py b/pineforge_codegen/analyzer/base.py index f3385cf2..b96347e4 100644 --- a/pineforge_codegen/analyzer/base.py +++ b/pineforge_codegen/analyzer/base.py @@ -309,6 +309,13 @@ def __init__(self, ast: Program, filename: str = "", # Probe: data/validation/udt-method-probe-20-udt-return-from-func. self._func_udt_return_types: dict[str, str] = {} self._func_return_type_specs: dict[str, "TypeSpec"] = {} + self._func_tuple_element_specs: dict[str, tuple] = {} + self._callable_bound_param_specs_by_node: dict[int, tuple] = {} + self._callable_bound_return_specs_by_node: dict[int, TypeSpec] = {} + self._callable_edge_specs: dict = {} + self._callable_return_specs_by_profile: dict = {} + self._func_callsite_param_specs: dict = {} + self._func_callsite_return_specs: dict = {} self._func_param_type_specs: dict[str, list] = {} # Direct terminal gets on an unshadowed built-in temporary are # registered while their lexical scope is live. A later-defined UDF @@ -769,6 +776,24 @@ def _analyze(self) -> AnalyzerContext: func_callsite_return_types=dict( self._func_callsite_return_types ), + func_callsite_param_specs={ + **{ + key: self._callable_bound_param_specs_by_node[node_id] + for node_id, key in self._func_call_cs_map.items() + if node_id in self._callable_bound_param_specs_by_node + }, + **self._func_callsite_param_specs, + }, + func_callsite_return_specs={ + **{ + key: self._callable_bound_return_specs_by_node[node_id] + for node_id, key in self._func_call_cs_map.items() + if node_id in self._callable_bound_return_specs_by_node + }, + **self._func_callsite_return_specs, + }, + func_value_call_edges=dict(self._callable_edge_specs), + func_value_return_specs=dict(self._callable_return_specs_by_profile), func_declared_param_type_specs={ name: tuple(specs) for name, specs in self._func_param_type_specs.items() @@ -3370,6 +3395,52 @@ def _edge_ta_variant_target_map( func_info_by_name, _bound_user_call_args, ) + self._resolve_callable_callsite_value_specs(call_edges, func_info_by_name) + + def _resolve_callable_callsite_value_specs(self, call_edges, infos): + """Bind exact value profiles to the variants the emitter actually uses.""" + profiles = self._func_callsite_param_specs + for owner, callee, call in call_edges: + target = self._func_call_cs_map.get(id(call)) + if owner is None and target is not None: + profile = self._callable_bound_param_specs_by_node.get(id(call)) + if profile is not None: + profiles[target] = profile + for _ in range(len(call_edges) + 1): + changed = False + for owner, callee, call in call_edges: + if owner is None or owner not in infos: + continue + info = infos[owner] + if info.node is None: + continue + count = self._func_call_site_count.get(owner, 0) + for owner_cs in range(count) if count else (None,): + profile = profiles.get( + (owner, owner_cs), tuple(info.param_type_specs), + ) + incoming = self._callable_edge_specs.get( + (owner, profile, id(call)) + ) + if incoming is None: + continue + target = self._func_call_cs_map.get(id(call)) + if target is None: + callee_count = self._func_call_site_count.get(callee, 0) + if owner_cs is None or owner_cs >= callee_count: + continue + target = (callee, owner_cs) + if target not in profiles: + profiles[target] = incoming + changed = True + if not changed: + break + if self._budget is not None: + self._budget.check(phase=Phase.ANALYZER) + for (name, cs), profile in profiles.items(): + exact = self._callable_return_specs_by_profile.get((name, profile)) + if exact is not None: + self._func_callsite_return_specs[(name, cs)] = exact # The primitive families a written call can pass an untyped parameter. _ARGUMENT_FAMILIES = frozenset({ @@ -3443,6 +3514,9 @@ def reads(value, names: set[str]) -> bool: owner_params = set(untyped_params(owner).values()) actuals = bound_args(callee, call) recorded = self._callable_bound_param_types_by_node.get(id(call), []) + recorded_specs = self._callable_bound_param_specs_by_node.get( + id(call), () + ) for index in params: if (index >= len(actuals) or index >= len(recorded) or actuals[index] is None @@ -3450,6 +3524,19 @@ def reads(value, names: set[str]) -> bool: continue family = recorded[index] shared = shared_family(callee, index) + info_specs = func_info_by_name[callee].param_type_specs + incoming_spec = ( + recorded_specs[index] if index < len(recorded_specs) else None + ) + shared_spec = info_specs[index] if index < len(info_specs) else None + if (incoming_spec is not None and shared_spec is not None + and incoming_spec != shared_spec + and (incoming_spec.kind != "primitive" + or shared_spec.kind != "primitive")): + # Structured types are argument families too. In particular + # vector& cannot receive a vector argument. + conflicted.add(callee) + break if (family in self._ARGUMENT_FAMILIES and shared in self._ARGUMENT_FAMILIES and family != shared @@ -5563,6 +5650,12 @@ def _visit_TupleAssign(self, node: TupleAssign) -> PineType: is_val_static = self._is_static_expression(node.value) declared_types: list[PineType] = [] + tuple_specs = ( + self._func_tuple_element_specs.get(node.value.callee.name, ()) + if isinstance(node.value, FuncCall) + and isinstance(node.value.callee, Identifier) + else () + ) for idx, name in enumerate(node.names): inferred_element_type = ( @@ -5612,6 +5705,13 @@ def _visit_TupleAssign(self, node: TupleAssign) -> PineType: setattr(sym, "_pf_decl_node_id", id(node)) setattr(sym, "_pf_decl_binding_name", name) setattr(sym, "_pf_tuple_binding", True) + if (idx < len(tuple_specs) and tuple_specs[idx] is not None + and (tuple_specs[idx].kind != "primitive" + or tuple_specs[idx].name == "string")): + sym.type_spec = tuple_specs[idx] + self._record_collection_type( + name, tuple_specs[idx], symbol_scope=sym.scope, + ) self._record_script_block_binding(sym, outer_symbol) self._note_member_writer(sym, name, node) @@ -5823,6 +5923,18 @@ def _visit_FuncDef(self, node: FuncDef) -> PineType: terminal_direct_return_spec = self._type_spec_from_expr( terminal_ret_expr ) + if isinstance(terminal_ret_expr, TupleLiteral): + self._func_tuple_element_specs[node.name] = tuple( + self._type_spec_from_expr(element) + for element in terminal_ret_expr.elements + ) + elif (isinstance(terminal_ret_expr, FuncCall) + and isinstance(terminal_ret_expr.callee, Identifier)): + self._func_tuple_element_specs[node.name] = ( + self._func_tuple_element_specs.get( + terminal_ret_expr.callee.name, () + ) + ) terminal_map_return = self._terminal_map_call_return( terminal_ret_expr, { @@ -8358,35 +8470,41 @@ def _validate_deferred_param_history_refs( ) validated.add(key) - def _propagate_deferred_map_callable_specs( + def _propagate_deferred_callable_specs( self, owner: str, parameter_specs: dict[str, TypeSpec | None], visiting: frozenset[str] = frozenset(), + _memo: set | None = None, ) -> bool: - """Monomorphize an all-untyped wrapper chain for one concrete map call. + """Carry exact value types through deferred callable argument edges. Function definitions are analyzed before their concrete top-level call sites. Consequently ``wrapper(a) => identity(a)`` initially creates both ``FuncInfo`` records with scalar fallbacks. The definition pass already captured identity-keyed parameter-flow edges for deferred map - history validation; reuse those exact edges to carry a later map - ``TypeSpec`` inward, then infer map returns on the way back out. - - This is deliberately bounded and map-triggered. Cycles stop at the - active owner, incompatible previously-established parameter specs stop - that edge, and scalar-only call graphs never mutate. PineForge emits - one C++ body per ordinary UDF, so silently replacing one concrete map - specialization with a different one would be a false polymorphic - inference rather than a valid widening. + history validation; reuse those exact edges to carry the concrete + ``TypeSpec`` inward, then infer returns on the way back out. + + Queries are memoized by function and input types, never by call path. + A diamond therefore visits each concrete signature once. Existing map + incompatibility diagnostics remain authoritative; other distinct value + families are retained at their written calls for clone specialization. """ if owner in visiting: return False if not any( - spec is not None and spec.kind == "map" + spec is not None + and (spec.kind != "primitive" or spec.name == "string") for spec in parameter_specs.values() ): return False + if _memo is None: + _memo = set() + profile = (owner, tuple(parameter_specs.items())) + if profile in _memo: + return False + _memo.add(profile) func_info = next( (info for info in self._func_infos if info.name == owner), @@ -8395,6 +8513,8 @@ def _propagate_deferred_map_callable_specs( func_def = self._func_defs.get(owner) if func_info is None or func_def is None: return False + if self._budget is not None: + self._budget.check(func_def.loc, Phase.ANALYZER) changed = False while len(func_info.param_type_specs) < len(func_def.params): @@ -8439,7 +8559,7 @@ def _propagate_deferred_map_callable_specs( for _ in range(max(1, len(edges) + 1)): pass_changed = False for ( - _call_id, + call_id, callee_owner, callee_param_names, bound_args, @@ -8493,10 +8613,19 @@ def _propagate_deferred_map_callable_specs( ) ): callee_specs[param_name] = contextual_map_spec - if self._propagate_deferred_map_callable_specs( + self._callable_bound_param_specs_by_node[call_id] = tuple( + callee_specs.get(name) for name in callee_param_names + ) + self._callable_edge_specs[( + owner, + tuple(parameter_specs.get(name) for name in func_def.params), + call_id, + )] = self._callable_bound_param_specs_by_node[call_id] + if self._propagate_deferred_callable_specs( callee_owner, callee_specs, next_visiting, + _memo, ): pass_changed = True changed = changed or pass_changed @@ -8506,6 +8635,16 @@ def _propagate_deferred_map_callable_specs( terminal = self._direct_terminal_return_expr(func_def) return_spec = None scalar_return_type = None + exact = self._callable_value_return_spec(func_def, parameter_specs) + if exact is not None and ( + exact.kind != "primitive" or exact.name == "string" + ): + return_spec = exact + self._callable_return_specs_by_profile[( + owner, tuple(parameter_specs.get(name) for name in func_def.params), + )] = exact + if exact.kind == "primitive": + scalar_return_type = self._primitive_pine_type_from_spec(exact) if isinstance(terminal, Identifier) and terminal.name in parameter_specs: candidate = parameter_specs.get(terminal.name) if candidate is not None and candidate.kind == "map": @@ -8532,7 +8671,9 @@ def _propagate_deferred_map_callable_specs( return_spec = candidate if return_spec is not None: established_return = self._func_return_type_specs.get(owner) - if established_return is None: + if (established_return is None + or (established_return.kind == "primitive" + and return_spec.kind != "primitive")): self._func_return_type_specs[owner] = return_spec func_info.return_type_spec = return_spec changed = True @@ -8540,6 +8681,14 @@ def _propagate_deferred_map_callable_specs( if func_info.return_type_spec is None: func_info.return_type_spec = return_spec changed = True + if return_spec.kind == "primitive": + result_type = self._primitive_pine_type_from_spec(return_spec) + if result_type != PineType.UNKNOWN: + func_info.return_type = result_type + self._func_return_types[owner] = result_type + elif return_spec.kind == "udt": + func_info.udt_return_type = return_spec.name + self._func_udt_return_types[owner] = return_spec.name # A different established return belongs to another concrete map # specialization. Keep it unchanged rather than falsely widening. diff --git a/pineforge_codegen/analyzer/call_handlers.py b/pineforge_codegen/analyzer/call_handlers.py index 8fef0994..7f25b6b2 100644 --- a/pineforge_codegen/analyzer/call_handlers.py +++ b/pineforge_codegen/analyzer/call_handlers.py @@ -1924,6 +1924,7 @@ def _handle_user_func_call(self, func_name: str, node: FuncCall) -> PineType: for i in range(len(param_specs)): if param_specs[i] is None and i < len(arg_specs): param_specs[i] = arg_specs[i] + self._callable_bound_param_specs_by_node[id(node)] = tuple(param_specs) self._record_deferred_param_call_edge( node, func_name, @@ -2171,13 +2172,43 @@ def _handle_user_func_call(self, func_name: str, node: FuncCall) -> PineType: # scalar fallbacks. Propagate the concrete specs through the exact # definition-time call edges and infer returns in post-order before # codegen snapshots the FuncInfo table. - self._propagate_deferred_map_callable_specs( + self._propagate_deferred_callable_specs( func_name, { name: spec for name, spec in zip(func_def.params, param_specs) }, ) + value_specs = [*param_specs, fi.return_type_spec] + exact_return = ( + self._callable_value_return_spec( + func_def, dict(zip(func_def.params, param_specs)), + ) + if any( + spec is not None + and (spec.kind != "primitive" or spec.name == "string") + for spec in value_specs + ) + else None + ) + if exact_return is not None and ( + exact_return.kind != "primitive" or exact_return.name == "string" + ): + self._callable_bound_return_specs_by_node[id(node)] = exact_return + self._callable_return_specs_by_profile[ + (func_name, tuple(param_specs)) + ] = exact_return + if (fi.return_type_spec is None + or fi.return_type_spec.kind == "primitive"): + fi.return_type_spec = exact_return + self._func_return_type_specs[func_name] = exact_return + if exact_return.kind == "primitive": + return_type = self._primitive_pine_type_from_spec(exact_return) + fi.return_type = return_type + self._func_return_types[func_name] = return_type + elif exact_return.kind == "udt": + fi.udt_return_type = exact_return.name + self._func_udt_return_types[func_name] = exact_return.name if return_type == PineType.NA: self._na_return_callables[id(node)] = func_def diff --git a/pineforge_codegen/analyzer/contracts.py b/pineforge_codegen/analyzer/contracts.py index 93478b1c..9b7a39a4 100644 --- a/pineforge_codegen/analyzer/contracts.py +++ b/pineforge_codegen/analyzer/contracts.py @@ -293,6 +293,12 @@ class AnalyzerContext: # helper called with both ``bar_index`` and ``close``). func_callsite_param_types: dict = field(default_factory=dict) func_callsite_return_types: dict = field(default_factory=dict) + # Exact argument/return value types retain collection element types, object + # identities and strings independently of the coarse primitive profiles. + func_callsite_param_specs: dict = field(default_factory=dict) + func_callsite_return_specs: dict = field(default_factory=dict) + func_value_call_edges: dict = field(default_factory=dict) + func_value_return_specs: dict = field(default_factory=dict) # Declaration-time parameter specs, before any call-site inference fills # untyped slots on the legacy FuncInfo record. Codegen uses this to apply # variant overrides only to genuinely untyped parameters. diff --git a/pineforge_codegen/analyzer/types.py b/pineforge_codegen/analyzer/types.py index 6c8fb0be..55302447 100644 --- a/pineforge_codegen/analyzer/types.py +++ b/pineforge_codegen/analyzer/types.py @@ -294,7 +294,7 @@ def _selection_type_spec( *, nullable: bool | None = None, ) -> TypeSpec | None: - """Keep exact collection joins; extend other families only around ``na``.""" + """A selection of identical value types retains that exact type.""" if nullable is None: nullable = any(self._selection_contains_na(node) for node, _ in branches) result: TypeSpec | None = None @@ -307,8 +307,6 @@ def _selection_type_spec( spec = TypeSpec.primitive("color") if spec is None: return None - if not nullable and spec.kind not in {"map", "matrix"}: - return None result = ( spec if result is None @@ -522,6 +520,11 @@ def direct_user_udt_ctor_name(node: ASTNode) -> str | None: )) return self._selection_type_spec(branches) if isinstance(value, FuncCall): + exact = getattr( + self, "_callable_bound_return_specs_by_node", {} + ).get(id(value)) + if exact is not None: + return exact cal = value.callee if (isinstance(cal, Identifier) and cal.name in ("line", "box") and len(value.args) == 1 and not value.kwargs @@ -869,6 +872,27 @@ def _callsite_terminal_return_spec( self._current_top_level_stmt = previous_owner self._symbols.current_scope = previous_scope + def _callable_value_return_spec(self, func_def, parameter_specs): + """Read a callable's value with its lexical locals and concrete inputs. + + This query never revisits the analyzer or allocates stateful call sites. + Each branch gets its own environment, so an expired local cannot borrow + the type of a same-named global or another branch's declaration. + """ + previous_owner = self._current_top_level_stmt + previous_scope = self._symbols.current_scope + self._symbols.current_scope = self._operator_definition_scopes.get( + id(func_def), self._symbols.global_scope, + ) + self._current_top_level_stmt = func_def + try: + return OperatorTypeQuery(self, dict(parameter_specs)).body_value_spec( + func_def.body + ) + finally: + self._current_top_level_stmt = previous_owner + self._symbols.current_scope = previous_scope + def _terminal_map_selection_return_spec( self, value: ASTNode | None, @@ -1442,6 +1466,79 @@ def check_body(self, body): for node in body: self._check_node(node) + def body_value_spec(self, body): + """Infer declaration and terminal value types without checking operators.""" + result = None + for node in body or (): + if self._analyzer._budget is not None: + self._analyzer._budget.check(node.loc, Phase.ANALYZER) + if isinstance(node, VarDecl): + result = ( + self._type_spec_from_hint(node.type_hint) + if node.type_hint else self._value_spec(node.value) + ) + self._parameter_types[node.name] = result + elif isinstance(node, Assignment): + result = ( + self._parameter_types.get(node.target.name) + if isinstance(node.target, Identifier) + else self._value_spec(node.target) + ) + if result is None: + result = self._value_spec(node.value) + elif isinstance(node, TupleAssign): + # Tuple elements are independent value bindings. + specs = () + if isinstance(node.value, FuncCall) and isinstance( + node.value.callee, Identifier + ): + specs = getattr( + self._analyzer, "_func_tuple_element_specs", {} + ).get(node.value.callee.name, ()) + for index, name in enumerate(node.names): + self._parameter_types[name] = ( + specs[index] if index < len(specs) else None + ) + result = None + elif isinstance(node, IfStmt): + branches = [ + OperatorTypeQuery( + self._analyzer, dict(self._parameter_types) + ).body_value_spec(branch) + for branch in (node.body, node.else_body) + ] + result = unify_type_specs( + *branches, + left_is_na=not node.body, + right_is_na=not node.else_body, + ) + elif isinstance(node, SwitchStmt): + result = None + first = True + for branch in [ + *(body for _, body in node.cases), node.default_body + ]: + spec = OperatorTypeQuery( + self._analyzer, dict(self._parameter_types) + ).body_value_spec(branch) + result = spec if first else unify_type_specs( + result, spec, right_is_na=not branch, + ) + first = False + elif isinstance(node, ExprStmt): + result = self._value_spec(node.expr) + else: + result = self._value_spec(node) + return result + + def _value_spec(self, value): + spec = self._type_spec_from_expr(value) + if spec is None: + spec = self._analyzer._operator_operand_type( + value, self._parameter_types + ) + return spec + def _check_node(self, node): if not isinstance(node, ASTNode): if isinstance(node, (tuple, list)): @@ -1538,6 +1635,23 @@ def _type_spec_from_expr(self, value, **kwargs): return self._analyzer._operator_operand_type( value, self._parameter_types, ) + if (isinstance(value, FuncCall) + and isinstance(value.callee, Identifier) + and value.callee.name in self._analyzer._func_defs): + name = value.callee.name + definition = self._analyzer._func_defs[name] + args = self._analyzer._bind_callable_args( + value, list(definition.params) + ) + profile = tuple( + self._value_spec(arg) if arg is not None else None + for arg in args + ) + exact = self._analyzer._callable_return_specs_by_profile.get( + (name, profile) + ) + if exact is not None: + return exact return super()._type_spec_from_expr(value, **kwargs) def _visit(self, node): diff --git a/pineforge_codegen/codegen/base.py b/pineforge_codegen/codegen/base.py index 6bd0a8ad..fdb388ba 100644 --- a/pineforge_codegen/codegen/base.py +++ b/pineforge_codegen/codegen/base.py @@ -2145,6 +2145,26 @@ def natural_name(fname: str, cs_idx: int) -> str: owns_non_ta_state = bool( g_var_originals or fixnan_originals(g_name) ) + owner_info = self._func_info_map.get(inst["fname"]) + owner_profile = inst.get("param_specs") + if owner_profile is None and owner_info is not None: + owner_profile = getattr( + ctx, "func_callsite_param_specs", {} + ).get( + (inst["fname"], inst.get("type_call_site_idx", + inst.get("call_site_idx"))), + tuple(owner_info.param_type_specs), + ) + incoming = getattr(ctx, "func_value_call_edges", {}).get( + (inst["fname"], owner_profile, id(callnode)) + ) + natural_profile = getattr( + ctx, "func_callsite_param_specs", {} + ).get((g_name, j)) + different_value_type = ( + incoming is not None and natural_profile is not None + and incoming != natural_profile + ) # Every non-cs0 enclosing variant is a distinct Pine written # call path. Even a pure wrapper can reach var/fixnan state # farther down the graph, so preserve the path identity here @@ -2155,6 +2175,7 @@ def natural_name(fname: str, cs_idx: int) -> str: ) if ( not path_requires_fresh_state + and not different_value_type and composed_ta == natural_ta and composed_var == natural_var and composed_fixnan == natural_fixnan @@ -2174,6 +2195,7 @@ def natural_name(fname: str, cs_idx: int) -> str: frozenset(composed_var.items()), frozenset(composed_fixnan.items()), path_identity, + incoming, ) ginst = interned.get(key) if ginst is None: @@ -2210,6 +2232,10 @@ def natural_name(fname: str, cs_idx: int) -> str: # takes the argument that call passes, as the # call's own cs{j} clone does. "type_call_site_idx": j, + "param_specs": incoming, + "return_spec": getattr( + ctx, "func_value_return_specs", {} + ).get((g_name, incoming)), "ta_remap": composed_ta, "var_remap": fvar_remap, "fixnan_remap": ffixnan_remap, @@ -2547,7 +2573,10 @@ def _register_global_aggregate_member_types(self) -> None: self._collection_types.setdefault(stmt.name, declared_spec) continue else: - continue + # Reassignment removes a binding from global_expr_map. Its + # initializer still establishes its matrix handle type, also + # for declarations inside top-level control-flow blocks. + declared_spec = self._type_spec_from_expr(stmt.value) if (from_selection and declared_spec is not None and declared_spec.kind == "array"): # ``a = switch mode ...`` whose every arm is an array of one diff --git a/pineforge_codegen/codegen/emit_top.py b/pineforge_codegen/codegen/emit_top.py index d9c67b33..0f4b2b24 100644 --- a/pineforge_codegen/codegen/emit_top.py +++ b/pineforge_codegen/codegen/emit_top.py @@ -2407,6 +2407,37 @@ def _emit_func_def_body(self, fi: FuncInfo, lines: list[str], call_site_idx if call_site_idx is not None else (instance or {}).get("type_call_site_idx") ) + if type_call_site_idx is not None: + from dataclasses import replace + incoming = getattr( + self.ctx, "func_callsite_param_specs", {} + ).get((fi.name, type_call_site_idx), ()) + if (instance or {}).get("param_specs") is not None: + incoming = instance["param_specs"] + declared = getattr( + self.ctx, "func_declared_param_type_specs", {} + ).get(fi.name, ()) + specs = list(fi.param_type_specs) + while len(specs) < len(node.params): + specs.append(None) + for index, spec in enumerate(incoming): + if (index < len(specs) and spec is not None + and (spec.kind != "primitive" or spec.name == "string") + and (index >= len(declared) or declared[index] is None)): + specs[index] = spec + exact_return = getattr( + self.ctx, "func_callsite_return_specs", {} + ).get((fi.name, type_call_site_idx), fi.return_type_spec) + if (instance or {}).get("return_spec") is not None: + exact_return = instance["return_spec"] + fi = replace( + fi, param_type_specs=specs, return_type_spec=exact_return, + udt_return_type=( + exact_return.name + if exact_return is not None and exact_return.kind == "udt" + else fi.udt_return_type + ), + ) # Collection registries historically used raw variable names for the # whole translation unit. Emit each callable against copy-on-write @@ -2651,6 +2682,11 @@ def _emit_func_def_body(self, fi: FuncInfo, lines: list[str], ret_type = "int64_t" elif ("return", fi.name) in self._array_widths.wide: ret_type = self._type_spec_to_cpp(self._array_return_spec(fi)) + elif (getattr(fi, "return_type_spec", None) is not None + and fi.return_type_spec.kind != "primitive"): + # Exact value types also apply to history/stateful clones. Their + # coarse primitive profile cannot represent a collection result. + ret_type = self._type_spec_to_cpp(self._array_return_spec(fi)) elif ( type_call_site_idx is not None and self._callsite_callable_return_pine_type( @@ -2664,8 +2700,6 @@ def _emit_func_def_body(self, fi: FuncInfo, lines: list[str], "double", ) elif getattr(fi, "return_type_spec", None) is not None: - # Exact return TypeSpec (collections plus narrowly cached primitive - # terminal reads) takes precedence over the coarse PineType slot. ret_type = self._type_spec_to_cpp(self._array_return_spec(fi)) else: ret_type = PINE_TYPE_TO_CPP.get(fi.return_type, "double") diff --git a/pineforge_codegen/codegen/types.py b/pineforge_codegen/codegen/types.py index b6023534..945f693a 100644 --- a/pineforge_codegen/codegen/types.py +++ b/pineforge_codegen/codegen/types.py @@ -3068,6 +3068,15 @@ def _type_spec_from_expr_raw( return TypeSpec.udt(func_name) if namespace is None: func_info = self._func_info_map.get(func_name) + if func_info is not None: + exact = getattr( + self.ctx, "func_callsite_return_specs", {} + ).get(( + func_info.name, + self._callable_target_callsite_idx(func_info, node), + )) + if exact is not None: + return exact return_spec = (self._array_return_spec(func_info) if func_info is not None else None) if return_spec is not None: diff --git a/tests/test_callable_compound_type_flow.py b/tests/test_callable_compound_type_flow.py new file mode 100644 index 00000000..d759c3cc --- /dev/null +++ b/tests/test_callable_compound_type_flow.py @@ -0,0 +1,195 @@ +"""Structured values retain their types across callable boundaries.""" + +import re + +import pytest + +from pineforge_codegen import transpile +from tests._compile import compile_cpp + + +def signatures(cpp, name): + return re.findall( + rf"^ ([^\n]+?) {re.escape(name)}(?:_cs\d+|__ni\d+)?\(([^\n]*)\) \{{$", + cpp, + re.MULTILINE, + ) + + +CASES = [ + pytest.param( + """choose(array values, bool enabled) => + if enabled + values + else + values +items = array.from(close) +selected = choose(items, close > open) +plot(selected.get(0)) +""", + "choose", "std::vector", "std::vector& values, bool enabled", 1, + id="array-block-return", + ), + pytest.param( + """type Reading + float value +choose(bool enabled) => + Reading item = Reading.new(close) + if enabled + item + else + item +chosen = choose(close > open) +plot(chosen.value) +""", + "choose", "Reading", "bool enabled", 1, + id="object-block-return", + ), + pytest.param( + """choose(bool enabled, array left, array right) => + enabled ? left : right +left = array.from("first") +right = array.from("second") +chosen = choose(close > open, left, right) +log.info(chosen.get(0)) +""", + "choose", "std::vector", + "bool enabled, std::vector& left, std::vector& right", 1, + id="array-ternary-return", + ), + pytest.param( + """source = ta.ema(close, 7) +features(int offset) => + array items = array.from(source[offset]) + items +current = features(0) +previous = features(2) +plot(current.get(0) - previous.get(0)) +""", + "features", "std::vector", "int offset", 2, + id="array-history-clone-returns", + ), + pytest.param( + """suffix(value) => + pieces = str.split(value, "/") + pieces.size() > 1 ? pieces.get(1) : value +items = array.from("left/right", "whole") +for index = 0 to items.size() - 1 + value = items.get(index) + log.info(suffix(value)) +""", + "suffix", "std::string", "std::string value", 1, + id="variable-string-return", + ), + pytest.param( + """first(values) => values.get(0) +outer(values) => first(values) / values.size() +items = array.from(close) +plot(outer(items)) +""", + "first", "double", "std::vector& values", 1, + id="array-forwarding", + ), + pytest.param( + """pair() => + a = array.from(high) + b = array.from(low) + [a, b] +last(values) => + float result = na + for item in values + result := item + result +[upper, lower] = pair() +plot(last(upper) + last(lower)) +""", + "last", "double", "std::vector& values", 1, + id="tuple-array-argument", + ), + pytest.param( + """parts(value) => + [str.substring(value, 0, 1), str.substring(value, 1, 2)] +outer(value) => + [first, second] = parts(value) + first + second +value = input.string("ab") +log.info(outer(value)) +""", + "parts", "std::tuple", "std::string value", 1, + id="string-forwarding", + ), +] + + +@pytest.mark.parametrize("body,name,result,parameters,count", CASES) +def test_callable_boundaries_keep_the_value_type(body, name, result, parameters, count): + cpp = transpile('//@version=6\nstrategy("value type flow")\n' + body) + assert signatures(cpp, name) == [(result, parameters)] * count + compile_cpp(cpp, label=name) + + +def test_untyped_array_calls_keep_distinct_element_types(): + cpp = transpile("""//@version=6 +strategy("distinct array types") +trim(values, int maximum) => + if values.size() > maximum + values.shift() +integers = array.from(bar_index) +decimals = array.from(close) +trim(integers, 4) +trim(decimals, 4) +""") + found = signatures(cpp, "trim") + assert sorted(parameters for _, parameters in found) == [ + "std::vector& values, int maximum", + "std::vector& values, int maximum", + ] + targets = re.findall( + r"^ (trim(?:_cs\d+|__ni\d+)?)\((integers|decimals), 4\);$", + cpp, re.MULTILINE, + ) + assert len(targets) == 2 and len({name for name, _ in targets}) == 2 + compile_cpp(cpp, label="distinct array argument types") + + +def test_reassigned_matrix_local_keeps_its_initializer_type(): + cpp = transpile("""//@version=6 +strategy("matrix local type") +nextValue(matrix source) => + result = matrix.new(1, 1, 0.0) + result.set(0, 0, source.get(0, 0) / 2) + result +var state = matrix.new(1, 1, 0.0) +state.set(0, 0, close) +if barstate.islast + working = matrix.copy(state) + for index = 1 to 2 + working := nextValue(working) + log.info(str.tostring(working.get(0, 0))) +""") + declarations = re.findall( + r"^\s+(NumericMatrix|double) working(?: = [^\n;]*)?;$", + cpp, re.MULTILINE, + ) + assert declarations == ["NumericMatrix"] + compile_cpp(cpp, label="reassigned matrix local") + + +def test_nested_array_variants_keep_their_argument_and_return_types(): + cpp = transpile("""//@version=6 +strategy("nested array profiles") +identity(values) => values +outer(values) => identity(values) +integerInput = array.new() +decimalInput = array.new() +integerInput.push(7) +decimalInput.push(close) +integerOutput = outer(integerInput) +decimalOutput = outer(decimalInput) +plot(integerOutput.get(0) + decimalOutput.get(0)) +""") + for function in ("outer", "identity"): + found = signatures(cpp, function) + assert ("std::vector", "std::vector& values") in found + assert ("std::vector", "std::vector& values") in found + compile_cpp(cpp, label="nested structured type profiles") From 9237e1baeef8656aad592325ce36929b9ac81ae0 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 07:28:05 +0800 Subject: [PATCH 07/27] Check compound values through native execution Use the established integer storage and matrix alias in type-flow assertions. Check array specializations, mutations, matrix values and copy independence. --- tests/test_callable_compound_type_flow.py | 114 ++++++++++++++++++++-- 1 file changed, 108 insertions(+), 6 deletions(-) diff --git a/tests/test_callable_compound_type_flow.py b/tests/test_callable_compound_type_flow.py index d759c3cc..791e82a6 100644 --- a/tests/test_callable_compound_type_flow.py +++ b/tests/test_callable_compound_type_flow.py @@ -5,7 +5,7 @@ import pytest from pineforge_codegen import transpile -from tests._compile import compile_cpp +from tests._compile import compile_cpp, run_emitted_tu def signatures(cpp, name): @@ -140,16 +140,77 @@ def test_untyped_array_calls_keep_distinct_element_types(): trim(decimals, 4) """) found = signatures(cpp, "trim") + # Small, non-epoch int arrays retain int storage; widening is a separate + # provenance rule pinned by test_wide_int_arrays_and_literals.py. assert sorted(parameters for _, parameters in found) == [ "std::vector& values, int maximum", - "std::vector& values, int maximum", + "std::vector& values, int maximum", ] targets = re.findall( r"^ (trim(?:_cs\d+|__ni\d+)?)\((integers|decimals), 4\);$", cpp, re.MULTILINE, ) assert len(targets) == 2 and len({name for name, _ in targets}) == 2 - compile_cpp(cpp, label="distinct array argument types") + assert {argument for _, argument in targets} == {"integers", "decimals"} + bound_targets = {argument: name for name, argument in targets} + driver = r''' +#include +#include +#include + +template +void dump_array_values(const std::vector& values) { + std::cout << values.size(); + for (auto value : values) std::cout << ' ' << value; + std::cout << '\n'; +} + +int main() { + const Bar bars[] = { + Bar{9.0, 11.0, 8.0, 10.25, 10.0, 1743465600000LL}, + Bar{19.0, 21.0, 18.0, 20.5, 11.0, 1743466500000LL}, + Bar{33.0, 35.0, 32.0, 33.75, 12.0, 1743467400000LL}, + }; + GeneratedStrategy strategy; + strategy.run(bars, 3); + if (!strategy.last_error().empty()) { + std::cerr << strategy.last_error() << '\n'; + return 2; + } + if (strategy.integers.size() != 1 || strategy.decimals.size() != 1) return 3; + std::cout << std::setprecision(17) + << strategy.integers.size() << ' ' << strategy.integers.front() + << ' ' << strategy.decimals.size() << ' ' << strategy.decimals.front() + << '\n'; + + // The unchanged Pine source creates singleton arrays. Exercise both + // emitted bodies with larger native arguments as well, including the + // untaken branch, one removal, and equality at the size boundary. + std::vector integer_values{-3, 0, 7, 21, 41}; + std::vector decimal_values{-3.25, 0.5, 7.75, 21.125, 41.875}; + for (int maximum : {5, 4, 4}) { + strategy.INTEGER_TARGET(integer_values, maximum); + strategy.DECIMAL_TARGET(decimal_values, maximum); + dump_array_values(integer_values); + dump_array_values(decimal_values); + } +} +'''.replace("INTEGER_TARGET", bound_targets["integers"]).replace( + "DECIMAL_TARGET", bound_targets["decimals"], + ) + output = run_emitted_tu( + cpp, driver, opt="-O2", label="distinct array argument types", + ) + rows = [[float(value) for value in line.split()] for line in output.splitlines()] + assert rows == [ + [1, 2, 1, 33.75], + [5, -3, 0, 7, 21, 41], + [5, -3.25, 0.5, 7.75, 21.125, 41.875], + [4, 0, 7, 21, 41], + [4, 0.5, 7.75, 21.125, 41.875], + [4, 0, 7, 21, 41], + [4, 0.5, 7.75, 21.125, 41.875], + ] def test_reassigned_matrix_local_keeps_its_initializer_type(): @@ -168,11 +229,52 @@ def test_reassigned_matrix_local_keeps_its_initializer_type(): log.info(str.tostring(working.get(0, 0))) """) declarations = re.findall( - r"^\s+(NumericMatrix|double) working(?: = [^\n;]*)?;$", + r"^\s+(PineMatrix|double) working(?: = [^\n;]*)?;$", cpp, re.MULTILINE, ) - assert declarations == ["NumericMatrix"] - compile_cpp(cpp, label="reassigned matrix local") + assert declarations == ["PineMatrix"] + driver = r''' +#include +#include +#include +#include + +// The emitter's PineMatrix spelling denotes the actual numeric matrix handle. +static_assert(std::is_same_v< + decltype(std::declval().working), pineforge::NumericMatrix>); + +int main() { + const Bar bars[] = { + Bar{9.0, 11.0, 8.0, 10.25, 10.0, 1743465600000LL}, + Bar{19.0, 21.0, 18.0, 20.5, 11.0, 1743466500000LL}, + Bar{33.0, 35.0, 32.0, 33.75, 12.0, 1743467400000LL}, + }; + GeneratedStrategy strategy; + strategy.run(bars, 3); + if (!strategy.last_error().empty()) { + std::cerr << strategy.last_error() << '\n'; + return 2; + } + std::cout << std::setprecision(17) + << strategy.state.rows() << ' ' << strategy.state.columns() + << ' ' << strategy.state.get(0, 0) + << ' ' << strategy.working.rows() << ' ' << strategy.working.columns() + << ' ' << strategy.working.get(0, 0) << '\n'; + strategy.working.set(0, 0, -17.5); + std::cout << strategy.state.get(0, 0) << ' ' << strategy.working.get(0, 0) << '\n'; + strategy.state.set(0, 0, 99.25); + std::cout << strategy.state.get(0, 0) << ' ' << strategy.working.get(0, 0) << '\n'; +} +''' + output = run_emitted_tu( + cpp, driver, opt="-O2", label="reassigned matrix local", + ) + rows = [[float(value) for value in line.split()] for line in output.splitlines()] + assert rows == [ + [1, 1, 33.75, 1, 1, 33.75 / 4], + [33.75, -17.5], + [99.25, -17.5], + ] def test_nested_array_variants_keep_their_argument_and_return_types(): From e004cbf5bea11d48d20df828b62b05ca6f9435dd Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 07:28:58 +0800 Subject: [PATCH 08/27] Bound type inference within expression queries Share operand and value inference while an expression query is active, then release cached results before bindings can change. Preserve ordinary matrix member initialization when the declaration expression is still available. --- CHANGELOG.md | 2 + pineforge_codegen/analyzer/base.py | 22 +++++++---- pineforge_codegen/analyzer/types.py | 58 ++++++++++++++++++++++++----- pineforge_codegen/codegen/base.py | 8 +++- 4 files changed, 72 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57a19f76..45161886 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,8 @@ supported as exact pairs; on the 0.x line they are independent. See the returns and history clones. Untyped arguments retain collection element types through nested calls and tuple bindings, and reassigned matrix locals keep their initializer's matrix type. (#391) +- Deeply nested user-function calls share type-inference work within each + expression query, including when a return type remains unresolved. - `array.sort_indices` returns integer indices, accepts typed `array` results and honors ascending/default or descending order in namespace and method calls, including named arguments. Numeric `na` values sort last in diff --git a/pineforge_codegen/analyzer/base.py b/pineforge_codegen/analyzer/base.py index 80f2af34..37700cb6 100644 --- a/pineforge_codegen/analyzer/base.py +++ b/pineforge_codegen/analyzer/base.py @@ -7084,6 +7084,14 @@ def _operator_operand_type( parameter_types: dict[str, PineType | TypeSpec] | None = None, ) -> TypeSpec | None: """Use lexical type facts and written-call facts, never a float fallback.""" + return OperatorTypeQuery(self, parameter_types)._operand_type(node) + + def _operator_operand_type_uncached( + self, node: ASTNode, + parameter_types: dict[str, PineType | TypeSpec] | None, + query: OperatorTypeQuery, + ) -> TypeSpec | None: + """Resolve an operand using the current expression query's memo.""" if isinstance(node, Identifier) and parameter_types is not None: if node.name in parameter_types: ptype = parameter_types[node.name] @@ -7138,7 +7146,7 @@ def _operator_operand_type( arguments.append(node.kwargs.get(name)) result = source_builtin_result( ns, callee.member, tuple( - self._operator_operand_type(arg, parameter_types) + query._operand_type(arg) if arg is not None else None for arg in arguments ), ) @@ -7146,7 +7154,7 @@ def _operator_operand_type( return result # The structured return contract can prove a collection # even when no scalar result rule applies. - result = OperatorTypeQuery(self, parameter_types)._type_spec_from_expr(node) + result = query._type_spec_from_expr(node) return result if result is not None and result.kind != "primitive" else None if isinstance(callee, Identifier) and callee.name in {"nz", "fixnan", "input"}: return None @@ -7154,7 +7162,7 @@ def _operator_operand_type( if isinstance(node.object, Subscript): # Nested history is decided by the subscript checker. return None - receiver = self._operator_operand_type(node.object, parameter_types) + receiver = query._operand_type(node.object) if receiver is None: return None if receiver.kind in {"array", "matrix", "map"}: @@ -7176,7 +7184,7 @@ def _operator_operand_type( terminal = self._selection_terminal_expr(body) if terminal is None: continue - arm = self._operator_operand_type(terminal, parameter_types) + arm = query._operand_type(terminal) if arm is None: return None if result is None: @@ -7198,7 +7206,7 @@ def _operator_operand_type( else: op, operands = "?:", (node.condition, node.true_val, node.false_val) result, _ = resolve_operator(op, tuple( - self._operator_operand_type(expr, parameter_types) + query._operand_type(expr) for expr in operands )) return result @@ -7236,7 +7244,7 @@ def _operator_operand_type( or getattr(sym, "_pf_parameter_owner", None)): result = sym.type_spec if result is None and isinstance(node, (MemberAccess, FuncCall, Subscript)): - result = OperatorTypeQuery(self, parameter_types)._type_spec_from_expr(node) + result = query._type_spec_from_expr(node) if isinstance(node, FuncCall) and result is not None and result.kind == "primitive": # A definition-wide callable result is legacy inference. # Collection accessors instead have a declared element slot. @@ -7249,7 +7257,7 @@ def _operator_operand_type( else callee.object ) receiver_type = ( - self._operator_operand_type(receiver, parameter_types) + query._operand_type(receiver) if receiver is not None else None ) accessors = { diff --git a/pineforge_codegen/analyzer/types.py b/pineforge_codegen/analyzer/types.py index 7961ce2d..3eda26e9 100644 --- a/pineforge_codegen/analyzer/types.py +++ b/pineforge_codegen/analyzer/types.py @@ -1464,10 +1464,41 @@ class OperatorTypeQuery(TypeHelper): def __init__(self, analyzer, parameter_types): self._analyzer = analyzer self._parameter_types = parameter_types + self._type_query_memo = None def __getattr__(self, name): return getattr(self._analyzer, name) + def _memoized_type(self, node, kind, context, infer): + # Share successful and unresolved results across the value/operand + # fallback paths of one read-only query. Drop the entire memo when + # that query returns, before any declaration, scope or callable facts + # can change. A later query, even on this object, starts fresh. + memo = self._type_query_memo + root = memo is None + if root: + memo = self._type_query_memo = {} + try: + key = (kind, id(node), context) + if key not in memo: + if self._analyzer._budget is not None: + self._analyzer._budget.check( + getattr(node, "loc", None), Phase.ANALYZER, + ) + memo[key] = infer() + return memo[key] + finally: + if root: + self._type_query_memo = None + + def _operand_type(self, node): + return self._memoized_type( + node, "operand", None, + lambda: self._analyzer._operator_operand_type_uncached( + node, self._parameter_types, self, + ), + ) + def check_body(self, body): for node in body: self._check_node(node) @@ -1538,11 +1569,14 @@ def body_value_spec(self, body): return result def _value_spec(self, value): + return self._memoized_type( + value, "value", None, lambda: self._value_spec_uncached(value), + ) + + def _value_spec_uncached(self, value): spec = self._type_spec_from_expr(value) if spec is None: - spec = self._analyzer._operator_operand_type( - value, self._parameter_types - ) + spec = self._operand_type(value) return spec def _check_node(self, node): @@ -1636,11 +1670,17 @@ def _check_node(self, node): return self._analyzer._check_operator_operands(node, op, operands, self._parameter_types) - def _type_spec_from_expr(self, value, **kwargs): + def _type_spec_from_expr(self, value, *, _ternary_arms=None): + return self._memoized_type( + value, "spec", _ternary_arms, + lambda: self._type_spec_from_expr_uncached( + value, _ternary_arms=_ternary_arms, + ), + ) + + def _type_spec_from_expr_uncached(self, value, **kwargs): if isinstance(value, Identifier): - return self._analyzer._operator_operand_type( - value, self._parameter_types, - ) + return self._operand_type(value) if (isinstance(value, FuncCall) and isinstance(value.callee, Identifier) and value.callee.name in self._analyzer._func_defs): @@ -1663,7 +1703,7 @@ def _type_spec_from_expr(self, value, **kwargs): def _visit(self, node): # TypeHelper's array.from fallback normally visits its arguments. # Here it asks for facts only, so no TA or request is registered again. - spec = self._analyzer._operator_operand_type(node, self._parameter_types) + spec = self._operand_type(node) if spec is not None and spec.kind == "primitive": return next((ptype for ptype in PineType if ptype.value == spec.name), PineType.UNKNOWN) @@ -1672,7 +1712,7 @@ def _visit(self, node): def _array_from_element_spec(self, value): # Array construction must not recover a coarse symbol/call result # after the operator query deliberately left its source unresolved. - return self._analyzer._operator_operand_type(value, self._parameter_types) + return self._operand_type(value) @staticmethod def _pine_type_to_spec(pine_type): diff --git a/pineforge_codegen/codegen/base.py b/pineforge_codegen/codegen/base.py index fdb388ba..0f306988 100644 --- a/pineforge_codegen/codegen/base.py +++ b/pineforge_codegen/codegen/base.py @@ -2551,6 +2551,7 @@ def _register_global_aggregate_member_types(self) -> None: # final state and can already contain an invalid later reassignment's # element type. The original declaration must remain the compatibility # baseline used to reject matrix := matrix. + gem = getattr(self.ctx, "global_expr_map", {}) or {} for stmt, _in_loop in self._walk_global_scope_with_loopflag( getattr(self.ctx.ast, "body", []), False ): @@ -2572,11 +2573,15 @@ def _register_global_aggregate_member_types(self) -> None: self._array_vars.add(stmt.name) self._collection_types.setdefault(stmt.name, declared_spec) continue - else: + elif stmt.name not in gem: # Reassignment removes a binding from global_expr_map. Its # initializer still establishes its matrix handle type, also # for declarations inside top-level control-flow blocks. declared_spec = self._type_spec_from_expr(stmt.value) + else: + # An available initializer still uses the established member + # inference and default initialization below. + continue if (from_selection and declared_spec is not None and declared_spec.kind == "array"): # ``a = switch mode ...`` whose every arm is an array of one @@ -2591,7 +2596,6 @@ def _register_global_aggregate_member_types(self) -> None: self._matrix_specs.setdefault(stmt.name, declared_spec) self._collection_types[stmt.name] = self._matrix_specs[stmt.name] - gem = getattr(self.ctx, "global_expr_map", {}) or {} for name, _ptype in self.ctx.global_var_decls: expr = gem.get(name) if expr is None or not isinstance(expr, FuncCall): From 8802058aee5a62c6f304f8f6beab0f0b37fe675e Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 07:50:59 +0800 Subject: [PATCH 09/27] Validate timeframe argument types Use the chart period for a generic missing timeframe argument and refuse numeric values for string parameters during analysis. Cover named arguments, helper calls, source locations and generated translation units. --- CHANGELOG.md | 4 +- pineforge_codegen/analyzer/base.py | 25 +++++++ pineforge_codegen/codegen/visit_call.py | 4 +- scripts/gen_diagnostics_catalog.py | 13 +++- tests/test_empty_timeframe_emission.py | 87 ++++++++++++++++++++++++- 5 files changed, 127 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb497781..32064caa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,9 @@ supported as exact pairs; on the 0.x line they are independent. See the - `timeframe.in_seconds` and `timeframe.change` resolve an evaluated empty string to the chart period, including input, variable, function-return and named arguments. The argument is evaluated once, and nonempty strings keep - their explicit period. Dotted builtin signatures retain supplied arguments, + their explicit period. A generic `na` argument also selects the chart period; + numeric timeframe arguments are refused during analysis with a source + location. Dotted builtin signatures retain supplied arguments, including escaped string literals and VWAP anchors. (#394) - Script identifiers spelled like integer-to-float conversion temporaries, including the underscore suffixes of nested conversions, now use the diff --git a/pineforge_codegen/analyzer/base.py b/pineforge_codegen/analyzer/base.py index 772e2426..6f903a6a 100644 --- a/pineforge_codegen/analyzer/base.py +++ b/pineforge_codegen/analyzer/base.py @@ -7589,6 +7589,31 @@ def _visit_FuncCall(self, node: FuncCall) -> PineType: visited_member_arg_types[id(arg)] = self._visit(arg) for val in node.kwargs.values(): visited_member_arg_types[id(val)] = self._visit(val) + if (self._pine_version == 6 + and not self._in_v5_library_definition(node) + and isinstance(obj, Identifier) and obj.name == "timeframe" + and self._value_bindings.declaration_bindings.get(id(obj)) is None): + intrinsic = sigs.BUILTIN_FUNCTIONS.get(f"{obj.name}.{member}") + if intrinsic is not None: + bound = self._bind_callable_args(node, intrinsic.param_names) + for parameter, value in zip(intrinsic.primary.params, bound): + if value is None or parameter.pine_type != PineType.STRING: + continue + # An untyped helper's provisional float is not proof + # that the caller supplies a numeric value. + actual_type = self._certain_scalar_type(value) + if actual_type in (PineType.INT, PineType.FLOAT): + # Generic na adopts the parameter's string type; + # a numeric value cannot satisfy that signature. + function_name = intrinsic.name + argument_type = actual_type.value + parameter_type = parameter.pine_type.value + name = parameter.name + self._error( + f"Cannot call '{function_name}' with a {argument_type} " + f"argument for {parameter_type} parameter '{name}'.", + value.loc, + ) # UDT method call-site typing. Method definitions may contain an # untyped parameter history read; resolve receiver + args here and # apply the same deferred map-history gate as regular UDFs before diff --git a/pineforge_codegen/codegen/visit_call.py b/pineforge_codegen/codegen/visit_call.py index eb97f7bd..4b3faf94 100644 --- a/pineforge_codegen/codegen/visit_call.py +++ b/pineforge_codegen/codegen/visit_call.py @@ -3232,7 +3232,9 @@ def timeframe_call(tf_arg: str) -> str: if argument is None: return timeframe_call("script_tf_") - argument_cpp = self._visit_expr(argument) + argument_cpp = self._visit_rhs_value( + argument, target_cpp_type="std::string", + ) if cpp_is_plain_read(argument_cpp): # Keep resolved locals and parameters as direct engine # arguments when nonempty. Stable reads can be checked diff --git a/scripts/gen_diagnostics_catalog.py b/scripts/gen_diagnostics_catalog.py index 9a7e3578..d0858ed6 100644 --- a/scripts/gen_diagnostics_catalog.py +++ b/scripts/gen_diagnostics_catalog.py @@ -1071,8 +1071,8 @@ def dump(catalog: dict) -> str: } -# Declaration presentation follows the extracted template through code allocation. -STRATEGY_DECLARATION_PRESENTATION = { +# Authored presentation follows the extracted template through code allocation. +DIAGNOSTIC_PRESENTATION = { "strategy(): too many positional arguments (expected {expected_count}, got {actual_count})": ( "A strategy declaration has more positional values than its signature accepts. " "Remove the extra values or use the declared parameter names.", @@ -1088,6 +1088,13 @@ def dump(catalog: dict) -> str: "Supply a literal or an expression over earlier constants.", "Strategy setting ''{name}'' must be a compile-time constant.", ), + "Cannot call ''{function_name}'' with a {argument_type} argument for " + "{parameter_type} parameter ''{name}''.": ( + "A numeric value cannot be used for a string parameter. Pass a string value " + "of the declared parameter type; a permitted generic na adopts that type.", + "Argument ''{name}'' of ''{function_name}'' has type {argument_type}; " + "expected {parameter_type}.", + ), } # Public reason vocabulary for the located series-timeframe diagnostic. @@ -1137,7 +1144,7 @@ def main(argv=None) -> int: entry["user_message"] = sentence entry["explanation"] = entry["explanation"] or _ALLOWLIST_EXPLANATION for entry in catalog["codes"].values(): - presentation = STRATEGY_DECLARATION_PRESENTATION.get(entry["message"]) + presentation = DIAGNOSTIC_PRESENTATION.get(entry["message"]) if presentation is not None: explanation, sentence = presentation entry["explanation"] = entry["explanation"] or explanation diff --git a/tests/test_empty_timeframe_emission.py b/tests/test_empty_timeframe_emission.py index 5ca2a65b..eb4bafc0 100644 --- a/tests/test_empty_timeframe_emission.py +++ b/tests/test_empty_timeframe_emission.py @@ -1,8 +1,13 @@ -"""Empty argument selection is covered without native engine inputs.""" +"""Timeframe argument typing and selection, with opt-in full-TU checks.""" import pytest from pineforge_codegen import transpile +from pineforge_codegen.analyzer import Analyzer +from pineforge_codegen.diagnostic_codes import UNCATALOGUED +from pineforge_codegen.errors import CompileError, Level, Phase +from pineforge_codegen.lexer import Lexer +from pineforge_codegen.parser import Parser def engine_call(function, argument): @@ -46,3 +51,83 @@ def test_call_argument_is_evaluated_once_before_chart_selection(function): selected = "(_pf_timeframe_arg.empty() ? script_tf_ : _pf_timeframe_arg)" assert engine_call(function, selected) in cpp assert cpp.count("const std::string _pf_timeframe_arg = (chosenFrame());") == 1 + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +def test_na_argument_uses_string_missing_value_before_chart_selection(function, named): + argument = "timeframe = na" if named else "na" + cpp = transpile( + '//@version=6\nstrategy("Missing timeframe")\n' + f"value = timeframe.{function}({argument})\n" + ) + missing = "na()" + expected = ( + "((" + missing + ").empty() ? " + + engine_call(function, "script_tf_") + " : " + + engine_call(function, missing) + ")" + ) + assert expected in cpp + assert "(na()).empty()" not in cpp + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("parameter", ["frameArg", "string frameArg"]) +def test_helper_string_parameter_keeps_chart_selection(function, parameter): + cpp = transpile( + '//@version=6\nstrategy("Helper timeframe")\n' + f"withFrame({parameter}) => timeframe.{function}(frameArg)\n" + 'value = withFrame("")\n' + ) + expected = ( + "((frameArg).empty() ? " + + engine_call(function, "script_tf_") + " : " + + engine_call(function, "frameArg") + ")" + ) + assert expected in cpp + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +@pytest.mark.parametrize("argument,pine_type", [ + ("0", "int"), + ("17", "int"), + ("-3", "int"), + ("0.0", "float"), + ("1.25", "float"), + ("-2.5", "float"), +]) +def test_numeric_timeframe_is_refused_during_analysis(function, named, argument, pine_type): + supplied = f"timeframe = {argument}" if named else argument + source = ( + '//@version=6\nstrategy("Numeric timeframe")\n' + f"value = timeframe.{function}({supplied})\n" + ) + ast = Parser(Lexer(source).tokenize(), source=source).parse() + with pytest.raises(CompileError) as refused: + Analyzer(ast).analyze() + diagnostic, = refused.value.diagnostics + assert diagnostic.level == Level.ERROR + assert diagnostic.phase == Phase.ANALYZER + assert diagnostic.code.startswith("PF-E") + assert diagnostic.code not in UNCATALOGUED.values() + assert f"timeframe.{function}" in diagnostic.message + assert pine_type in diagnostic.message + assert "string" in diagnostic.message + assert diagnostic.location.line == 3 + assert diagnostic.location.col == source.splitlines()[2].index(argument) + 1 + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +@pytest.mark.parametrize("optimization", ["-O0", "-O2"]) +def test_na_timeframe_full_translation_unit_compiles(function, named, optimization): + from tests._compile import compile_cpp + + argument = "timeframe = na" if named else "na" + cpp = transpile( + '//@version=6\nstrategy("Missing timeframe compilation")\n' + f"value = timeframe.{function}({argument})\n" + ) + compile_cpp(cpp, label=f"timeframe.{function}({argument}) {optimization}", + extra_flags=[optimization]) From 7a28a2968ea9dd520f414712b7347c6bb656e1f7 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 08:52:54 +0800 Subject: [PATCH 10/27] Register the timeframe argument diagnostic Include only the stock-generated PF-E2095 definition and pin. Preserve all prior catalog entry bytes and their existing order. --- pineforge_codegen/diagnostics_catalog.json | 1 + tests/fixtures/diagnostic_codes_pin.json | 1 + 2 files changed, 2 insertions(+) diff --git a/pineforge_codegen/diagnostics_catalog.json b/pineforge_codegen/diagnostics_catalog.json index eb4bc5ff..91f7a3c6 100644 --- a/pineforge_codegen/diagnostics_catalog.json +++ b/pineforge_codegen/diagnostics_catalog.json @@ -235,6 +235,7 @@ "PF-E2090": {"severity": "error", "area": "analysis", "message": "input.time() needs a constant default in Pine v6: timestamp() with a time zone and calendar numbers is not a constant. Use the date-string form, for example timestamp(\"01 Jan 2023 00:00 +0000\").", "hint": "Use a single date string in timestamp or an integer timestamp literal for the default.", "explanation": "The time-zone overload of timestamp, which takes a zone and calendar numbers, returns a simple int, while a Pine v6 input.time default requires a const int.", "args": {}, "user_message": "input.time() requires a constant default in Pine v6."}, "PF-E2091": {"severity": "error", "area": "analysis", "message": "plot() cannot have a linewidth below 1 in Pine v6: use a value of 1 or greater.", "hint": "Set the plot linewidth to an integer of at least 1.", "explanation": "A Pine v6 plot call cannot use an integer literal below one for linewidth; variable and calculated widths retain their existing handling.", "args": {}, "user_message": "plot() requires a linewidth of at least 1 in Pine v6."}, "PF-E2092": {"severity": "error", "area": "analysis", "message": "\"range\" cannot be used as a variable or function name in Pine v6: use another name.", "hint": "Rename the variable, function or method declaration.", "explanation": "Pine v6 reserves range as a variable and callable declaration name; parameters, user type fields and member access retain their existing handling.", "args": {}, "user_message": "Choose another variable, function or method name instead of range in Pine v6."}, + "PF-E2095": {"severity": "error", "area": "analysis", "message": "Cannot call ''{function_name}'' with a {argument_type} argument for {parameter_type} parameter ''{name}''.", "hint": null, "explanation": "A numeric value cannot be used for a string parameter. Pass a string value of the declared parameter type; a permitted generic na adopts that type.", "args": {"function_name": {"kind": "identifier"}, "argument_type": {"kind": "type"}, "parameter_type": {"kind": "type"}, "name": {"kind": "identifier"}}, "user_message": "Argument ''{name}'' of ''{function_name}'' has type {argument_type}; expected {parameter_type}."}, "PF-E3001": {"severity": "error", "area": "request-security", "message": "request.security timeframe ''{name}'' cannot be resolved before the first bar: PineForge registers every requested timeframe before the script runs.", "hint": "Pass a literal, an input or a global to the helper that holds request.security.", "explanation": "The timeframe given to request.security is a name whose value PineForge cannot compute before the first bar; use a literal, an input or a global.", "args": {"name": {"kind": "identifier"}}, "user_message": "The timeframe given to request.security is a name whose value PineForge cannot compute before the first bar."}, "PF-E3002": {"severity": "error", "area": "request-security", "message": "request.security timeframe expression cannot be resolved before the first bar: PineForge registers every requested timeframe before the script runs.", "hint": "Pass a literal, an input or a global to the helper that holds request.security.", "explanation": "The timeframe expression given to request.security cannot be computed before the first bar; use a literal, an input or a global.", "args": {}, "user_message": "The timeframe expression given to request.security cannot be computed before the first bar."}, "PF-E3003": {"severity": "error", "area": "request-security", "message": "request.security timeframe switch arms must contain one expression", "hint": "Compute multi-statement timeframe logic before the request.security call or rewrite it as pure switch arms.", "explanation": "A switch that picks a request.security timeframe must have exactly one expression in each arm; compute multi-statement logic beforehand.", "args": {}, "user_message": "A switch that picks a request.security timeframe must have exactly one expression in each arm."}, diff --git a/tests/fixtures/diagnostic_codes_pin.json b/tests/fixtures/diagnostic_codes_pin.json index 0d4aaa73..e8719a8e 100644 --- a/tests/fixtures/diagnostic_codes_pin.json +++ b/tests/fixtures/diagnostic_codes_pin.json @@ -236,6 +236,7 @@ "PF-E2090": "dcc4d05f50b881e5345de8c73ec7b7951bedc86c30a24d40bc0077fca5242a0d", "PF-E2091": "2d1b04fd25a4198619628b94bf8f90b4002f33e4081c2b780fea510c49e0b44a", "PF-E2092": "74b483fe343d239ec254f03e839b257611db5295b0433eabd15072f919950f3d", + "PF-E2095": "2f11b08a085935c9d4583e461d14594a607ca43b593b6a706bc042c425f3aa7c", "PF-E3001": "585dad07e797bbe2cfffa9f45e14dee040be5570cc13f8e46fa4c31e953fc92a", "PF-E3002": "db089893825afe5ad1d91934c21107fc58451cadf3d71de2539b9c5805549433", "PF-E3003": "02571beee5163d1596d8a11fe49336dfcf65f86d4fed13254bac6b36023c3c79", From c04553dfffdf0a617fcdab5abba0f02934aa2168 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:04:24 +0800 Subject: [PATCH 11/27] Disambiguate diagnostic templates by argument kinds Use recognized Pine types in declared type slots to resolve equally literal diagnostic templates. Keep stable code order for other ties and preserve qualifier identities, raw arguments and rendering. Pin the timeframe type diagnostic and its named arguments with direct regression controls. --- CHANGELOG.md | 6 ++-- pineforge_codegen/diagnostic_codes.py | 34 +++++++++++++++++++++-- tests/test_diagnostic_codes.py | 38 ++++++++++++++++++++++++++ tests/test_empty_timeframe_emission.py | 22 +++++++++++---- 4 files changed, 89 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db7be37d..98784ae1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,8 +26,10 @@ supported as exact pairs; on the 0.x line they are independent. See the string to the chart period, including input, variable, function-return and named arguments. The argument is evaluated once, and nonempty strings keep their explicit period. A generic `na` argument also selects the chart period; - numeric timeframe arguments are refused during analysis with a source - location. Dotted builtin signatures retain supplied arguments, + numeric timeframe arguments are refused during analysis with the stable + `PF-E2095` type diagnostic and a source location. Equally specific diagnostic + templates distinguish primitive types from qualifiers using their catalog + argument kinds. Dotted builtin signatures retain supplied arguments, including escaped string literals and VWAP anchors. (#394) - Script identifiers spelled like integer-to-float conversion temporaries, including the underscore suffixes of nested conversions, now use the diff --git a/pineforge_codegen/diagnostic_codes.py b/pineforge_codegen/diagnostic_codes.py index 4b8ca4c7..bcd6c7a8 100644 --- a/pineforge_codegen/diagnostic_codes.py +++ b/pineforge_codegen/diagnostic_codes.py @@ -14,6 +14,11 @@ never guessed: a text no template renders gets the uncatalogued code of its severity (``PF-E0000`` / ``PF-W0000``), which the test suite refuses. +For templates with the same amount of literal text, recognized Pine primitive +types in catalog-declared type arguments take precedence over open string +arguments. Remaining ties keep the stable code order; every candidate still +renders the same text. + The match never backtracks over the text's characters: each literal segment of a template goes to its leftmost place after the previous one, the last to the text's end, and every argument is the text between its literals -- the @@ -38,6 +43,8 @@ from pathlib import Path from typing import Any +from .symbols import PineType + CATALOG_PATH = Path(__file__).with_name("diagnostics_catalog.json") CATALOG_SCHEMA = "pineforge-diagnostics-catalog/v1" # Notes keep historical PF-W identities. The nonfatal fallback is shared; @@ -178,6 +185,7 @@ def render_diagnostic(code: str, args: dict) -> tuple[str, str | None]: _CANONICAL_INT = re.compile(r"-?(?:0|[1-9][0-9]*)") _CANONICAL_FLOAT = re.compile(r"-?(?:0|[1-9][0-9]*)\.[0-9]+") +_PINE_TYPE_NAMES = frozenset(pine_type.value for pine_type in PineType) def _segments(parts: list) -> tuple[str, tuple[tuple[str, str], ...]]: @@ -313,6 +321,15 @@ def match(self, message: str, hint: str | None) -> dict | None: args[name] = value return args + def type_specificity(self, args: dict) -> int: + """Count recognized primitive types in this template's type arguments. + + Other spellings stay raw: this only breaks a literal-specificity tie, + without rejecting identifiers, composite types or open diagnostic text. + """ + return sum(value in _PINE_TYPE_NAMES for name, value in args.items() + if self._kinds.get(name) == "type") + @lru_cache(maxsize=1) def _matchers() -> dict[str, list[_Matcher]]: @@ -322,8 +339,8 @@ def _matchers() -> dict[str, list[_Matcher]]: continue by_severity[entry["severity"]].append(_Matcher(code, entry)) for matchers in by_severity.values(): - # The template with the most literal text wins: a message two templates - # render reads as the more specific one (both render it byte for byte). + # Prefer literal text first. Argument types can resolve equal literal + # specificity in classify; remaining ties keep this stable code order. matchers.sort(key=lambda m: (-m.specificity, m.code)) return by_severity @@ -336,14 +353,25 @@ def classify(severity: str, message: str, hint: str | None = None) -> tuple[str, no template renders gets ``PF-E0000`` for an error or ``PF-W0000`` for a warning/note, with its text as ``args`` and no new fallback identity. """ + best: tuple[str, dict] | None = None + best_specificity = -1 + best_types = -1 for matcher in _matchers().get(severity, ()): + if best is not None and matcher.specificity < best_specificity: + break if matcher.has_hint != (hint is not None): continue if not message.startswith(matcher.prefix) or not message.endswith(matcher.suffix): continue args = matcher.match(message, hint) if args is not None: - return matcher.code, args + types = matcher.type_specificity(args) + if best is None or types > best_types: + best = (matcher.code, args) + best_specificity = matcher.specificity + best_types = types + if best is not None: + return best args = {"message": message} if hint is not None: args["hint"] = hint diff --git a/tests/test_diagnostic_codes.py b/tests/test_diagnostic_codes.py index 8090ba70..203a1831 100644 --- a/tests/test_diagnostic_codes.py +++ b/tests/test_diagnostic_codes.py @@ -141,6 +141,44 @@ def test_classify_reads_arguments_raw(): assert render_diagnostic(code, args) == ("Undefined variable: 'rsiLen'", None) +@pytest.mark.parametrize("message, expected_code, expected_args", [ + ( + "Cannot call 'timeframe.change' with a int argument for string parameter 'timeframe'.", + "PF-E2095", + {"function_name": "timeframe.change", "name": "timeframe", + "argument_type": "int", "parameter_type": "string"}, + ), + ( + "Cannot call 'chooseFrame' with a float argument for string parameter 'candidate'.", + "PF-E2095", + {"function_name": "chooseFrame", "name": "candidate", + "argument_type": "float", "parameter_type": "string"}, + ), + ( + "Cannot call 'strategy.risk.allow_entry_in' with a series argument for simple parameter 'value'.", + "PF-E2080", + {"function_name": "strategy.risk.allow_entry_in", "name": "value", + "argument_qualifier": "series", "parameter_qualifier": "simple"}, + ), + ( + "Cannot call 'keep' with a input argument for const parameter 'amount'.", + "PF-E2080", + {"function_name": "keep", "name": "amount", + "argument_qualifier": "input", "parameter_qualifier": "const"}, + ), + ( + "matrix>: nested collection element types not supported in v1.", + "PF-E1004", + {"element_type": "array"}, + ), +]) +def test_catalog_argument_kinds_disambiguate_matching_templates( + message, expected_code, expected_args, +): + assert classify("error", message) == (expected_code, expected_args) + assert render_diagnostic(expected_code, expected_args) == (message, None) + + def test_uncatalogued_text_keeps_a_code(): diagnostic = Diagnostic(Level.ERROR, Phase.ANALYZER, SourceLocation("t.pine", 1, 1, 1), "boom") diff --git a/tests/test_empty_timeframe_emission.py b/tests/test_empty_timeframe_emission.py index eb4bafc0..07b198a7 100644 --- a/tests/test_empty_timeframe_emission.py +++ b/tests/test_empty_timeframe_emission.py @@ -4,7 +4,7 @@ from pineforge_codegen import transpile from pineforge_codegen.analyzer import Analyzer -from pineforge_codegen.diagnostic_codes import UNCATALOGUED +from pineforge_codegen.diagnostic_codes import render_diagnostic from pineforge_codegen.errors import CompileError, Level, Phase from pineforge_codegen.lexer import Lexer from pineforge_codegen.parser import Parser @@ -109,11 +109,21 @@ def test_numeric_timeframe_is_refused_during_analysis(function, named, argument, diagnostic, = refused.value.diagnostics assert diagnostic.level == Level.ERROR assert diagnostic.phase == Phase.ANALYZER - assert diagnostic.code.startswith("PF-E") - assert diagnostic.code not in UNCATALOGUED.values() - assert f"timeframe.{function}" in diagnostic.message - assert pine_type in diagnostic.message - assert "string" in diagnostic.message + assert diagnostic.code == "PF-E2095" + assert diagnostic.args == { + "function_name": f"timeframe.{function}", + "name": "timeframe", + "argument_type": pine_type, + "parameter_type": "string", + } + assert diagnostic.message == ( + f"Cannot call 'timeframe.{function}' with a {pine_type} " + "argument for string parameter 'timeframe'." + ) + assert diagnostic.hint is None + assert render_diagnostic(diagnostic.code, diagnostic.args) == ( + diagnostic.message, diagnostic.hint, + ) assert diagnostic.location.line == 3 assert diagnostic.location.col == source.splitlines()[2].index(argument) + 1 From f2f6ec8123b58bf53a96da4c8bfefe2e751154a8 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 13:57:38 +0800 Subject: [PATCH 12/27] Validate timeframe arguments using semantic types Reject non-string timeframe arguments through the intrinsic signature before C++ lowering, including helper parameters and boolean values. Preserve valid string calls and exercise complete native controls at distinct optimization levels. Declare the new diagnostic in the existing catalog delta fixture. --- CHANGELOG.md | 14 +- pineforge_codegen/analyzer/base.py | 71 ++++++---- pineforge_codegen/analyzer/types.py | 3 + pineforge_codegen/codegen/visit_call.py | 39 ++++++ tests/fixtures/diagnostic_notes/delta.json | 3 +- tests/test_empty_timeframe_emission.py | 33 ++++- tests/test_timeframe_argument_types.py | 155 +++++++++++++++++++++ tests/test_timeframe_string_tu.py | 77 ++++++++++ 8 files changed, 360 insertions(+), 35 deletions(-) create mode 100644 tests/test_timeframe_argument_types.py create mode 100644 tests/test_timeframe_string_tu.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 98784ae1..41136894 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,12 +25,14 @@ supported as exact pairs; on the 0.x line they are independent. See the - `timeframe.in_seconds` and `timeframe.change` resolve an evaluated empty string to the chart period, including input, variable, function-return and named arguments. The argument is evaluated once, and nonempty strings keep - their explicit period. A generic `na` argument also selects the chart period; - numeric timeframe arguments are refused during analysis with the stable - `PF-E2095` type diagnostic and a source location. Equally specific diagnostic - templates distinguish primitive types from qualifiers using their catalog - argument kinds. Dotted builtin signatures retain supplied arguments, - including escaped string literals and VWAP anchors. (#394) + their explicit period. Bare `na` remains accepted; the value returned by + `timeframe.change(na)` is still awaiting verification against TradingView. + Non-string timeframe arguments are refused before C++ emission with the + stable `PF-E2095` type diagnostic and a source location, including input + values, reassignments, indicator results and helper parameters. Equally + specific diagnostic templates distinguish primitive types from qualifiers + using their catalog argument kinds. Dotted builtin signatures retain supplied + arguments, including escaped string literals and VWAP anchors. (#394) - Script identifiers spelled like integer-to-float conversion temporaries, including the underscore suffixes of nested conversions, now use the generator's reserved-name escaping. diff --git a/pineforge_codegen/analyzer/base.py b/pineforge_codegen/analyzer/base.py index 6f903a6a..293ae5bb 100644 --- a/pineforge_codegen/analyzer/base.py +++ b/pineforge_codegen/analyzer/base.py @@ -7459,6 +7459,51 @@ def _visit_Ternary(self, node: Ternary) -> PineType: return main_type return result_type + def _check_timeframe_string_arguments( + self, node: FuncCall, + parameter_types: dict[str, PineType | TypeSpec] | None = None, + ) -> None: + """Check string signature slots using lexical and written-call types.""" + if self._pine_version != 6 or self._in_v5_library_definition(node): + return + callee = node.callee + if (not isinstance(callee, MemberAccess) + or not isinstance(callee.object, Identifier) + or callee.object.name != "timeframe" + or self._value_bindings.declaration_bindings.get(id(callee.object)) is not None + or (parameter_types is not None and callee.object.name in parameter_types)): + return + intrinsic = sigs.BUILTIN_FUNCTIONS.get(f"timeframe.{callee.member}") + if intrinsic is None: + return + bound = self._bind_callable_args(node, intrinsic.param_names) + for parameter, value in zip(intrinsic.primary.params, bound): + if value is None or parameter.pine_type != PineType.STRING: + continue + actual = self._operator_operand_type(value, parameter_types) + if (actual is None and isinstance(value, MemberAccess) + and isinstance(value.object, Identifier) + and self._value_bindings.declaration_bindings.get(id(value.object)) is None + and (parameter_types is None or value.object.name not in parameter_types)): + variable_type = sigs.get_variable_type(value.object.name, value.member) + if variable_type != PineType.UNKNOWN: + actual = TypeSpec.primitive(variable_type.value) + if actual is None or actual in { + TypeSpec.primitive("string"), TypeSpec.primitive("na"), + }: + # Untyped helper parameters stay open until their written + # call supplies a type. Bare na adopts the string signature. + continue + function_name = intrinsic.name + argument_type = method_receiver_type_name(actual) or str(actual) + parameter_type = parameter.pine_type.value + name = parameter.name + self._error( + f"Cannot call '{function_name}' with a {argument_type} " + f"argument for {parameter_type} parameter '{name}'.", + value.loc, + ) + def _visit_FuncCall(self, node: FuncCall) -> PineType: if self._pine_version == 6 and not self._in_v5_library_definition(node): self._parameter_qualifiers.check_builtin(node) @@ -7589,31 +7634,7 @@ def _visit_FuncCall(self, node: FuncCall) -> PineType: visited_member_arg_types[id(arg)] = self._visit(arg) for val in node.kwargs.values(): visited_member_arg_types[id(val)] = self._visit(val) - if (self._pine_version == 6 - and not self._in_v5_library_definition(node) - and isinstance(obj, Identifier) and obj.name == "timeframe" - and self._value_bindings.declaration_bindings.get(id(obj)) is None): - intrinsic = sigs.BUILTIN_FUNCTIONS.get(f"{obj.name}.{member}") - if intrinsic is not None: - bound = self._bind_callable_args(node, intrinsic.param_names) - for parameter, value in zip(intrinsic.primary.params, bound): - if value is None or parameter.pine_type != PineType.STRING: - continue - # An untyped helper's provisional float is not proof - # that the caller supplies a numeric value. - actual_type = self._certain_scalar_type(value) - if actual_type in (PineType.INT, PineType.FLOAT): - # Generic na adopts the parameter's string type; - # a numeric value cannot satisfy that signature. - function_name = intrinsic.name - argument_type = actual_type.value - parameter_type = parameter.pine_type.value - name = parameter.name - self._error( - f"Cannot call '{function_name}' with a {argument_type} " - f"argument for {parameter_type} parameter '{name}'.", - value.loc, - ) + self._check_timeframe_string_arguments(node) # UDT method call-site typing. Method definitions may contain an # untyped parameter history read; resolve receiver + args here and # apply the same deferred map-history gate as regular UDFs before diff --git a/pineforge_codegen/analyzer/types.py b/pineforge_codegen/analyzer/types.py index f65daf91..c62ffa06 100644 --- a/pineforge_codegen/analyzer/types.py +++ b/pineforge_codegen/analyzer/types.py @@ -1535,6 +1535,9 @@ def _check_node(self, node): op, operands = node.op, (node.operand,) elif isinstance(node, Ternary): op, operands = "?:", (node.condition, node.true_val, node.false_val) + elif isinstance(node, FuncCall): + self._analyzer._check_timeframe_string_arguments(node, self._parameter_types) + return else: return self._analyzer._check_operator_operands(node, op, operands, self._parameter_types) diff --git a/pineforge_codegen/codegen/visit_call.py b/pineforge_codegen/codegen/visit_call.py index 4b3faf94..28191ca7 100644 --- a/pineforge_codegen/codegen/visit_call.py +++ b/pineforge_codegen/codegen/visit_call.py @@ -159,6 +159,7 @@ from ..limits import iter_ast_nodes from ..security_contexts import ticker_symbol_arg from ..symbols import PineType, TypeSpec, method_receiver_type_name +from ..analyzer.value_bindings import ValueBindings from ..method_binding import ( MethodBindError, bind_function_defaults, @@ -3232,6 +3233,44 @@ def timeframe_call(tf_arg: str) -> str: if argument is None: return timeframe_call("script_tf_") + actual = self._type_spec_from_expr(argument) + if (actual is None and isinstance(argument, MemberAccess) + and isinstance(argument.object, Identifier) + and self.ctx.identifier_decl_bindings.get(id(argument.object)) is None + and ValueBindings.declaration_of(argument.object) is None): + variable_type = sigs.get_variable_type( + argument.object.name, argument.member, + ) + if variable_type != PineType.UNKNOWN: + actual = TypeSpec.primitive(variable_type.value) + actual_cpp_type = self._text_type(argument) + argument_is_string = ( + actual == TypeSpec.primitive("string") + or (actual is None and actual_cpp_type == "std::string") + ) + if (not isinstance(argument, NaLiteral) + and (not argument_is_string or actual_cpp_type == "bool")): + # A call-site variant may resolve an untyped helper after + # analysis. Check before bool-to-text compatibility can + # turn a non-string value into a C++ string. + argument_type = ( + "bool" if actual_cpp_type == "bool" else + (method_receiver_type_name(actual) or str(actual)) + if actual is not None else + {"int": "int", "int64_t": "int", "double": "float"}.get( + actual_cpp_type, actual_cpp_type, + ) + ) + intrinsic = sigs.BUILTIN_FUNCTIONS[f"{namespace}.{func_name}"] + parameter = intrinsic.primary.params[0] + function_name = intrinsic.name + parameter_type = parameter.pine_type.value + name = parameter.name + self._codegen_error( + argument, + f"Cannot call '{function_name}' with a {argument_type} " + f"argument for {parameter_type} parameter '{name}'.", + ) argument_cpp = self._visit_rhs_value( argument, target_cpp_type="std::string", ) diff --git a/tests/fixtures/diagnostic_notes/delta.json b/tests/fixtures/diagnostic_notes/delta.json index 5027fc9a..fae0cb0d 100644 --- a/tests/fixtures/diagnostic_notes/delta.json +++ b/tests/fixtures/diagnostic_notes/delta.json @@ -4,7 +4,8 @@ "user_message" ], "added_codes": [ - "PF-E5087" + "PF-E5087", + "PF-E2095" ], "catalog_additions": { "PF-E5088": { diff --git a/tests/test_empty_timeframe_emission.py b/tests/test_empty_timeframe_emission.py index 07b198a7..add66391 100644 --- a/tests/test_empty_timeframe_emission.py +++ b/tests/test_empty_timeframe_emission.py @@ -132,12 +132,39 @@ def test_numeric_timeframe_is_refused_during_analysis(function, named, argument, @pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) @pytest.mark.parametrize("optimization", ["-O0", "-O2"]) def test_na_timeframe_full_translation_unit_compiles(function, named, optimization): - from tests._compile import compile_cpp + from tests._compile import run_emitted_tu argument = "timeframe = na" if named else "na" cpp = transpile( '//@version=6\nstrategy("Missing timeframe compilation")\n' f"value = timeframe.{function}({argument})\n" ) - compile_cpp(cpp, label=f"timeframe.{function}({argument}) {optimization}", - extra_flags=[optimization]) + driver = r''' +#include +#include +int main() { + std::vector bars(4); + for (std::size_t i = 0; i < bars.size(); ++i) { + bars[i].open = bars[i].high = bars[i].low = bars[i].close = 100.0 + i; + bars[i].volume = 1.0; + bars[i].timestamp = 1704067200000LL + static_cast(i) * 3600000LL; + } + GeneratedStrategy strategy; + strategy.run(bars.data(), bars.size(), "60", "60"); + if (!strategy.last_error().empty()) { + std::cerr << strategy.last_error() << "\n"; + return 1; + } + std::cout << "executed " << static_cast(strategy.value) << "\n"; +} +''' + output = run_emitted_tu( + cpp, driver, opt=optimization, + label=f"timeframe.{function}({argument})", + ) + # This row proves a real optimized build and execution. It records the + # observed value without settling the separate TradingView na-value check. + marker, observed = output.strip().split() + assert marker == "executed" + float(observed) + print(f"timeframe.{function}({argument}) {optimization}: {output.strip()}") diff --git a/tests/test_timeframe_argument_types.py b/tests/test_timeframe_argument_types.py new file mode 100644 index 00000000..b691358a --- /dev/null +++ b/tests/test_timeframe_argument_types.py @@ -0,0 +1,155 @@ +"""Timeframe string parameters use semantic types before C++ conversion.""" + +import pytest + +from pineforge_codegen import transpile +from pineforge_codegen.analyzer import Analyzer +from pineforge_codegen.diagnostic_codes import render_diagnostic +from pineforge_codegen.errors import CompileError, Level, Phase +from pineforge_codegen.lexer import Lexer +from pineforge_codegen.parser import Parser + + +HEADER = '//@version=6\nstrategy("Timeframe argument types")\n' +NON_STRINGS = [ + pytest.param("frames = input.int(14)\n", "frames", "int", id="integer-input"), + pytest.param("", "input.int(7)", "int", id="inline-integer-input"), + pytest.param("counter = 0\ncounter := counter + 1\n", "counter", "int", + id="reassigned-counter"), + pytest.param("", "syminfo.mintick", "float", id="member-value"), + pytest.param("", "timeframe.multiplier", "int", id="integer-timeframe-member"), + pytest.param("", "timeframe.isdaily", "bool", id="boolean-timeframe-member"), + pytest.param("", "strategy.closedtrades", "int", id="integer-strategy-member"), + pytest.param("", "barstate.isfirst", "bool", id="boolean-barstate-member"), + pytest.param("", "ta.sma(close, 3)", "float", id="indicator-result"), + pytest.param("", "input.float(2.25)", "float", id="float-input"), + pytest.param("", "true", "bool", id="boolean-literal"), + pytest.param("", "close > open", "bool", id="boolean-series"), + pytest.param("flag = input.bool(true)\n", "flag", "bool", id="boolean-input"), + pytest.param("flag = false\nflag := close > open\n", "flag", "bool", + id="reassigned-boolean"), + pytest.param("", "color.red", "color", id="color"), + pytest.param("frames = array.new(2, 1)\n", "frames", "array", + id="array"), + pytest.param("frames = matrix.new(1, 1, 2.0)\n", "frames", + "matrix", id="matrix"), + pytest.param("frames = map.new()\n", "frames", + "map", id="map"), + pytest.param('type FrameBox\n string frame\n' + 'boxValue = FrameBox.new("60")\n', "boxValue", "FrameBox", + id="object"), +] + + +def _assert_type_diagnostic(diagnostic, function, pine_type): + assert diagnostic.level == Level.ERROR + assert diagnostic.code == "PF-E2095" + assert diagnostic.args == { + "function_name": f"timeframe.{function}", + "name": "timeframe", + "argument_type": pine_type, + "parameter_type": "string", + } + assert diagnostic.hint is None + assert render_diagnostic(diagnostic.code, diagnostic.args) == ( + diagnostic.message, diagnostic.hint, + ) + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +@pytest.mark.parametrize("scope", [ + "direct", "helper", "helper-local", "named-helper", "typed-helper", +]) +@pytest.mark.parametrize("conversion", [False, True], ids=["alone", "bool-to-text"]) +@pytest.mark.parametrize("declarations,argument,pine_type", NON_STRINGS) +def test_non_string_timeframes_are_refused_by_semantic_type( + function, named, scope, conversion, declarations, argument, pine_type, +): + source = HEADER + declarations + if conversion: + source += "unrelatedString = str.tostring(close > open)\n" + value = argument if scope == "direct" else "frameArg" + if scope == "helper-local": + value = "localFrame" + supplied = f"timeframe = {value}" if named else value + call = f"timeframe.{function}({supplied})" + if scope == "direct": + source += f"value = {call}\n" + elif scope in ("helper", "named-helper", "typed-helper"): + parameter = f"{pine_type} frameArg" if scope == "typed-helper" else "frameArg" + actual = f"frameArg = {argument}" if scope == "named-helper" else argument + source += f"withFrame({parameter}) => {call}\nvalue = withFrame({actual})\n" + else: + source += ( + "withFrame(frameArg) =>\n" + " localFrame = frameArg\n" + f" {call}\n" + f"value = withFrame({argument})\n" + ) + ast = Parser(Lexer(source).tokenize(), source=source).parse() + with pytest.raises(CompileError) as refused: + Analyzer(ast).analyze() + diagnostic, = refused.value.diagnostics + _assert_type_diagnostic(diagnostic, function, pine_type) + assert diagnostic.phase == Phase.ANALYZER + line, = [(index, line) for index, line in enumerate(source.splitlines(), 1) + if call in line] + assert diagnostic.location.line == line[0] + assert diagnostic.location.col == line[1].index(supplied) + ( + len("timeframe = ") if named else 0 + ) + 1 + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +@pytest.mark.parametrize("conversion", [False, True], ids=["alone", "bool-to-text"]) +@pytest.mark.parametrize("body,pine_type", [ + ("input.int(14)", "int"), + ("syminfo.mintick", "float"), + ("close > open", "bool"), +]) +def test_helper_return_cannot_be_converted_to_a_timeframe_string( + function, named, conversion, body, pine_type, +): + source = HEADER + if conversion: + source += "unrelatedString = str.tostring(close > open)\n" + source += f"chosenFrame() => {body}\n" + supplied = "timeframe = chosenFrame()" if named else "chosenFrame()" + source += f"value = timeframe.{function}({supplied})\n" + with pytest.raises(CompileError) as refused: + transpile(source) + diagnostic, = refused.value.diagnostics + _assert_type_diagnostic(diagnostic, function, pine_type) + assert diagnostic.phase in (Phase.ANALYZER, Phase.CODEGEN) + assert diagnostic.location.line == len(source.splitlines()) + assert diagnostic.location.col == source.splitlines()[-1].index("chosenFrame()") + 1 + + +@pytest.mark.parametrize("function", ["change", "in_seconds"]) +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +@pytest.mark.parametrize("argument,pine_type", [ + ("input.int(14)", "int"), + ("syminfo.mintick", "float"), + ("true", "bool"), +]) +def test_emitter_checks_type_before_implicit_string_conversion( + monkeypatch, function, named, argument, pine_type, +): + # An internal caller can reach emission after bypassing an analyzer guard. + # The emitter must still refuse the semantic type before bool text lowering. + monkeypatch.setattr(Analyzer, "_check_timeframe_string_arguments", + lambda *args, **kwargs: None, raising=False) + supplied = f"timeframe = {argument}" if named else argument + source = ( + HEADER + "unrelatedString = str.tostring(close > open)\n" + f"value = timeframe.{function}({supplied})\n" + ) + with pytest.raises(CompileError) as refused: + transpile(source) + diagnostic, = refused.value.diagnostics + _assert_type_diagnostic(diagnostic, function, pine_type) + assert diagnostic.phase == Phase.CODEGEN + assert diagnostic.location.line == 4 + assert diagnostic.location.col == source.splitlines()[3].index(argument) + 1 diff --git a/tests/test_timeframe_string_tu.py b/tests/test_timeframe_string_tu.py new file mode 100644 index 00000000..fb67f87b --- /dev/null +++ b/tests/test_timeframe_string_tu.py @@ -0,0 +1,77 @@ +"""Build and execute complete string-argument TUs at distinct optimizations.""" + +import pytest + +from pineforge_codegen import transpile +from tests._compile import run_emitted_tu + + +@pytest.mark.parametrize("optimization", ["-O0", "-O2"]) +def test_string_timeframe_controls_build_and_run(optimization): + source = ( + '//@version=6\nstrategy("String timeframe compilation")\n' + 'frameChoice = input.timeframe("")\n' + 'string missingFrame = na\n' + 'unrelatedString = str.tostring(close > open)\n' + 'chosenFrame() => frameChoice\n' + 'withSeconds(frameArg) => timeframe.in_seconds(frameArg)\n' + 'withChange(frameArg) => timeframe.change(timeframe = frameArg)\n' + 'typedSeconds(string frameArg) => timeframe.in_seconds(timeframe = frameArg)\n' + 'typedChange(string frameArg) => timeframe.change(frameArg)\n' + ) + arguments = { + "explicit": '"240"', + "empty": '""', + "variable": "frameChoice", + "return": "chosenFrame()", + "formatted": "str.tostring(60)", + "hostString": "strategy.position_entry_name", + "typedMissing": "missingFrame", + } + names = [] + for function in ("in_seconds", "change"): + for kind, argument in arguments.items(): + for named in (False, True): + name = f"{function}_{kind}_{'named' if named else 'positional'}" + names.append(name) + supplied = f"timeframe = {argument}" if named else argument + source += f"{name} = timeframe.{function}({supplied})\n" + for helper in ("withSeconds", "withChange", "typedSeconds", "typedChange"): + for kind, argument in (("empty", '""'), ("explicit", '"240"')): + name = f"{helper}_{kind}" + names.append(name) + source += f"{name} = {helper}({argument})\n" + driver = r''' +#include +#include +int main() { + std::vector bars(5); + for (std::size_t i = 0; i < bars.size(); ++i) { + bars[i].open = bars[i].high = bars[i].low = bars[i].close = 100.0 + i; + bars[i].volume = 1.0; + bars[i].timestamp = 1704067200000LL + static_cast(i) * 3600000LL; + } + GeneratedStrategy strategy; + strategy.run(bars.data(), bars.size(), "60", "60"); + if (!strategy.last_error().empty()) { + std::cerr << strategy.last_error() << "\n"; + return 1; + } +''' + for name in names: + driver += ( + f' std::cout << "{name} " << ' + f'static_cast(strategy.{name}) << "\\n";\n' + ) + driver += "}\n" + output = run_emitted_tu( + transpile(source), driver, opt=optimization, label="string timeframe controls", + ) + # Value parity has its own replay oracles. This test's target is building + # every control and executing it through the real generated strategy. + rows = [line.split() for line in output.splitlines()] + assert [row[0] for row in rows] == names + assert all(len(row) == 2 for row in rows) + for _, value in rows: + float(value) + print(f"string timeframe controls {optimization}:\n{output}", end="") From b2db9774d30fc1a2f8162950dfefbc67edc40bf6 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 14:07:38 +0800 Subject: [PATCH 13/27] fix(codegen): allocate function clone names against authored members Keep generated state and callable clone identities distinct from authored members and use the same allocated identities at declarations and calls. Preserve noncolliding names and duplicate-state assertions. --- CHANGELOG.md | 5 + pineforge_codegen/codegen/base.py | 68 ++++-- pineforge_codegen/codegen/emit_top.py | 8 +- pineforge_codegen/codegen/helpers.py | 34 ++- pineforge_codegen/codegen/ta.py | 2 +- pineforge_codegen/codegen/visit_call.py | 12 +- tests/test_clone_name_allocation.py | 299 ++++++++++++++++++++++++ 7 files changed, 399 insertions(+), 29 deletions(-) create mode 100644 tests/test_clone_name_allocation.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 88dac8f9..8e86efa4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,11 @@ supported as exact pairs; on the 0.x line they are independent. See the ## Unreleased (next release) +- Generated function variants and persistent local copies avoid authored and + generated member names, including names ending in `_cs1`. Declarations, + calls and history storage use the same allocated identities. Regenerate C++ + and relink affected strategies. + - A zone name the engine rejects now makes calendar-form `timestamp()` stop the run at its first evaluation, instead of silently calculating a UTC value. The paired engine reports a typed timezone error identifying the failed bar. diff --git a/pineforge_codegen/codegen/base.py b/pineforge_codegen/codegen/base.py index 6bd0a8ad..2abc379f 100644 --- a/pineforge_codegen/codegen/base.py +++ b/pineforge_codegen/codegen/base.py @@ -736,7 +736,12 @@ def func_var_storage(owner: str, raw_name: str) -> str: remap = {} for orig_name in orig_names: safe = self._safe_name(orig_name) - remap[safe] = f"{safe}_cs{cs_idx}" + # Several enclosing owners can refer to the same clone. + # Its identity belongs to the original member + callsite, + # independently of which owner first requests its name. + remap[safe] = self._generated_class_name( + ("var-clone", safe, cs_idx), f"{safe}_cs{cs_idx}" + ) self._func_cs_var_remap[(fname, cs_idx)] = remap self._func_var_members_set.update( orig_name for orig_name in orig_names) @@ -1221,8 +1226,8 @@ def func_var_storage(owner: str, raw_name: str) -> str: _h = _hints[_i] if _i < len(_hints) else None if _h and str(_h).replace(" ", "") in _SKIP_DECL_TYPES: self._visual_drop_vars.add(_p) - # Build set of all member names (series vars, var members) for collision - # detection. It precedes the security metadata below: a timeframe + # Build the authored/analyzer data-member inventory for callable name + # collisions. It precedes the security metadata below: a timeframe # computed by a user function (``tf = tfFromLabel(choice)``) renders # that call through ``_func_safe_name`` at registration time. self._all_member_names: set[str] = set() @@ -1230,6 +1235,15 @@ def func_var_storage(owner: str, raw_name: str) -> str: self._all_member_names.add(self._safe_name(name)) for name, _, _ in ctx.var_members: self._all_member_names.add(self._safe_name(name)) + for name, _ptype in ctx.global_var_decls: + self._all_member_names.add(self._safe_name(name)) + # Freeze natural callable identities before security rendering and + # later support allocators. Calls and declarations must use this same + # allocation even when an authored member is spelled like a clone. + for fi in ctx.func_infos: + self._func_cpp_base_name(fi.name) + for cs_idx in range(ctx.func_call_site_counts.get(fi.name, 0)): + self._func_variant_name(fi.name, cs_idx) # Collect request.security metadata per call self._security_eval_info: list[dict] = [] self._security_ta_variant_names: dict[tuple[int, int, tuple], str] = {} @@ -1573,6 +1587,7 @@ def _prepare_runtime_scalar_var_initializers(self) -> None: self._runtime_var_init_flags: dict[tuple[int, str], str] = {} used_names = set(self._all_member_names) + used_names.update(self._generated_class_names.values()) # ``_all_member_names`` historically covers persistent ``var`` and # Series members only. Plain global declarations are class members as # well, so include them before minting a generated flag; otherwise a @@ -1609,7 +1624,7 @@ def _prepare_runtime_scalar_var_initializers(self) -> None: for callsite in range( self.ctx.func_call_site_counts.get(func_info.name, 0) ): - used_names.add(f"{emitted}_cs{callsite}") + used_names.add(self._func_variant_name(func_info.name, callsite)) used_names.update(inst["name"] for inst in self._fresh_instances) def allocate_flag(base: str) -> str: @@ -2059,7 +2074,7 @@ def fixnan_originals(fname: str) -> list[str]: return list(self._func_cs_fixnan_remap.get((fname, 0), {}).keys()) def natural_name(fname: str, cs_idx: int) -> str: - return f"{self._func_cpp_base_name(fname)}_cs{cs_idx}" + return self._func_variant_name(fname, cs_idx) interned: dict[tuple, dict] = {} worklist: list[dict] = [] @@ -2178,17 +2193,26 @@ def natural_name(fname: str, cs_idx: int) -> str: ginst = interned.get(key) if ginst is None: fresh_counter += 1 - inst_name = f"{self._func_cpp_base_name(g_name)}__ni{fresh_counter}" + inst_name = self._generated_class_name( + ("function-instance", fresh_counter), + f"{self._func_cpp_base_name(g_name)}__ni{fresh_counter}", + ) fvar_remap: dict[str, str] = {} for v in var_originals(g_name): - fresh_member = f"{v}__ni{fresh_counter}" + fresh_member = self._generated_class_name( + ("var-instance", fresh_counter, v), + f"{v}__ni{fresh_counter}", + ) fvar_remap[v] = fresh_member self._fresh_var_members.append((g_name, v, fresh_member)) # Fresh fixnan members: each path gets its OWN previous- # value member so two call paths never share fixnan state. ffixnan_remap: dict[str, str] = {} for orig_fn_member in fixnan_originals(g_name): - fresh_fn_member = f"{orig_fn_member}__ni{fresh_counter}" + fresh_fn_member = self._generated_class_name( + ("fixnan-instance", fresh_counter, orig_fn_member), + f"{orig_fn_member}__ni{fresh_counter}", + ) ffixnan_remap[orig_fn_member] = fresh_fn_member # Find the original FixnanCallSite to carry its type. orig_fn_site = None @@ -3948,6 +3972,15 @@ def _func_cpp_base_name(self, fname: str) -> str: return self._emit_udt_method_cpp_name(fi) return self._func_safe_name(fname) + def _func_variant_name(self, fname: str, cs_idx: int) -> str: + """Resolve a callable clone once for emission, dispatch and history.""" + name = self._generated_class_name( + ("function-clone", fname, cs_idx), + f"{self._func_cpp_base_name(fname)}_cs{cs_idx}", + ) + self._func_variant_callsite[(fname, name)] = cs_idx + return name + def _inline_history_contexts_for_owner(self, owner: str | None) -> list[str | None]: """Return every method-emission context that owns one source AST site. @@ -3970,7 +4003,7 @@ def _inline_history_contexts_for_owner(self, owner: str | None) -> list[str | No ) and total_cs > 0 if cloned: contexts: list[str | None] = [ - f"{self._func_cpp_base_name(owner)}_cs{idx}" + self._func_variant_name(owner, idx) for idx in range(total_cs) ] else: @@ -4131,11 +4164,7 @@ def owner_cs_for_context( ) -> int | None: if owner is None or context is None: return None - prefix = f"{self._func_cpp_base_name(owner)}_cs" - if not context.startswith(prefix): - return None - suffix = context[len(prefix):] - return int(suffix) if suffix.isdigit() else None + return self._func_variant_callsite.get((owner, context)) def target_cs_for_context( fi, @@ -4271,7 +4300,7 @@ def emitted_context_for_call( fi, call, owner, owner_context ) if target_cs is not None: - return f"{self._func_cpp_base_name(fi.name)}_cs{target_cs}" + return self._func_variant_name(fi.name, target_cs) return None requested_roots: set[tuple[str, str | None]] = set() @@ -4733,6 +4762,7 @@ def _prepare_udt_generated_names(self) -> None: getattr(self, "_runtime_var_init_flag_used_names", ()) ) class_used.update(self._all_member_names) + class_used.update(self._generated_class_names.values()) class_used.update( self._safe_name(name) for name in self._all_bound_names ) @@ -6209,12 +6239,14 @@ def allocate_legacy_flag(base: str) -> str: for fi in self.ctx.func_infos: if fi.name not in self.ctx.func_var_members: continue - emitted = self._func_cpp_base_name(fi.name) total_cs = self.ctx.func_call_site_counts.get(fi.name, 0) variants = range(total_cs) if total_cs > 0 else (None,) for cs_idx in variants: - suffix = f"_cs{cs_idx}" if cs_idx is not None else "" - flag = allocate_legacy_flag(f"_fvinit_{emitted}{suffix}") + emitted = ( + self._func_variant_name(fi.name, cs_idx) + if cs_idx is not None else self._func_cpp_base_name(fi.name) + ) + flag = allocate_legacy_flag(f"_fvinit_{emitted}") lines.append(f" bool {flag} = false;") for inst in self._fresh_instances: diff --git a/pineforge_codegen/codegen/emit_top.py b/pineforge_codegen/codegen/emit_top.py index d9c67b33..c23911e4 100644 --- a/pineforge_codegen/codegen/emit_top.py +++ b/pineforge_codegen/codegen/emit_top.py @@ -2358,10 +2358,14 @@ def _emit_udt_method_cpp_name(self, fi: FuncInfo) -> str: allocated = {} for key, name in methods: encoded = cpp_identifier_fragment(name) - allocated[key] = ( + preferred = ( name if encoded == name else self._allocate_generated_cpp_name(encoded, used) ) + allocated[key] = self._generated_class_name( + ("typed-method", key), preferred + ) + used.add(allocated[key]) self._escaped_method_cpp_names = allocated return self._func_safe_name(allocated[fi.name]) @@ -2703,7 +2707,7 @@ def _emit_func_def_body(self, fi: FuncInfo, lines: list[str], self._active_call_site_idx = None self._current_instance_name = instance["name"] elif call_site_idx is not None: - func_name = f"{func_name}_cs{call_site_idx}" + func_name = self._func_variant_name(fi.name, call_site_idx) remap = self._func_cs_ta_remap.get((fi.name, call_site_idx), {}) self._active_ta_remap = remap var_remap = self._func_cs_var_remap.get((fi.name, call_site_idx), {}) diff --git a/pineforge_codegen/codegen/helpers.py b/pineforge_codegen/codegen/helpers.py index d0d666cc..d1693e09 100644 --- a/pineforge_codegen/codegen/helpers.py +++ b/pineforge_codegen/codegen/helpers.py @@ -750,6 +750,34 @@ def _initialise_safe_names(self, ast) -> None: self._local_function_names[name] = self._safe_name(name) self._allocate_safe_name(name) + # Analyzer-qualified storage (including sibling-block members) and + # TA/fixnan sites can have names absent from the authored AST. Clones + # and generated methods share their C++ class namespace. Reserve exact + # identities, rather than treating every authored _csN suffix as private. + self._safe_name_occupied.update( + self._safe_name(name) for name in member_names + ) + self._safe_name_occupied.update( + self._safe_name(name) + for name, _ptype in getattr(ctx, "global_var_decls", ()) + ) + for sites in ( + getattr(ctx, "ta_call_sites", ()), + getattr(ctx, "fixnan_sites", ()), + ): + self._safe_name_occupied.update(site.member_name for site in sites) + self._generated_class_names: dict[tuple, str] = {} + self._func_variant_callsite: dict[tuple[str, str], int] = {} + + def _generated_class_name(self, key: tuple, preferred: str) -> str: + """Allocate one stable member/method identity outside authored names.""" + allocated = self._generated_class_names + if key not in allocated: + allocated[key] = self._allocate_generated_cpp_name( + preferred, self._safe_name_occupied + ) + return allocated[key] + def _allocate_safe_name(self, name: str) -> str: if name in LEGACY_CPP_RESERVED or name in BUILTIN_ACCESSOR_NAMES: base = f"_{name}_" @@ -784,12 +812,14 @@ def _safe_name(self, name: str) -> str: return name def _func_safe_name(self, name: str) -> str: - """Prefix function names that collide with class members (series vars or var members).""" + """Allocate function names that collide with class data members.""" safe = getattr(self, "_local_function_names", {}).get( name, self._safe_name(name), ) if safe in self._all_member_names: - return f"_fn_{safe}" + return self._generated_class_name( + ("function", name), f"_fn_{safe}" + ) return safe def _resolve_callee(self, callee) -> tuple[str | None, str | None]: diff --git a/pineforge_codegen/codegen/ta.py b/pineforge_codegen/codegen/ta.py index 765796f6..29b2985a 100644 --- a/pineforge_codegen/codegen/ta.py +++ b/pineforge_codegen/codegen/ta.py @@ -547,7 +547,7 @@ def allocate(base: str, reserved: set[str]) -> str: reserved_types.add(emitted) total = self.ctx.func_call_site_counts.get(info.name, 0) for callsite in range(total): - reserved_members.add(f"{emitted}_cs{callsite}") + reserved_members.add(self._func_variant_name(info.name, callsite)) for instance in getattr(self, "_fresh_instances", ()): reserved_members.add(instance["name"]) self._lazy_source_clock_type_name = allocate("_PFLazySourceClock", reserved_types) diff --git a/pineforge_codegen/codegen/visit_call.py b/pineforge_codegen/codegen/visit_call.py index efb19c5c..3fe33c25 100644 --- a/pineforge_codegen/codegen/visit_call.py +++ b/pineforge_codegen/codegen/visit_call.py @@ -428,12 +428,12 @@ def _udt_method_call_emit_name(self, fi, node: FuncCall) -> str: cs_info = self.ctx.func_call_cs_map.get(id(node)) if self._active_call_site_idx is not None and cs_info is not None: - return f"{base}_cs{self._active_call_site_idx}" + return self._func_variant_name(fi.name, self._active_call_site_idx) if cs_info is not None and cs_info[0] == fi.name: - return f"{base}_cs{cs_info[1]}" + return self._func_variant_name(fi.name, cs_info[1]) if (self._active_call_site_idx is not None and self.ctx.func_call_site_counts.get(fi.name, 0) > 1): - return f"{base}_cs{self._active_call_site_idx}" + return self._func_variant_name(fi.name, self._active_call_site_idx) return base def _typed_user_method_info(self, receiver, member: str): @@ -3612,16 +3612,16 @@ def _visit_arg_for_series(arg_node, arg_idx): # the parent's active call-site index. This ensures sub-functions # called from ma_cs6() use their _cs6 variant, not _cs0. fname, _ = cs_info - emit_name = f"{self._func_safe_name(fname)}_cs{self._active_call_site_idx}" + emit_name = self._func_variant_name(fname, self._active_call_site_idx) elif cs_info is not None: fname, cs_idx = cs_info - emit_name = f"{self._func_safe_name(fname)}_cs{cs_idx}" + emit_name = self._func_variant_name(fname, cs_idx) elif (self._active_call_site_idx is not None and func_name in self._func_names and self.ctx.func_call_site_counts.get(func_name, 0) > 1): # Inside a per-call-site variant: propagate call-site index to # sub-functions that also have variants (for state isolation) - emit_name = f"{self._func_safe_name(func_name)}_cs{self._active_call_site_idx}" + emit_name = self._func_variant_name(func_name, self._active_call_site_idx) call_head = emit_name if namespace is None and func_name in self._func_names: return self._ordered_user_call_expr( diff --git a/tests/test_clone_name_allocation.py b/tests/test_clone_name_allocation.py new file mode 100644 index 00000000..b55a5d53 --- /dev/null +++ b/tests/test_clone_name_allocation.py @@ -0,0 +1,299 @@ +"""Generated state and methods share a namespace with authored identifiers.""" + +from __future__ import annotations + +import re + +import pytest + +from pineforge_codegen import transpile +from pineforge_codegen.analyzer import Analyzer +from pineforge_codegen.codegen import CodeGen +from pineforge_codegen.lexer import Lexer +from pineforge_codegen.parser import Parser +from tests._compile import compile_cpp +from tests._e2e import reference_codegen, transpile_json + + +_VAR_CLONE = """//@version=6 +strategy("clone member spelling") +f() => + var float acc = 0.0 + acc += ta.ema(close, 3) + acc +a = f() +b = f() +var float acc_cs1 = 0.0 +acc_cs1 := acc_cs1 + 1.0 +plot(a + b + acc_cs1) +""" + +_METHOD_CLONE = """//@version=6 +strategy("clone method spelling") +f(src) => ta.ema(src, 3) +a = f(close) +b = f(open) +var float f_cs1 = 2.0 +plot(a + b + f_cs1) +""" + + +def _emit(source: str) -> tuple[CodeGen, str]: + program = Parser(Lexer(source).tokenize(), source=source).parse() + generator = CodeGen(Analyzer(program).analyze()) + return generator, generator.generate() + + +def _scalar_member(cpp: str, name: str) -> None: + declarations = re.findall( + rf"^ double {re.escape(name)}(?: = [^;\n]+)?;$", cpp, re.M + ) + assert len(declarations) == 1, name + + +def _method_body(cpp: str, name: str) -> str: + declarations = list(re.finditer( + rf"^ double {re.escape(name)}\([^\n]*\) \{{\n", cpp, re.M + )) + assert len(declarations) == 1, name + start = declarations[0].end() + return cpp[start:cpp.index("\n }", start)] + + +def _has_identifier(cpp: str, name: str) -> bool: + return re.search(rf"\b{re.escape(name)}\b", cpp) is not None + + +@pytest.mark.parametrize("spelling", ["acc_cs1", "aa"]) +def test_persistent_clone_reproducer_and_rename_control(spelling: str) -> None: + generator, cpp = _emit(_VAR_CLONE.replace("acc_cs1", spelling)) + clone = generator._func_cs_var_remap[("f", 1)]["acc"] + + assert generator._safe_name(spelling) == spelling + _scalar_member(cpp, spelling) + _scalar_member(cpp, clone) + assert clone == ("acc_cs1__pf2" if spelling == "acc_cs1" else "acc_cs1") + body = _method_body(cpp, "f_cs1") + assert _has_identifier(body, clone) + assert not _has_identifier(body, spelling) + assert re.search(rf"return {re.escape(clone)};", body) + compile_cpp(cpp, label=f"persistent-clone-{spelling}") + + +@pytest.mark.parametrize("spelling", ["f_cs1", "aa"]) +def test_callable_clone_reproducer_and_rename_control(spelling: str) -> None: + generator, cpp = _emit(_METHOD_CLONE.replace("f_cs1", spelling)) + first = generator._func_variant_name("f", 0) + second = generator._func_variant_name("f", 1) + + assert generator._safe_name(spelling) == spelling + _scalar_member(cpp, spelling) + assert first == "f_cs0" + assert second == ("f_cs1__pf2" if spelling == "f_cs1" else "f_cs1") + for output, method in (("a", first), ("b", second)): + _method_body(cpp, method) + assert re.search(rf"\b{output} = {re.escape(method)}\(", cpp) + compile_cpp(cpp, label=f"callable-clone-{spelling}") + + +def test_clone_fallback_also_avoids_authored_fallback_spelling() -> None: + source = ( + _VAR_CLONE + + "var float acc_cs1__pf2 = 7.0\n" + + "plot(acc_cs1__pf2)\n" + ) + generator, cpp = _emit(source) + clone = generator._func_cs_var_remap[("f", 1)]["acc"] + + assert clone == "acc_cs1__pf3" + for name in ("acc_cs1", "acc_cs1__pf2", clone): + _scalar_member(cpp, name) + body = _method_body(cpp, "f_cs1") + assert _has_identifier(body, clone) + assert not _has_identifier(body, "acc_cs1__pf2") + compile_cpp(cpp, label="occupied-clone-fallback") + + +def test_clone_member_cannot_be_hidden_by_an_authored_parameter() -> None: + generator, cpp = _emit("""//@version=6 +strategy("clone parameter spelling") +f(float acc_cs1) => + var float acc = 0.0 + acc += acc_cs1 + acc +a = f(close) +b = f(open) +plot(a + b) +""") + clone = generator._func_cs_var_remap[("f", 1)]["acc"] + assert clone == "acc_cs1__pf2" + _scalar_member(cpp, clone) + body = _method_body(cpp, "f_cs1") + assert _has_identifier(body, "acc_cs1") + assert _has_identifier(body, clone) + assert re.search(rf"return {re.escape(clone)};", body) + compile_cpp(cpp, label="clone-versus-parameter") + + +@pytest.mark.parametrize("storage", ["var ", ""], ids=["persistent", "ordinary"]) +def test_function_member_prefix_is_allocated_against_authored_members( + storage: str, +) -> None: + source = """//@version=6 +strategy("callable prefix spelling") +echo(float src) => src + 1.0 +var float echo = 10.0 +var float _fn_echo = 20.0 +a = echo(close) +plot(a + echo + _fn_echo) +""" + generator, cpp = _emit(source.replace("var float ", f"{storage}float ")) + method = generator._func_cpp_base_name("echo") + + assert method == "_fn_echo__pf2" + _method_body(cpp, method) + assert re.search(rf"\ba = {re.escape(method)}\(", cpp) + for name in ("echo", "_fn_echo"): + assert generator._safe_name(name) == name + _scalar_member(cpp, name) + compile_cpp(cpp, label="occupied-function-prefix") + + +def test_generated_method_base_cannot_alias_generated_storage_clone() -> None: + generator, cpp = _emit("""//@version=6 +strategy("generated method and storage spelling") +type Holder + float value +method sample_cs1(Holder self) => self.value +leaf() => + var float _udt_Holder_sample = 0.0 + _udt_Holder_sample += close + _udt_Holder_sample +a = leaf() +b = leaf() +var Holder holder = Holder.new(3.0) +c = holder.sample_cs1() +plot(a + b + c) +""") + storage = generator._func_cs_var_remap[("leaf", 1)]["_udt_Holder_sample"] + info = next(fi for fi in generator.ctx.func_infos if fi.is_udt_method) + method = generator._func_cpp_base_name(info.name) + + assert storage == "_udt_Holder_sample_cs1" + assert method == "_udt_Holder_sample_cs1__pf2" + _scalar_member(cpp, storage) + _method_body(cpp, method) + assert re.search(rf"\bc = {re.escape(method)}\(", cpp) + compile_cpp(cpp, label="generated-method-versus-storage") + + +def test_nested_instances_reserve_authored_method_and_state_spellings() -> None: + generator, cpp = _emit("""//@version=6 +strategy("nested instance spelling") +leaf(int size, float src) => + var float state = 0.0 + state += ta.highest(src, size) + state +f_get(int len, float src) => leaf(len, src) +g_get(int len, float src) => leaf(len, src) +a = f_get(10, close) +b = f_get(20, open) +c = g_get(30, high) +var float leaf__ni1 = 2.0 +var float state__ni1 = 3.0 +plot(a + b + c + leaf__ni1 + state__ni1) +""") + authored = {"leaf__ni1", "state__ni1"} + assert generator._fresh_instances + generated: list[str] = [] + for instance in generator._fresh_instances: + method = instance["name"] + generated.append(method) + body = _method_body(cpp, method) + for storage in instance["var_remap"].values(): + generated.append(storage) + _scalar_member(cpp, storage) + assert _has_identifier(body, storage) + assert all(not _has_identifier(body, name) for name in authored) + assert authored.isdisjoint(generated) + assert len(generated) == len(set(generated)) + assert "leaf__ni1__pf2" in generated + assert "state__ni1__pf2" in generated + for name in authored: + _scalar_member(cpp, name) + compile_cpp(cpp, label="occupied-nested-instances") + + +def test_renamed_callable_context_keeps_compound_argument_history() -> None: + generator, cpp = _emit("""//@version=6 +strategy("clone context history spelling") +inner(float src) => src[1] +wrapped(float src) => inner(src + 1.0) +a = wrapped(close) +b = wrapped(open) +var float wrapped_cs1 = 2.0 +plot(a + b + wrapped_cs1) +""") + contexts = generator._inline_history_contexts_for_owner("wrapped") + assert contexts == ["wrapped_cs0", "wrapped_cs1__pf2"] + bridges = [ + info for info in generator._inline_history_members + if info["kind"] in {"series_arg", "udf_series_arg"} + ] + assert set(contexts) <= {info["context"] for info in bridges} + for index, context in enumerate(contexts): + assert generator._func_variant_callsite[("wrapped", context)] == index + body = _method_body(cpp, context) + owned = [info for info in bridges if info["context"] == context] + assert all(_has_identifier(body, info["member_name"]) for info in owned) + compile_cpp(cpp, label="renamed-callable-history-context") + + +def test_typed_method_variants_reserve_authored_clone_spelling() -> None: + generator, cpp = _emit("""//@version=6 +strategy("typed method clone spelling") +type Holder + float seed +method sample(Holder self, float src) => self.seed + src[1] +var Holder holder = Holder.new(3.0) +a = holder.sample(close + 1.0) +b = holder.sample(open + 1.0) +var float _udt_Holder_sample_cs1 = 2.0 +plot(a + b + _udt_Holder_sample_cs1) +""") + info = next(fi for fi in generator.ctx.func_infos if fi.is_udt_method) + first = generator._func_variant_name(info.name, 0) + second = generator._func_variant_name(info.name, 1) + + assert first == "_udt_Holder_sample_cs0" + assert second == "_udt_Holder_sample_cs1__pf2" + _scalar_member(cpp, "_udt_Holder_sample_cs1") + for output, method in (("a", first), ("b", second)): + _method_body(cpp, method) + assert re.search(rf"\b{output} = {re.escape(method)}\(", cpp) + assert any(info["kind"] == "series_arg" + for info in generator._inline_history_members) + compile_cpp(cpp, label="occupied-typed-method-clone") + + +@pytest.fixture(scope="module") +def clone_reference(): + reference = reference_codegen("4e51f6d00e3a015194d93b06e51ad90f2404cc14") + if reference is None: + pytest.skip("The clone spelling reference commit is unavailable") + return reference + + +@pytest.mark.parametrize("source, original", [ + (_VAR_CLONE, "acc_cs1"), + (_METHOD_CLONE, "f_cs1"), +]) +def test_noncolliding_controls_keep_complete_reference_cpp( + source, original, clone_reference, tmp_path +) -> None: + source = source.replace(original, "aa") + pine = tmp_path / "strategy.pine" + pine.write_text(source, encoding="utf-8") + reference = transpile_json(pine, root=clone_reference) + assert reference["ok"], reference + assert transpile(source) == reference["cpp"] From c9bb034f6e5c5022abc2869974945195a490cb63 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:03:44 +0800 Subject: [PATCH 14/27] Use the generated timeframe type diagnostic presentation Carry the generated argument-type entry and its pin into the member while preserving all other catalog bytes and order. Use the grammatical message and exact approved user presentation with the existing argument kinds. Retain the same numeric refusal cases and qualifier controls. --- pineforge_codegen/analyzer/base.py | 4 ++-- pineforge_codegen/codegen/visit_call.py | 4 ++-- pineforge_codegen/diagnostics_catalog.json | 2 +- scripts/gen_diagnostics_catalog.py | 8 ++++---- tests/fixtures/diagnostic_codes_pin.json | 2 +- tests/test_diagnostic_codes.py | 4 ++-- tests/test_empty_timeframe_emission.py | 4 ++-- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/pineforge_codegen/analyzer/base.py b/pineforge_codegen/analyzer/base.py index 293ae5bb..3f1f01f8 100644 --- a/pineforge_codegen/analyzer/base.py +++ b/pineforge_codegen/analyzer/base.py @@ -7499,8 +7499,8 @@ def _check_timeframe_string_arguments( parameter_type = parameter.pine_type.value name = parameter.name self._error( - f"Cannot call '{function_name}' with a {argument_type} " - f"argument for {parameter_type} parameter '{name}'.", + f"Cannot call '{function_name}' with an argument of type {argument_type} " + f"for {parameter_type} parameter '{name}'.", value.loc, ) diff --git a/pineforge_codegen/codegen/visit_call.py b/pineforge_codegen/codegen/visit_call.py index 28191ca7..4b659470 100644 --- a/pineforge_codegen/codegen/visit_call.py +++ b/pineforge_codegen/codegen/visit_call.py @@ -3268,8 +3268,8 @@ def timeframe_call(tf_arg: str) -> str: name = parameter.name self._codegen_error( argument, - f"Cannot call '{function_name}' with a {argument_type} " - f"argument for {parameter_type} parameter '{name}'.", + f"Cannot call '{function_name}' with an argument of type {argument_type} " + f"for {parameter_type} parameter '{name}'.", ) argument_cpp = self._visit_rhs_value( argument, target_cpp_type="std::string", diff --git a/pineforge_codegen/diagnostics_catalog.json b/pineforge_codegen/diagnostics_catalog.json index 91f7a3c6..c6eef36f 100644 --- a/pineforge_codegen/diagnostics_catalog.json +++ b/pineforge_codegen/diagnostics_catalog.json @@ -235,7 +235,7 @@ "PF-E2090": {"severity": "error", "area": "analysis", "message": "input.time() needs a constant default in Pine v6: timestamp() with a time zone and calendar numbers is not a constant. Use the date-string form, for example timestamp(\"01 Jan 2023 00:00 +0000\").", "hint": "Use a single date string in timestamp or an integer timestamp literal for the default.", "explanation": "The time-zone overload of timestamp, which takes a zone and calendar numbers, returns a simple int, while a Pine v6 input.time default requires a const int.", "args": {}, "user_message": "input.time() requires a constant default in Pine v6."}, "PF-E2091": {"severity": "error", "area": "analysis", "message": "plot() cannot have a linewidth below 1 in Pine v6: use a value of 1 or greater.", "hint": "Set the plot linewidth to an integer of at least 1.", "explanation": "A Pine v6 plot call cannot use an integer literal below one for linewidth; variable and calculated widths retain their existing handling.", "args": {}, "user_message": "plot() requires a linewidth of at least 1 in Pine v6."}, "PF-E2092": {"severity": "error", "area": "analysis", "message": "\"range\" cannot be used as a variable or function name in Pine v6: use another name.", "hint": "Rename the variable, function or method declaration.", "explanation": "Pine v6 reserves range as a variable and callable declaration name; parameters, user type fields and member access retain their existing handling.", "args": {}, "user_message": "Choose another variable, function or method name instead of range in Pine v6."}, - "PF-E2095": {"severity": "error", "area": "analysis", "message": "Cannot call ''{function_name}'' with a {argument_type} argument for {parameter_type} parameter ''{name}''.", "hint": null, "explanation": "A numeric value cannot be used for a string parameter. Pass a string value of the declared parameter type; a permitted generic na adopts that type.", "args": {"function_name": {"kind": "identifier"}, "argument_type": {"kind": "type"}, "parameter_type": {"kind": "type"}, "name": {"kind": "identifier"}}, "user_message": "Argument ''{name}'' of ''{function_name}'' has type {argument_type}; expected {parameter_type}."}, + "PF-E2095": {"severity": "error", "area": "analysis", "message": "Cannot call ''{function_name}'' with an argument of type {argument_type} for {parameter_type} parameter ''{name}''.", "hint": null, "explanation": "A non-string value cannot be used for a string parameter. Pass a string value of the declared parameter type; a permitted generic na adopts that type.", "args": {"function_name": {"kind": "identifier"}, "argument_type": {"kind": "type"}, "parameter_type": {"kind": "type"}, "name": {"kind": "identifier"}}, "user_message": "Argument ''{name}'' of ''{function_name}'' has type {argument_type}, but requires {parameter_type}."}, "PF-E3001": {"severity": "error", "area": "request-security", "message": "request.security timeframe ''{name}'' cannot be resolved before the first bar: PineForge registers every requested timeframe before the script runs.", "hint": "Pass a literal, an input or a global to the helper that holds request.security.", "explanation": "The timeframe given to request.security is a name whose value PineForge cannot compute before the first bar; use a literal, an input or a global.", "args": {"name": {"kind": "identifier"}}, "user_message": "The timeframe given to request.security is a name whose value PineForge cannot compute before the first bar."}, "PF-E3002": {"severity": "error", "area": "request-security", "message": "request.security timeframe expression cannot be resolved before the first bar: PineForge registers every requested timeframe before the script runs.", "hint": "Pass a literal, an input or a global to the helper that holds request.security.", "explanation": "The timeframe expression given to request.security cannot be computed before the first bar; use a literal, an input or a global.", "args": {}, "user_message": "The timeframe expression given to request.security cannot be computed before the first bar."}, "PF-E3003": {"severity": "error", "area": "request-security", "message": "request.security timeframe switch arms must contain one expression", "hint": "Compute multi-statement timeframe logic before the request.security call or rewrite it as pure switch arms.", "explanation": "A switch that picks a request.security timeframe must have exactly one expression in each arm; compute multi-statement logic beforehand.", "args": {}, "user_message": "A switch that picks a request.security timeframe must have exactly one expression in each arm."}, diff --git a/scripts/gen_diagnostics_catalog.py b/scripts/gen_diagnostics_catalog.py index d0858ed6..ba5f4ca4 100644 --- a/scripts/gen_diagnostics_catalog.py +++ b/scripts/gen_diagnostics_catalog.py @@ -1088,12 +1088,12 @@ def dump(catalog: dict) -> str: "Supply a literal or an expression over earlier constants.", "Strategy setting ''{name}'' must be a compile-time constant.", ), - "Cannot call ''{function_name}'' with a {argument_type} argument for " + "Cannot call ''{function_name}'' with an argument of type {argument_type} for " "{parameter_type} parameter ''{name}''.": ( - "A numeric value cannot be used for a string parameter. Pass a string value " + "A non-string value cannot be used for a string parameter. Pass a string value " "of the declared parameter type; a permitted generic na adopts that type.", - "Argument ''{name}'' of ''{function_name}'' has type {argument_type}; " - "expected {parameter_type}.", + "Argument ''{name}'' of ''{function_name}'' has type {argument_type}, " + "but requires {parameter_type}.", ), } diff --git a/tests/fixtures/diagnostic_codes_pin.json b/tests/fixtures/diagnostic_codes_pin.json index e8719a8e..a59e136b 100644 --- a/tests/fixtures/diagnostic_codes_pin.json +++ b/tests/fixtures/diagnostic_codes_pin.json @@ -236,7 +236,7 @@ "PF-E2090": "dcc4d05f50b881e5345de8c73ec7b7951bedc86c30a24d40bc0077fca5242a0d", "PF-E2091": "2d1b04fd25a4198619628b94bf8f90b4002f33e4081c2b780fea510c49e0b44a", "PF-E2092": "74b483fe343d239ec254f03e839b257611db5295b0433eabd15072f919950f3d", - "PF-E2095": "2f11b08a085935c9d4583e461d14594a607ca43b593b6a706bc042c425f3aa7c", + "PF-E2095": "a2bf71e6ab48d39b72365b5a69960a8622c7b38deb0aace009215b46dc3cb89d", "PF-E3001": "585dad07e797bbe2cfffa9f45e14dee040be5570cc13f8e46fa4c31e953fc92a", "PF-E3002": "db089893825afe5ad1d91934c21107fc58451cadf3d71de2539b9c5805549433", "PF-E3003": "02571beee5163d1596d8a11fe49336dfcf65f86d4fed13254bac6b36023c3c79", diff --git a/tests/test_diagnostic_codes.py b/tests/test_diagnostic_codes.py index 203a1831..0db7aeaf 100644 --- a/tests/test_diagnostic_codes.py +++ b/tests/test_diagnostic_codes.py @@ -143,13 +143,13 @@ def test_classify_reads_arguments_raw(): @pytest.mark.parametrize("message, expected_code, expected_args", [ ( - "Cannot call 'timeframe.change' with a int argument for string parameter 'timeframe'.", + "Cannot call 'timeframe.change' with an argument of type int for string parameter 'timeframe'.", "PF-E2095", {"function_name": "timeframe.change", "name": "timeframe", "argument_type": "int", "parameter_type": "string"}, ), ( - "Cannot call 'chooseFrame' with a float argument for string parameter 'candidate'.", + "Cannot call 'chooseFrame' with an argument of type float for string parameter 'candidate'.", "PF-E2095", {"function_name": "chooseFrame", "name": "candidate", "argument_type": "float", "parameter_type": "string"}, diff --git a/tests/test_empty_timeframe_emission.py b/tests/test_empty_timeframe_emission.py index add66391..43d197fa 100644 --- a/tests/test_empty_timeframe_emission.py +++ b/tests/test_empty_timeframe_emission.py @@ -117,8 +117,8 @@ def test_numeric_timeframe_is_refused_during_analysis(function, named, argument, "parameter_type": "string", } assert diagnostic.message == ( - f"Cannot call 'timeframe.{function}' with a {pine_type} " - "argument for string parameter 'timeframe'." + f"Cannot call 'timeframe.{function}' with an argument of type {pine_type} " + "for string parameter 'timeframe'." ) assert diagnostic.hint is None assert render_diagnostic(diagnostic.code, diagnostic.args) == ( From 5beeddab303d115f13c00f3cf3925aab188bb90c Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:43:56 +0800 Subject: [PATCH 15/27] Locate helper argument expectations inside the builtin call Use the argument occurrence within the call instead of an identically named helper parameter. Keep the exact line and column assertions and all source inputs, diagnostic identities and test rows unchanged. --- tests/test_timeframe_argument_types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_timeframe_argument_types.py b/tests/test_timeframe_argument_types.py index b691358a..9c241aaf 100644 --- a/tests/test_timeframe_argument_types.py +++ b/tests/test_timeframe_argument_types.py @@ -96,7 +96,7 @@ def test_non_string_timeframes_are_refused_by_semantic_type( line, = [(index, line) for index, line in enumerate(source.splitlines(), 1) if call in line] assert diagnostic.location.line == line[0] - assert diagnostic.location.col == line[1].index(supplied) + ( + assert diagnostic.location.col == line[1].index(supplied, line[1].index(call)) + ( len("timeframe = ") if named else 0 ) + 1 From bd8a3c6adf4143f2c692fa854dd1dd8e54346789 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:32:28 +0800 Subject: [PATCH 16/27] Resolve registered argument types and report their source starts Carry registered namespace types through the semantic query used for written helper calls while respecting lexical shadowing and version boundaries. Reuse the existing expression-start locator for timeframe type refusals. --- pineforge_codegen/analyzer/base.py | 20 +++----------------- pineforge_codegen/analyzer/types.py | 19 +++++++++++++++++++ pineforge_codegen/codegen/visit_call.py | 4 +++- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/pineforge_codegen/analyzer/base.py b/pineforge_codegen/analyzer/base.py index 3f1f01f8..ea0b88e8 100644 --- a/pineforge_codegen/analyzer/base.py +++ b/pineforge_codegen/analyzer/base.py @@ -7157,15 +7157,6 @@ def _operator_operand_type( or authored_method ): result = None - if result is None and isinstance(node, MemberAccess): - if isinstance(node.object, Identifier): - ns = node.object.name - if ns == "syminfo": - ptype = sigs.SYMINFO_VARIABLES.get(f"syminfo.{node.member}") - if ptype is not None: - result = TypeSpec.primitive(ptype.value) - elif ns == "timeframe" and node.member == "period": - result = TypeSpec.primitive("string") if result is None and isinstance(node, FuncCall): callee = node.callee if isinstance(callee, Identifier): @@ -7481,13 +7472,6 @@ def _check_timeframe_string_arguments( if value is None or parameter.pine_type != PineType.STRING: continue actual = self._operator_operand_type(value, parameter_types) - if (actual is None and isinstance(value, MemberAccess) - and isinstance(value.object, Identifier) - and self._value_bindings.declaration_bindings.get(id(value.object)) is None - and (parameter_types is None or value.object.name not in parameter_types)): - variable_type = sigs.get_variable_type(value.object.name, value.member) - if variable_type != PineType.UNKNOWN: - actual = TypeSpec.primitive(variable_type.value) if actual is None or actual in { TypeSpec.primitive("string"), TypeSpec.primitive("na"), }: @@ -7498,10 +7482,12 @@ def _check_timeframe_string_arguments( argument_type = method_receiver_type_name(actual) or str(actual) parameter_type = parameter.pine_type.value name = parameter.name + from ..pine_spelling import expr_start + self._error( f"Cannot call '{function_name}' with an argument of type {argument_type} " f"for {parameter_type} parameter '{name}'.", - value.loc, + expr_start(value).loc or value.loc, ) def _visit_FuncCall(self, node: FuncCall) -> PineType: diff --git a/pineforge_codegen/analyzer/types.py b/pineforge_codegen/analyzer/types.py index c62ffa06..a8fa4568 100644 --- a/pineforge_codegen/analyzer/types.py +++ b/pineforge_codegen/analyzer/types.py @@ -1547,6 +1547,25 @@ def _type_spec_from_expr(self, value, **kwargs): return self._analyzer._operator_operand_type( value, self._parameter_types, ) + if (isinstance(value, MemberAccess) + and isinstance(value.object, Identifier) + and self._pine_version == 6 + and not self._in_v5_library_definition(value)): + receiver = value.object + bindings = self._value_bindings.declaration_bindings + # Require the receiver's recorded lexical namespace use. A + # missing record is not proof, and even an unresolved parameter + # or local in the written-call environment shadows a namespace. + if (id(receiver) in bindings and bindings[id(receiver)] is None + and (self._parameter_types is None + or receiver.name not in self._parameter_types) + and receiver.name not in self._udt_fields + and receiver.name not in self._enum_defs): + spec = self._pine_type_to_spec( + sigs.get_variable_type(receiver.name, value.member), + ) + if spec is not None: + return spec return super()._type_spec_from_expr(value, **kwargs) def _visit(self, node): diff --git a/pineforge_codegen/codegen/visit_call.py b/pineforge_codegen/codegen/visit_call.py index 4b659470..6bb0d804 100644 --- a/pineforge_codegen/codegen/visit_call.py +++ b/pineforge_codegen/codegen/visit_call.py @@ -3266,8 +3266,10 @@ def timeframe_call(tf_arg: str) -> str: function_name = intrinsic.name parameter_type = parameter.pine_type.value name = parameter.name + from ..pine_spelling import expr_start + self._codegen_error( - argument, + expr_start(argument), f"Cannot call '{function_name}' with an argument of type {argument_type} " f"for {parameter_type} parameter '{name}'.", ) From 4da8042d9d00f671ec6afcc957fb59a3e1d652bc Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 18:20:51 +0800 Subject: [PATCH 17/27] fix(codegen): separate composed helper and lexical storage names Allocate composed callable and persistent-storage identities outside reserved helper families, and reserve all authored spellings before sibling-block qualification. Keep declarations, calls and history consumers on the same allocated identity. Exercise collision cases through the public pipeline. --- CHANGELOG.md | 8 +- pineforge_codegen/analyzer/base.py | 29 ++- pineforge_codegen/codegen/base.py | 4 +- pineforge_codegen/codegen/helpers.py | 19 +- tests/test_clone_name_allocation.py | 363 ++++++++++++++++++++++++++- 5 files changed, 404 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e86efa4..aa0f91c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,9 +23,11 @@ supported as exact pairs; on the 0.x line they are independent. See the ## Unreleased (next release) - Generated function variants and persistent local copies avoid authored and - generated member names, including names ending in `_cs1`. Declarations, - calls and history storage use the same allocated identities. Regenerate C++ - and relink affected strategies. + generated member names, including names ending in `_cs1` and composed names + that overlap collection-history or requested-context storage. Persistent + declarations in sibling blocks also retain distinct storage when a script + uses their generated spelling. Declarations, calls and history storage use + the same allocated identities. Regenerate C++ and relink affected strategies. - A zone name the engine rejects now makes calendar-form `timestamp()` stop the run at its first evaluation, instead of silently calculating a UTC value. diff --git a/pineforge_codegen/analyzer/base.py b/pineforge_codegen/analyzer/base.py index 772e2426..5b2fd285 100644 --- a/pineforge_codegen/analyzer/base.py +++ b/pineforge_codegen/analyzer/base.py @@ -265,6 +265,7 @@ def __init__(self, ast: Program, filename: str = "", self._block_var_owner: dict[str, int] = {} self._block_var_renames: dict[int, dict[str, str]] = {} self._block_var_seq = 0 + self._block_var_taken: set[str] | None = None # Identifier node identity -> the lexical Symbol.scope resolved while # that node's source scope is live. Codegen uses this narrow # provenance to distinguish true global aliases from same-named local @@ -5018,6 +5019,31 @@ def _record_script_block_binding(self, sym: Symbol, outer: Symbol | None) -> Non or any(id(body) == outer_owner for body in self._block_node_stack) ) + def _allocate_block_var_member(self, node: VarDecl) -> str: + """Keep generated lexical storage apart from every syntax spelling.""" + if self._block_var_taken is None: + self._block_var_taken = set() + for item, _depth in iter_ast_nodes(self._ast): + if self._budget is not None: + self._budget.check(item.loc, Phase.ANALYZER) + for attr in ("name", "var", "member"): + value = getattr(item, attr, None) + if isinstance(value, str): + self._block_var_taken.add(value) + for attr in ("names", "vars", "params", "members"): + self._block_var_taken.update( + value for value in (getattr(item, attr, None) or ()) + if isinstance(value, str) + ) + while True: + if self._budget is not None: + self._budget.check(node.loc, Phase.ANALYZER) + self._block_var_seq += 1 + member_name = f"{node.name}__blk{self._block_var_seq}" + if member_name not in self._block_var_taken: + self._block_var_taken.add(member_name) + return member_name + def _visit_VarDecl(self, node: VarDecl) -> PineType: if (not node.is_var and not node.is_varip and node.type_hint is None and isinstance(node.value, NaLiteral) @@ -5276,8 +5302,7 @@ def _visit_VarDecl(self, node: VarDecl) -> PineType: self._block_var_owner[node.name] = block_id elif owner != block_id: # Sibling-scope collision: disambiguate this declaration. - self._block_var_seq += 1 - member_name = f"{node.name}__blk{self._block_var_seq}" + member_name = self._allocate_block_var_member(node) self._block_var_renames.setdefault(block_id, {})[node.name] = member_name self._var_members.append((member_name, val_type, init_str)) scope_cursor = self._symbols.current_scope diff --git a/pineforge_codegen/codegen/base.py b/pineforge_codegen/codegen/base.py index 2abc379f..8d395f90 100644 --- a/pineforge_codegen/codegen/base.py +++ b/pineforge_codegen/codegen/base.py @@ -2207,9 +2207,11 @@ def natural_name(fname: str, cs_idx: int) -> str: self._fresh_var_members.append((g_name, v, fresh_member)) # Fresh fixnan members: each path gets its OWN previous- # value member so two call paths never share fixnan state. + # These are the owners of the reserved fixnan namespace, + # rather than identities composed from Pine spellings. ffixnan_remap: dict[str, str] = {} for orig_fn_member in fixnan_originals(g_name): - fresh_fn_member = self._generated_class_name( + fresh_fn_member = self._generated_helper_name( ("fixnan-instance", fresh_counter, orig_fn_member), f"{orig_fn_member}__ni{fresh_counter}", ) diff --git a/pineforge_codegen/codegen/helpers.py b/pineforge_codegen/codegen/helpers.py index d1693e09..b5274b03 100644 --- a/pineforge_codegen/codegen/helpers.py +++ b/pineforge_codegen/codegen/helpers.py @@ -717,6 +717,14 @@ def _initialise_safe_names(self, ast) -> None: ctx = getattr(self, "ctx", None) if "time_close" in getattr(ctx, "series_vars", ()): bound.add("time_close") + # Sibling-block and owner-qualified persistent identities need not + # occur in the syntax. Reserve them before escaping any name, and let + # their declarations and consumers use the same safe-name mapping. + storage_names = { + name for name, _, _ in getattr(ctx, "var_members", ()) + } + bound.update(storage_names) + authored.update(storage_names) self._safe_name_map: dict[str, str] = {} self._safe_name_occupied = authored self._safe_name_bound = bound @@ -770,7 +778,16 @@ def _initialise_safe_names(self, ast) -> None: self._func_variant_callsite: dict[tuple[str, str], int] = {} def _generated_class_name(self, key: tuple, preferred: str) -> str: - """Allocate one stable member/method identity outside authored names.""" + """Allocate a composed Pine identity outside fixed helper namespaces.""" + if (preferred in CPP_RESERVED or preferred in BUILTIN_ACCESSOR_NAMES + or is_emitter_temporary(preferred)): + # Suffixing a reserved family such as _secN_* never escapes it. + # Change the leading token before checking exact occupied names. + preferred = f"pf_safe_{preferred}" + return self._generated_helper_name(key, preferred) + + def _generated_helper_name(self, key: tuple, preferred: str) -> str: + """Allocate one stable identity, including a helper's own namespace.""" allocated = self._generated_class_names if key not in allocated: allocated[key] = self._allocate_generated_cpp_name( diff --git a/tests/test_clone_name_allocation.py b/tests/test_clone_name_allocation.py index b55a5d53..348a3143 100644 --- a/tests/test_clone_name_allocation.py +++ b/tests/test_clone_name_allocation.py @@ -2,15 +2,15 @@ from __future__ import annotations +import os import re +from unittest.mock import patch import pytest from pineforge_codegen import transpile -from pineforge_codegen.analyzer import Analyzer from pineforge_codegen.codegen import CodeGen -from pineforge_codegen.lexer import Lexer -from pineforge_codegen.parser import Parser +from pineforge_codegen.codegen.helpers import is_emitter_temporary from tests._compile import compile_cpp from tests._e2e import reference_codegen, transpile_json @@ -37,11 +37,101 @@ plot(a + b + f_cs1) """ +_COLLECTION_CLONE = """//@version=6 +strategy("clone versus collection history") +f() => + var float _pf_collection_hist = 0.0 + _pf_collection_hist += ta.ema(close, 3) + _pf_collection_hist +a = f() +b = f() +var cs1 = array.new(1, 0.0) +array.set(cs1, 0, close) +prev = cs1[1] +plot(a + b + (na(prev) ? 0.0 : array.get(prev, 0))) +""" + +_SECURITY_CLONE = """//@version=6 +strategy("clone versus security state") +f() => + var float _sec1 = 0.0 + _sec1 += ta.ema(close, 3) + _sec1 +a = f() +b = f() +c = f() +var float cs2 = 0.0 +cs2 := cs2 + 1.0 +h() => close + cs2 +v0 = request.security(syminfo.tickerid, "60", close) +v1 = request.security(syminfo.tickerid, "60", h()) +plot(a + b + c + v0 + v1) +""" + +_BLOCK_MEMBER = """//@version=6 +strategy("block member spelling") +var float x__blk1 = 100.0 +float up = na +float down = na +if close > open + var float x = 0.0 + x += 1.0 + up := x +if close <= open + var float x = 0.0 + x += 2.0 + down := x +plot(x__blk1) +plot(nz(up) + nz(down)) +""" + +_RESERVED_CALLABLE = """//@version=6 +strategy("reserved callable history") +inner(float src) => src[1] +_sec7(float src) => inner(src + 1.0) +a = _sec7(close) +b = _sec7(open) +plot(a + b) +""" + +_FUNCTION_HISTORY_PREFIX = """//@version=6 +strategy("callable prefix and global history") +global_hist_1(float src) => src + 1.0 +var float global_hist_1 = 10.0 +a = global_hist_1(close) +var float total = 0.0 +total += close +observe() => total[1] +b = observe() +plot(a + global_hist_1 + b) +""" + +_FRESH_FIXNAN = """//@version=6 +strategy("nested fixnan storage") +leaf(float src) => fixnan(src) +left(float src) => leaf(src) +right(float src) => leaf(src) +a = left(bar_index % 3 == 0 ? na : close) +b = left(bar_index % 4 == 0 ? na : open) +c = right(bar_index % 5 == 0 ? na : high) +plot(a + b + c) +""" + def _emit(source: str) -> tuple[CodeGen, str]: - program = Parser(Lexer(source).tokenize(), source=source).parse() - generator = CodeGen(Analyzer(program).analyze()) - return generator, generator.generate() + # Observe the actual final generator without replacing any pipeline pass. + # Public transpile still runs support checks, re-analysis and the allowlist. + generated: list[CodeGen] = [] + generate = CodeGen.generate + + def record(generator: CodeGen) -> str: + cpp = generate(generator) + generated.append(generator) + return cpp + + with patch.object(CodeGen, "generate", record): + cpp = transpile(source, check_support=True, libraries={}) + return generated[-1], cpp def _scalar_member(cpp: str, name: str) -> None: @@ -249,8 +339,9 @@ def test_renamed_callable_context_keeps_compound_argument_history() -> None: compile_cpp(cpp, label="renamed-callable-history-context") -def test_typed_method_variants_reserve_authored_clone_spelling() -> None: - generator, cpp = _emit("""//@version=6 +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +def test_typed_method_variants_reserve_authored_clone_spelling(named: bool) -> None: + source = """//@version=6 strategy("typed method clone spelling") type Holder float seed @@ -260,7 +351,12 @@ def test_typed_method_variants_reserve_authored_clone_spelling() -> None: b = holder.sample(open + 1.0) var float _udt_Holder_sample_cs1 = 2.0 plot(a + b + _udt_Holder_sample_cs1) -""") +""" + if named: + source = source.replace( + "sample(close + 1.0)", "sample(src=close + 1.0)", + ).replace("sample(open + 1.0)", "sample(src=open + 1.0)") + generator, cpp = _emit(source) info = next(fi for fi in generator.ctx.func_infos if fi.is_udt_method) first = generator._func_variant_name(info.name, 0) second = generator._func_variant_name(info.name, 1) @@ -276,24 +372,267 @@ def test_typed_method_variants_reserve_authored_clone_spelling() -> None: compile_cpp(cpp, label="occupied-typed-method-clone") +@pytest.mark.parametrize("spelling", ["cs1", "aa"]) +def test_clone_and_collection_history_keep_distinct_state(spelling: str) -> None: + generator, cpp = _emit(re.sub(r"\bcs1\b", spelling, _COLLECTION_CLONE)) + clone = generator._func_cs_var_remap[("f", 1)]["_pf_collection_hist"] + variable, = generator._collection_history_variables() + history = generator._collection_history_member(variable) + + assert clone == "pf_safe__pf_collection_hist_cs1" + assert history == f"_pf_collection_hist_{spelling}" + assert clone != history + _scalar_member(cpp, clone) + body = _method_body(cpp, "f_cs1") + assert _has_identifier(body, clone) + assert not _has_identifier(body, history) + assert f"return {clone};" in body + assert f"{history}.open_slot({spelling}," in cpp + assert re.search(rf"{re.escape(history)}\.(?:reference|value)\(1\)", cpp) + assert f"{history}.close({spelling}" in cpp + compile_cpp(cpp, label=f"clone-collection-history-{spelling}") + + +def test_reserved_authored_escape_and_composed_clone_do_not_alias() -> None: + generator, cpp = _emit( + _COLLECTION_CLONE + + "var float _pf_collection_hist_cs1 = 7.0\n" + + "plot(_pf_collection_hist_cs1)\n" + ) + authored = generator._safe_name("_pf_collection_hist_cs1") + clone = generator._func_cs_var_remap[("f", 1)]["_pf_collection_hist"] + assert authored == "pf_safe__pf_collection_hist_cs1" + assert clone == f"{authored}__pf2" + for member in (authored, clone): + _scalar_member(cpp, member) + body = _method_body(cpp, "f_cs1") + assert f"return {clone};" in body + assert not _has_identifier(body, authored) + compile_cpp(cpp, label="authored-escape-versus-composed-clone") + + +@pytest.mark.parametrize("spelling", ["cs2", "aa"]) +def test_clone_and_requested_mutable_state_stay_distinct(spelling: str) -> None: + generator, cpp = _emit(re.sub(r"\bcs2\b", spelling, _SECURITY_CLONE)) + clone = generator._func_cs_var_remap[("f", 2)]["_sec1"] + state = generator._security_state_name(1, spelling) + + assert clone == "pf_safe__sec1_cs2" + assert state == f"_sec1_{spelling}" + assert clone != state + assert _has_identifier(cpp, state) + _scalar_member(cpp, clone) + body = _method_body(cpp, "f_cs2") + assert _has_identifier(body, clone) + assert not _has_identifier(body, state) + assert f"return {clone};" in body + compile_cpp(cpp, label=f"clone-requested-state-{spelling}") + + +@pytest.mark.parametrize("spelling", ["x__blk1", "aa"]) +def test_sibling_storage_avoids_an_authored_derived_name(spelling: str) -> None: + generator, cpp = _emit(_BLOCK_MEMBER.replace("x__blk1", spelling)) + members = [ + meta[1] for meta in generator.ctx.var_member_metadata_by_node.values() + if meta[0].name == "x" + ] + assert members == ["x", "x__blk2" if spelling == "x__blk1" else "x__blk1"] + assert spelling not in members + for member in [spelling, *members]: + _scalar_member(cpp, member) + assert f"up = {members[0]};" in cpp + assert f"down = {members[1]};" in cpp + assert f"{members[0]} += 1.0;" in cpp + assert f"{members[1]} += 2.0;" in cpp + compile_cpp(cpp, label=f"sibling-storage-{spelling}") + + +@pytest.mark.parametrize("stem", [ + "_pf_collection_hist", "_sec7", "_precalc", "_strat", +]) +def test_composed_reserved_storage_avoids_a_parameter_at_its_escape( + stem: str, +) -> None: + parameter = f"pf_safe_{stem}_cs1" + generator, cpp = _emit(f"""//@version=6 +strategy("reserved clone parameter") +f(float {parameter}) => + var float {stem} = 0.0 + {stem} += {parameter} + {stem} +a = f(close) +b = f(open) +plot(a + b) +""") + clone = generator._func_cs_var_remap[("f", 1)][stem] + assert is_emitter_temporary(f"{stem}_cs1") + assert not is_emitter_temporary(clone) + assert clone == f"{parameter}__pf2" + _scalar_member(cpp, clone) + body = _method_body(cpp, "f_cs1") + assert _has_identifier(body, parameter) + assert _has_identifier(body, clone) + assert f"return {clone};" in body + checkpoint = cpp.split("_pf_script_state_checkpoint_.emplace(_PFScriptState{", 1)[1] + checkpoint = checkpoint.split("\n });", 1)[0] + assert re.search(rf"^\s+{re.escape(clone)},$", checkpoint, re.M) + compile_cpp(cpp, label="reserved-clone-versus-escaped-parameter") + + +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +def test_reserved_callable_variants_keep_their_history_contexts(named: bool) -> None: + source = _RESERVED_CALLABLE + if named: + source = source.replace( + "_sec7(close)", "_sec7(src=close)", + ).replace("_sec7(open)", "_sec7(src=open)") + generator, cpp = _emit(source) + contexts = generator._inline_history_contexts_for_owner("_sec7") + assert contexts == ["pf_safe__sec7_cs0", "pf_safe__sec7_cs1"] + bridges = [ + info for info in generator._inline_history_members + if info["kind"] in {"series_arg", "udf_series_arg"} + ] + for index, context in enumerate(contexts): + assert generator._func_variant_callsite[("_sec7", context)] == index + body = _method_body(cpp, context) + owned = [info for info in bridges if info["context"] == context] + assert owned + assert all(_has_identifier(body, info["member_name"]) for info in owned) + assert re.search(rf"\b{'ab'[index]} = {context}\(", cpp) + compile_cpp(cpp, label="reserved-callable-history-contexts") + + +def test_callable_prefix_cannot_capture_fixed_global_history_storage() -> None: + generator, cpp = _emit(_FUNCTION_HISTORY_PREFIX) + method = generator._func_cpp_base_name("global_hist_1") + assert method == "pf_safe__fn_global_hist_1" + _method_body(cpp, method) + assert re.search(rf"\ba = {method}\(", cpp) + assert re.search(r"^ Series _fn_global_hist_1;$", cpp, re.M) + assert any( + info["kind"] == "fn_global_hist" and info["member_name"] == "_fn_global_hist_1" + for info in generator._inline_history_members + ) + _scalar_member(cpp, "global_hist_1") + compile_cpp(cpp, label="callable-prefix-versus-global-history") + + +def test_sibling_allocation_reserves_later_names_and_history_storage() -> None: + source = _BLOCK_MEMBER.replace("up := x", "up := nz(x[1])").replace( + "down := x", "down := nz(x[1])", + ) + "var float x__blk2 = 7.0\nplot(x__blk2)\n" + generator, cpp = _emit(source) + members = [ + generator._safe_name(meta[1]) + for meta in generator.ctx.var_member_metadata_by_node.values() + if meta[0].name == "x" + ] + assert members == ["x", "x__blk3"] + for member in members: + assert member in generator._series_var_member_names + assert re.search(rf"^ Series {member};$", cpp, re.M) + for output, member in zip(("up", "down"), members): + assignment = [ + line for line in cpp.splitlines() if re.match(rf"\s+{output} = ", line) + ][-1] + assert _has_identifier(assignment, member) + _scalar_member(cpp, "x__blk1") + _scalar_member(cpp, "x__blk2") + compile_cpp(cpp, label="sibling-later-name-and-history") + + +def test_analyzer_only_storage_uses_collision_safe_cpp_escaping() -> None: + source = re.sub( + r"\bx\b", "_sec7", + _BLOCK_MEMBER.replace("x__blk1", "pf_safe__sec7__blk1"), + ) + generator, cpp = _emit(source) + members = [ + meta[1] for meta in generator.ctx.var_member_metadata_by_node.values() + if meta[0].name == "_sec7" + ] + assert members == ["_sec7", "_sec7__blk1"] + safe = generator._safe_name(members[1]) + assert safe == "pf_safe__sec7__blk1_2" + for member in ["pf_safe__sec7__blk1", members[0], safe]: + _scalar_member(cpp, member) + assert f"up = {members[0]};" in cpp + assert f"down = {safe};" in cpp + compile_cpp(cpp, label="analyzer-storage-escaping") + + +def test_callable_block_storage_keeps_parameters_and_clones_distinct() -> None: + generator, cpp = _emit("""//@version=6 +strategy("callable block member spelling") +f(float x__blk1) => + float result = x__blk1 + if close > open + var float x = 0.0 + x += x__blk1 + result += x + if close <= open + var float x = 0.0 + x += x__blk1 * 2.0 + result += x + result +a = f(close) +b = f(open) +plot(a + b) +""") + assert generator.ctx.block_var_renames + for index in (0, 1): + remap = generator._func_cs_var_remap[("f", index)] + members = [remap[name] for name in ("x", "x__blk2")] + assert len(set(members)) == 2 + body = _method_body(cpp, f"f_cs{index}") + assert "double x__blk1" in cpp + assert _has_identifier(body, "x__blk1") + for member in members: + _scalar_member(cpp, member) + assert _has_identifier(body, member) + compile_cpp(cpp, label="callable-block-parameter-and-clones") + + +def test_fixed_fixnan_instances_retain_their_helper_namespace() -> None: + generator, cpp = _emit(_FRESH_FIXNAN) + fresh = generator._fresh_fixnan_members + assert fresh + for site, member in fresh: + assert member.startswith(f"{site.member_name}__ni") + assert not member.startswith("pf_safe_") + _scalar_member(cpp, member) + assert any( + _has_identifier(_method_body(cpp, instance["name"]), member) + for instance in generator._fresh_instances + ) + compile_cpp(cpp, label="fixed-fixnan-instance-namespace") + + @pytest.fixture(scope="module") def clone_reference(): reference = reference_codegen("4e51f6d00e3a015194d93b06e51ad90f2404cc14") if reference is None: - pytest.skip("The clone spelling reference commit is unavailable") + unavailable = (pytest.fail if os.environ.get("PINEFORGE_REQUIRE_NATIVE") == "1" + else pytest.skip) + unavailable("MISSING INPUT: the clone spelling reference commit is unavailable") return reference @pytest.mark.parametrize("source, original", [ (_VAR_CLONE, "acc_cs1"), (_METHOD_CLONE, "f_cs1"), + (_BLOCK_MEMBER, "x__blk1"), + (_RESERVED_CALLABLE, "_sec7"), + (_FRESH_FIXNAN, None), ]) def test_noncolliding_controls_keep_complete_reference_cpp( source, original, clone_reference, tmp_path ) -> None: - source = source.replace(original, "aa") + if original is not None: + source = source.replace(original, "aa") pine = tmp_path / "strategy.pine" pine.write_text(source, encoding="utf-8") reference = transpile_json(pine, root=clone_reference) assert reference["ok"], reference - assert transpile(source) == reference["cpp"] + assert transpile(source, check_support=True, libraries={}) == reference["cpp"] From 95e5924d9bba13c1cd6c2b644ecbe92be8a08853 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 19:48:26 +0800 Subject: [PATCH 18/27] Preserve sizing string values across expressions and host settings --- CHANGELOG.md | 4 + pineforge_codegen/analyzer/base.py | 8 +- pineforge_codegen/analyzer/types.py | 26 ++- pineforge_codegen/codegen/emit_top.py | 16 +- pineforge_codegen/codegen/order_shapes.py | 3 +- .../codegen/strategy_declaration.py | 8 +- pineforge_codegen/codegen/types.py | 22 ++ pineforge_codegen/codegen/visit_call.py | 47 +++- pineforge_codegen/codegen/visit_expr.py | 10 +- pineforge_codegen/codegen/visit_stmt.py | 2 +- pineforge_codegen/signatures.py | 113 ++++++++-- pineforge_codegen/symbols.py | 26 +++ .../test_namespace_operator_semantic_facts.py | 25 ++ tests/test_namespace_sizing_consumers.py | 213 ++++++++++++++++++ 14 files changed, 465 insertions(+), 58 deletions(-) create mode 100644 tests/test_namespace_operator_semantic_facts.py create mode 100644 tests/test_namespace_sizing_consumers.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 41136894..8e64c52b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,10 @@ supported as exact pairs; on the 0.x line they are independent. See the ## Unreleased (next release) +- Strategy sizing constants retain their string values in expressions, aliases + and helper calls. Host order and risk settings convert these values to their + numeric representation only at the setting boundary. + - `timeframe.in_seconds` and `timeframe.change` resolve an evaluated empty string to the chart period, including input, variable, function-return and named arguments. The argument is evaluated once, and nonempty strings keep diff --git a/pineforge_codegen/analyzer/base.py b/pineforge_codegen/analyzer/base.py index ea0b88e8..19c8a1b8 100644 --- a/pineforge_codegen/analyzer/base.py +++ b/pineforge_codegen/analyzer/base.py @@ -27,6 +27,7 @@ SymbolTable, TypeSpec, method_receiver_type_name, + public_type_name, unify_pine_types, ) from ..errors import SourceLocation, Diagnostic, CompileError, Level, Phase @@ -7479,7 +7480,7 @@ def _check_timeframe_string_arguments( # call supplies a type. Bare na adopts the string signature. continue function_name = intrinsic.name - argument_type = method_receiver_type_name(actual) or str(actual) + argument_type = public_type_name(actual) or str(actual) parameter_type = parameter.pine_type.value name = parameter.name from ..pine_spelling import expr_start @@ -8990,6 +8991,8 @@ def _visit_MemberAccess(self, node: MemberAccess) -> PineType: if drawing_all_kind(node) not in {None, "polyline"}: # Collections use TypeSpec for their shape; PineType stays scalar. return PineType.FLOAT + if self._builtin_sizing_constant(node) is not None: + return PineType.STRING # Handle specific namespaces if isinstance(node.object, Identifier): ns = node.object.name @@ -8999,9 +9002,6 @@ def _visit_MemberAccess(self, node: MemberAccess) -> PineType: # Direction constants if node.member in ("long", "short"): return PineType.INT - # Qty type constants - if node.member in ("percent_of_equity", "fixed", "cash"): - return PineType.INT # Commission type constants if node.member in ("commission",): return PineType.VOID # namespace prefix for .percent etc. diff --git a/pineforge_codegen/analyzer/types.py b/pineforge_codegen/analyzer/types.py index a8fa4568..8fe25194 100644 --- a/pineforge_codegen/analyzer/types.py +++ b/pineforge_codegen/analyzer/types.py @@ -91,6 +91,26 @@ class TypeHelper: docstring above. """ + def _builtin_sizing_constant(self, value): + if not (isinstance(value, MemberAccess) + and isinstance(value.object, Identifier)): + return None + receiver = value.object + constant = sigs.get_sizing_constant(receiver.name, value.member) + if constant is None: + return None + parameters = getattr(self, "_parameter_types", None) + bindings = self._value_bindings.declaration_bindings + if ((self._pine_version == 6 and id(receiver) not in bindings) + or self._value_bindings.binding_of(receiver) is not None + or (self._pine_version != 6 + and self._symbols.resolve(receiver.name) is not None) + or (parameters is not None and receiver.name in parameters) + or receiver.name in self._udt_fields + or receiver.name in self._enum_defs): + return None + return constant + def _split_top_level_type_args(self, text: str) -> list[str]: args: list[str] = [] cur: list[str] = [] @@ -807,6 +827,8 @@ def direct_user_udt_ctor_name(node: ASTNode) -> str | None: owner = self._type_spec_from_expr(value.object) if owner is not None and owner.kind == "udt" and owner.name: return (self._udt_field_type_specs.get(owner.name) or {}).get(value.member) + if self._builtin_sizing_constant(value) is not None: + return TypeSpec.primitive("string") return None @staticmethod @@ -1561,9 +1583,7 @@ def _type_spec_from_expr(self, value, **kwargs): or receiver.name not in self._parameter_types) and receiver.name not in self._udt_fields and receiver.name not in self._enum_defs): - spec = self._pine_type_to_spec( - sigs.get_variable_type(receiver.name, value.member), - ) + spec = sigs.get_variable_type_spec(receiver.name, value.member) if spec is not None: return spec return super()._type_spec_from_expr(value, **kwargs) diff --git a/pineforge_codegen/codegen/emit_top.py b/pineforge_codegen/codegen/emit_top.py index d9c67b33..2f2198c5 100644 --- a/pineforge_codegen/codegen/emit_top.py +++ b/pineforge_codegen/codegen/emit_top.py @@ -87,6 +87,7 @@ import re +from ..signatures import STRATEGY_SIZING_CONSTANTS from .run_stops import RUN_STOP_SHIMS_CPP, TIMEZONE_STOP_SHIM_CPP from ..ast_nodes import ( @@ -1353,9 +1354,8 @@ def declared_or_v6_default(key: str): # default_qty_type: strategy.fixed / strategy.percent_of_equity / strategy.cash qty_type_map = { - "strategy.fixed": "QtyType::FIXED", - "strategy.percent_of_equity": "QtyType::PERCENT_OF_EQUITY", - "strategy.cash": "QtyType::CASH", + name: f"QtyType::{item.host_name}" + for name, item in STRATEGY_SIZING_CONSTANTS.items() } qty_type = declared_or_v6_default("default_qty_type") if qty_type in qty_type_map: @@ -1446,9 +1446,13 @@ def declared_or_v6_default(key: str): lines.append(' } else if (key == "close_entries_rule") {') lines.append(' overrides.close_entries_rule = (value == "ANY" || value == "any" || value == "1");') lines.append(' } else if (key == "default_qty_type") {') - lines.append(' if (value == "fixed" || value == "strategy.fixed" || value == "0") overrides.default_qty_type = static_cast(QtyType::FIXED);') - lines.append(' else if (value == "percent_of_equity" || value == "strategy.percent_of_equity" || value == "1") overrides.default_qty_type = static_cast(QtyType::PERCENT_OF_EQUITY);') - lines.append(' else if (value == "cash" || value == "strategy.cash" || value == "2") overrides.default_qty_type = static_cast(QtyType::CASH);') + for index, (name, item) in enumerate(STRATEGY_SIZING_CONSTANTS.items()): + branch = "if" if index == 0 else "else if" + lines.append( + f' {branch} (value == "{item.value}" || value == "{name}" ' + f'|| value == "{item.host_value}") overrides.default_qty_type = ' + f'static_cast(QtyType::{item.host_name});' + ) lines.append(" else return;") lines.append(' } else if (key == "commission_type") {') lines.append(' if (value == "percent" || value == "strategy.commission.percent" || value == "0") overrides.commission_type = static_cast(CommissionType::PERCENT);') diff --git a/pineforge_codegen/codegen/order_shapes.py b/pineforge_codegen/codegen/order_shapes.py index 86d426c8..f8230fc3 100644 --- a/pineforge_codegen/codegen/order_shapes.py +++ b/pineforge_codegen/codegen/order_shapes.py @@ -12,6 +12,7 @@ TupleAssign, UnaryOp, VarDecl, WhileStmt, ) from ..limits import iter_ast_nodes, syntax_children +from ..signatures import STRATEGY_SIZING_CONSTANTS from .capabilities import READ_ONLY_STRATEGY_CALLS, _expression from .host_members import HOST_MEMBER_NAMES @@ -23,7 +24,7 @@ FINITE_BAR_VALUES = frozenset(("open", "high", "low", "close", "hl2", "hlc3", "ohlc4", "hlcc4")) ENUM_VALUES = { "strategy.oca.none": 0, "strategy.oca.cancel": 1, "strategy.oca.reduce": 2, - "strategy.fixed": 0, "strategy.percent_of_equity": 1, "strategy.cash": 2, + **{name: item.host_value for name, item in STRATEGY_SIZING_CONSTANTS.items()}, } SETTING_DEFAULTS = { "initial_capital": 10000.0, "default_qty_type": "fixed", "default_qty_value": 1.0, diff --git a/pineforge_codegen/codegen/strategy_declaration.py b/pineforge_codegen/codegen/strategy_declaration.py index 5efe9c8f..a0731458 100644 --- a/pineforge_codegen/codegen/strategy_declaration.py +++ b/pineforge_codegen/codegen/strategy_declaration.py @@ -5,7 +5,7 @@ from ..ast_nodes import EnumDecl, FuncDef, Identifier, StrategyDecl, StringLiteral, TypeDecl, VarDecl from ..errors import CompileError, Diagnostic, Level, Phase, SourceLocation from ..limits import iter_ast_nodes -from ..signatures import MATH_CONSTANTS, STRATEGY_PARAMETERS +from ..signatures import MATH_CONSTANTS, STRATEGY_PARAMETERS, STRATEGY_SIZING_CONSTANTS from .constant_fold import NON_CONSTANT, NamedConstant, fold_constant_expression from .tables import DAYOFWEEK_MAP, NAME_ECHO_STRING_MEMBERS @@ -16,12 +16,16 @@ _NAMED_CONSTANTS = { f"strategy.{name}": NamedConstant(f"strategy.{name}", name.rsplit(".", 1)[-1]) for name in ( - "fixed", "cash", "percent_of_equity", "commission.percent", + "commission.percent", "commission.cash_per_order", "commission.cash_per_contract", "long", "short", "oca.none", "oca.cancel", "oca.reduce", "direction.all", "direction.long", "direction.short", ) } +_NAMED_CONSTANTS.update({ + name: NamedConstant(name, item.value) + for name, item in STRATEGY_SIZING_CONSTANTS.items() +}) _NAMED_CONSTANTS.update({ f"{namespace}.{member}": NamedConstant(f"{namespace}.{member}", member) for namespace in ("currency", "format") diff --git a/pineforge_codegen/codegen/types.py b/pineforge_codegen/codegen/types.py index abc3fb2b..bb3099d1 100644 --- a/pineforge_codegen/codegen/types.py +++ b/pineforge_codegen/codegen/types.py @@ -3061,6 +3061,8 @@ def _type_spec_from_expr_raw( plan = getattr(self, "_array_widths", None) return (plan.widen(spec, {("field", owner.name, node.member)}) if plan is not None else spec) + if self._builtin_sizing_constant(node) is not None: + return TypeSpec.primitive("string") return None if isinstance(node, FuncCall): func_name, namespace = self._resolve_callee(node.callee) @@ -6138,6 +6140,26 @@ def _infer_cpp_type_for_security_elem(self, node) -> str: return "std::vector" return "double" + def _builtin_sizing_constant(self, node): + """Resolve registered constants only through an unshadowed namespace.""" + if not (isinstance(node, MemberAccess) + and isinstance(node.object, Identifier)): + return None + receiver = node.object + name = receiver.name + constant = sigs.get_sizing_constant(name, node.member) + if constant is None: + return None + if (self.ctx.identifier_decl_bindings.get(id(receiver)) is not None + or ValueBindings.declaration_of(receiver) is not None + or name in self._current_func_param_types + or name in self._current_func_locals + or name in self._current_loop_vars + or name in self._udt_defs + or name in self._enum_defs): + return None + return constant + def _text_type(self, node) -> str: """Pine bool provenance is consumed only by explicit text conversion.""" if not self._pine_v5_body: diff --git a/pineforge_codegen/codegen/visit_call.py b/pineforge_codegen/codegen/visit_call.py index 6bb0d804..909efdad 100644 --- a/pineforge_codegen/codegen/visit_call.py +++ b/pineforge_codegen/codegen/visit_call.py @@ -158,7 +158,13 @@ from ..intrinsic_binding import bind_intrinsic_arguments, validate_intrinsic_keywords from ..limits import iter_ast_nodes from ..security_contexts import ticker_symbol_arg -from ..symbols import PineType, TypeSpec, method_receiver_type_name +from ..symbols import ( + PineType, + TypeSpec, + method_receiver_type_name, + public_cpp_type_name, + public_type_name, +) from ..analyzer.value_bindings import ValueBindings from ..method_binding import ( MethodBindError, @@ -3238,11 +3244,11 @@ def timeframe_call(tf_arg: str) -> str: and isinstance(argument.object, Identifier) and self.ctx.identifier_decl_bindings.get(id(argument.object)) is None and ValueBindings.declaration_of(argument.object) is None): - variable_type = sigs.get_variable_type( + variable_spec = sigs.get_variable_type_spec( argument.object.name, argument.member, ) - if variable_type != PineType.UNKNOWN: - actual = TypeSpec.primitive(variable_type.value) + if variable_spec is not None: + actual = variable_spec actual_cpp_type = self._text_type(argument) argument_is_string = ( actual == TypeSpec.primitive("string") @@ -3254,12 +3260,9 @@ def timeframe_call(tf_arg: str) -> str: # analysis. Check before bool-to-text compatibility can # turn a non-string value into a C++ string. argument_type = ( - "bool" if actual_cpp_type == "bool" else - (method_receiver_type_name(actual) or str(actual)) - if actual is not None else - {"int": "int", "int64_t": "int", "double": "float"}.get( - actual_cpp_type, actual_cpp_type, - ) + public_type_name(actual) + if actual is not None + else public_cpp_type_name(actual_cpp_type) ) intrinsic = sigs.BUILTIN_FUNCTIONS[f"{namespace}.{func_name}"] parameter = intrinsic.primary.params[0] @@ -3820,6 +3823,28 @@ def _strategy_string_arg(self, node) -> str: """A missing Pine string uses the host's empty-string sentinel.""" return '""' if node is None or isinstance(node, NaLiteral) else self._visit_expr(node) + def _strategy_qty_type_arg(self, node) -> str: + """Convert a Pine sizing string only where the host expects its code.""" + if node is None: + return "-1" + constant = self._builtin_sizing_constant(node) + if constant is not None: + return str(constant.host_value) + value = self._visit_expr(node) + if self._infer_type(node) != "std::string": + # Retain the legacy numeric host API, including its -1 sentinel. + return value + cases = " ".join( + f"if (_pf_qty_type == {self._visit_expr(StringLiteral(value=item.value))}) " + f"return {item.host_value};" + for item in sigs.STRATEGY_SIZING_CONSTANTS.values() + ) + return ( + "([&]() -> int { const std::string _pf_qty_type = " + f"({value}); {cases} " + 'throw std::invalid_argument("Unknown strategy sizing value"); }())' + ) + def _visit_strategy_call(self, func_name: str, node: FuncCall) -> str: from .order_shapes import record_order_call if func_name in ("convert_to_account", "convert_to_symbol"): @@ -3855,7 +3880,7 @@ def _visit_strategy_call(self, func_name: str, node: FuncCall) -> str: comment_val = self._strategy_string_arg(comment) oca_name_val = self._strategy_string_arg(oca_name) oca_type_val = self._visit_expr(oca_type) if oca_type else "0" - qty_type_val = self._visit_expr(qty_type) if qty_type else "-1" + qty_type_val = self._strategy_qty_type_arg(qty_type) qty_val = self._visit_float_arg(qty) if stop is not None or limit is not None or qty is not None or oca_name is not None or oca_type is not None or qty_type is not None: limit_val = self._visit_float_arg(limit) diff --git a/pineforge_codegen/codegen/visit_expr.py b/pineforge_codegen/codegen/visit_expr.py index da1f99a1..3b0e3e04 100644 --- a/pineforge_codegen/codegen/visit_expr.py +++ b/pineforge_codegen/codegen/visit_expr.py @@ -997,6 +997,9 @@ def _visit_member_access(self, node: MemberAccess) -> str: # call reaches the array it holds. return f"(*{field})" return field + sizing = self._builtin_sizing_constant(node) + if sizing is not None: + return self._visit_expr(StringLiteral(value=sizing.value)) if isinstance(node.object, Identifier): ns = node.object.name if ns == "alert" and node.member in ALERT_FREQ_VALUES: @@ -1085,13 +1088,6 @@ def _visit_member_access(self, node: MemberAccess) -> str: return "avg_losing_trade_percent()" if node.member == "margin_liquidation_price": return "margin_liquidation_price()" - # Qty type / commission constants - if node.member == "fixed": - return "0" - if node.member == "percent_of_equity": - return "1" - if node.member == "cash": - return "2" if node.member == "account_currency": return self._strategy_account_currency_expr() # Sub-namespaces (oca, direction, risk, commission) handled below diff --git a/pineforge_codegen/codegen/visit_stmt.py b/pineforge_codegen/codegen/visit_stmt.py index ffdb77e0..352ff880 100644 --- a/pineforge_codegen/codegen/visit_stmt.py +++ b/pineforge_codegen/codegen/visit_stmt.py @@ -493,7 +493,7 @@ def _visit_stmt(self, node: ASTNode, lines: list[str], indent: int) -> None: if is_pct is None: # Mutable aliases and computed enums must use # the value at this call, not an initializer. - type_value = self._visit_expr(type_arg) + type_value = self._strategy_qty_type_arg(type_arg) percent_flag = ( f"(({type_value}) == " "static_cast(QtyType::PERCENT_OF_EQUITY))" diff --git a/pineforge_codegen/signatures.py b/pineforge_codegen/signatures.py index 6ab3522f..0f4483d5 100644 --- a/pineforge_codegen/signatures.py +++ b/pineforge_codegen/signatures.py @@ -14,7 +14,7 @@ from dataclasses import dataclass, field from typing import Any -from .symbols import PineType +from .symbols import PineType, TypeSpec @dataclass @@ -694,6 +694,39 @@ def get_value_function(name: str) -> IntrinsicFunc | None: # Built-in variables (not functions) # ============================================================================ +@dataclass(frozen=True) +class _StorageOnlyType: + """Legacy host representation without a proven scalar Pine type.""" + + storage_type: PineType + + +@dataclass(frozen=True) +class SizingConstant: + """A Pine string value and its conversion at a host sizing boundary.""" + + value: str + host_value: int + host_name: str + + +# The complete strategy sizing constant class. Its numeric codes belong only +# to the host API; an ordinary expression reads the string value. +STRATEGY_SIZING_CONSTANTS = { + "strategy.fixed": SizingConstant("fixed", 0, "FIXED"), + "strategy.percent_of_equity": SizingConstant("percent_of_equity", 1, "PERCENT_OF_EQUITY"), + "strategy.cash": SizingConstant("cash", 2, "CASH"), +} + + +def _variable_storage_type(value: PineType | _StorageOnlyType | SizingConstant) -> PineType: + if isinstance(value, _StorageOnlyType): + return value.storage_type + if isinstance(value, SizingConstant): + return PineType.STRING + return value + + BUILTIN_VARIABLES: dict[str, PineType] = { # Price series "open": F, "high": F, "low": F, "close": F, "volume": F, @@ -735,7 +768,7 @@ def get_value_function(name: str) -> IntrinsicFunc | None: ) # Strategy variables (read-only properties, not function calls) -STRATEGY_VARIABLES: dict[str, PineType] = { +_STRATEGY_VARIABLE_DECLARATIONS: dict[str, PineType | _StorageOnlyType | SizingConstant] = { "strategy.position_size": F, "strategy.position_avg_price": F, "strategy.position_entry_name": S, @@ -771,25 +804,27 @@ def get_value_function(name: str) -> IntrinsicFunc | None: "strategy.account_currency": S, "strategy.opentrades.capital_held": F, # Direction constants - "strategy.long": B, - "strategy.short": B, + "strategy.long": _StorageOnlyType(B), + "strategy.short": _StorageOnlyType(B), # Qty type constants - "strategy.fixed": I, - "strategy.cash": I, - "strategy.percent_of_equity": I, + **STRATEGY_SIZING_CONSTANTS, # Commission type constants - "strategy.commission.percent": I, - "strategy.commission.cash_per_contract": I, - "strategy.commission.cash_per_order": I, + "strategy.commission.percent": _StorageOnlyType(I), + "strategy.commission.cash_per_contract": _StorageOnlyType(I), + "strategy.commission.cash_per_order": _StorageOnlyType(I), # OCA constants - "strategy.oca.cancel": I, - "strategy.oca.reduce": I, - "strategy.oca.none": I, + "strategy.oca.cancel": _StorageOnlyType(I), + "strategy.oca.reduce": _StorageOnlyType(I), + "strategy.oca.none": _StorageOnlyType(I), # Direction constants "strategy.direction.all": S, "strategy.direction.long": S, "strategy.direction.short": S, } +STRATEGY_VARIABLES: dict[str, PineType] = { + name: _variable_storage_type(value) + for name, value in _STRATEGY_VARIABLE_DECLARATIONS.items() +} # Barstate variables BARSTATE_VARIABLES: dict[str, PineType] = { @@ -866,13 +901,17 @@ def get_value_function(name: str) -> IntrinsicFunc | None: } # display.* plot_display constants (PineScript v6 — values for C++ codegen) +_DISPLAY_VARIABLE_DECLARATIONS: dict[str, _StorageOnlyType] = { + "display.all": _StorageOnlyType(I), + "display.none": _StorageOnlyType(I), + "display.pane": _StorageOnlyType(I), + "display.data_window": _StorageOnlyType(I), + "display.status_line": _StorageOnlyType(I), + "display.price_scale": _StorageOnlyType(I), +} DISPLAY_VARIABLES: dict[str, PineType] = { - "display.all": I, - "display.none": I, - "display.pane": I, - "display.data_window": I, - "display.status_line": I, - "display.price_scale": I, + name: value.storage_type + for name, value in _DISPLAY_VARIABLE_DECLARATIONS.items() } @@ -1040,20 +1079,48 @@ def is_intrinsic_function(namespace: str | None, func_name: str) -> bool: return False -def get_variable_type(namespace: str | None, name: str) -> PineType: - """Return a builtin variable's declared type, or UNKNOWN for other names.""" +def _variable_declaration( + namespace: str | None, name: str, +) -> PineType | _StorageOnlyType | SizingConstant: if namespace is None: return BUILTIN_VARIABLES.get(name, PineType.UNKNOWN) full = f"{namespace}.{name}" for table in ( - STRATEGY_VARIABLES, BARSTATE_VARIABLES, SYMINFO_VARIABLES, - TIMEFRAME_VARIABLES, BUILTIN_VARIABLES, DISPLAY_VARIABLES, + _STRATEGY_VARIABLE_DECLARATIONS, BARSTATE_VARIABLES, SYMINFO_VARIABLES, + TIMEFRAME_VARIABLES, BUILTIN_VARIABLES, _DISPLAY_VARIABLE_DECLARATIONS, ): if full in table: return table[full] return PineType.UNKNOWN +def get_variable_type(namespace: str | None, name: str) -> PineType: + """Return a builtin's legacy storage type, not an operator-domain proof.""" + return _variable_storage_type(_variable_declaration(namespace, name)) + + +def get_sizing_constant(namespace: str | None, name: str) -> SizingConstant | None: + """Return a sizing constant's Pine value and host conversion together.""" + return STRATEGY_SIZING_CONSTANTS.get(f"{namespace}.{name}") + + +def get_variable_type_spec( + namespace: str | None, name: str, +) -> TypeSpec | None: + """Return a builtin variable's semantic Pine type specification. + + The variable tables also serve storage and lowering consumers, so this + lookup carries only the semantic facts that are safe for operator proofs. + """ + declaration = _variable_declaration(namespace, name) + if isinstance(declaration, _StorageOnlyType): + return None + primitive = _variable_storage_type(declaration) + if primitive in (PineType.UNKNOWN, PineType.VOID): + return None + return TypeSpec.primitive(primitive.value) + + def is_intrinsic_variable(namespace: str | None, name: str) -> bool: """Check if a name is a known TradingView built-in variable.""" return get_variable_type(namespace, name) != PineType.UNKNOWN diff --git a/pineforge_codegen/symbols.py b/pineforge_codegen/symbols.py index 71ead7fd..91eb86a0 100644 --- a/pineforge_codegen/symbols.py +++ b/pineforge_codegen/symbols.py @@ -123,6 +123,32 @@ def method_receiver_type_name(spec: TypeSpec | None) -> str | None: return None +def public_type_name(spec: TypeSpec | None) -> str | None: + """Return a Pine type name suitable for a public diagnostic. + + An unresolved numeric domain retains its uncertainty; it is not evidence + for either concrete Pine scalar type. Structured specs keep their spelling. + """ + if spec is None: + return None + name = method_receiver_type_name(spec) + if name is not None: + return name + return str(spec) + + +def public_cpp_type_name(cpp_type: str) -> str: + """Map the scalar C++ fallbacks used by codegen to Pine names.""" + return { + "bool": "bool", + "int": "int", + "int64_t": "int", + "float": "float", + "double": "float", + "std::string": "string", + }.get(cpp_type, "unknown") + + def method_receiver_cpp_token( spec: TypeSpec | None, fallback_name: str | None = None, diff --git a/tests/test_namespace_operator_semantic_facts.py b/tests/test_namespace_operator_semantic_facts.py new file mode 100644 index 00000000..9a95b520 --- /dev/null +++ b/tests/test_namespace_operator_semantic_facts.py @@ -0,0 +1,25 @@ +"""Valid operator operands must survive legacy namespace storage types.""" + +from pineforge_codegen import transpile + + +SIZING_LABEL_SOURCE = '''//@version=6 +strategy("Sizing label") +txt = "Sizing: " + strategy.cash +if barstate.islast + label.new(bar_index, high, txt) +''' + +ORDINARY_CONTROL_SOURCE = '''//@version=6 +strategy("Ordinary addition") +plot(close + open) +''' + + +def test_strategy_sizing_string_constant_still_transpiles(): + # Acceptance only: the old numeric emission is a separate value defect. + transpile(SIZING_LABEL_SOURCE) + + +def test_ordinary_numeric_operator_still_transpiles(): + transpile(ORDINARY_CONTROL_SOURCE) diff --git a/tests/test_namespace_sizing_consumers.py b/tests/test_namespace_sizing_consumers.py new file mode 100644 index 00000000..dc1dd15c --- /dev/null +++ b/tests/test_namespace_sizing_consumers.py @@ -0,0 +1,213 @@ +"""Sizing constants keep their Pine values through live emitted consumers.""" + +import re + +import pytest + +from pineforge_codegen import signatures as sigs, transpile +from pineforge_codegen.analyzer import Analyzer +from pineforge_codegen.errors import CompileError +from pineforge_codegen.lexer import Lexer +from pineforge_codegen.parser import Parser +from pineforge_codegen.symbols import PineType, TypeSpec, public_type_name + + +SIZING_MEMBERS = ("fixed", "cash", "percent_of_equity") +HEADER = '//@version=6\nstrategy("Sizing consumers")\n' + + +def _source_body(cpp): + match = re.search( + r"void on_source_bar\([^)]*\) override \{(.*?)\n \}", + cpp, re.DOTALL, + ) + assert match is not None + return match.group(1) + + +def _engine_call(function, argument): + if function == "change": + return ( + "tf_change(prev_bar_timestamp_, current_bar_.timestamp, " + f"{argument}, syminfo_.timezone, syminfo_.session)" + ) + return f"tf_to_seconds({argument})" + + +def _live_timeframe_source(member, function, form): + argument = f"strategy.{member}" + declarations = "" + supplied = f"timeframe = {argument}" if form == "named" else argument + call = f"timeframe.{function}({supplied})" + if form in ("helper", "named-helper"): + supplied = "timeframe = frameArg" if form == "named-helper" else "frameArg" + declarations = f"withFrame(frameArg) => timeframe.{function}({supplied})\n" + call = f"withFrame({argument})" + elif form == "alias": + declarations = f"frameMode = {argument}\n" + call = f"timeframe.{function}(frameMode)" + elif form == "return": + declarations = f"chosenFrame() => {argument}\n" + call = f"timeframe.{function}(chosenFrame())" + value = f"{call} ? 1 : 0" if function == "change" else call + return ( + HEADER + declarations + f"sampleValue = {value}\n" + "if sampleValue > 0\n" + ' strategy.entry("L", strategy.long)\n' + ) + + +@pytest.mark.parametrize("member", SIZING_MEMBERS) +@pytest.mark.parametrize("function", ("change", "in_seconds")) +@pytest.mark.parametrize( + "form", ("direct", "named", "helper", "named-helper", "alias", "return"), +) +def test_sizing_timeframe_arguments_reach_live_string_lowering(member, function, form): + cpp = transpile(_live_timeframe_source(member, function, form)) + body = _source_body(cpp) + assert re.search(r"\bsampleValue\s*=", body) + assert re.search(r"if \([^;]*\bsampleValue\b", body) + assert 'strategy_entry(std::string("L"), true' in body + literal = f'std::string("{member}")' + if form in ("helper", "named-helper"): + assert f"withFrame({literal})" in body + assert re.search(r"\bwithFrame\(std::string frameArg\)", cpp) + assert _engine_call(function, "frameArg") in cpp + assert "(frameArg).empty()" in cpp + elif form == "alias": + assert f"frameMode = {literal};" in body + assert _engine_call(function, "frameMode") in body + assert "(frameMode).empty()" in body + elif form == "return": + assert re.search(r"\bstd::string chosenFrame\(", cpp) + assert f"return {literal};" in cpp + assert "const std::string _pf_timeframe_arg = (chosenFrame());" in body + assert _engine_call( + function, "(_pf_timeframe_arg.empty() ? script_tf_ : _pf_timeframe_arg)", + ) in body + else: + assert _engine_call(function, literal) in body + assert f"({literal}).empty()" in body + assert not re.search(r"\(\s*(?:[+-]?\d+(?:\.\d*)?|true|false)\s*\)\.empty\(", cpp) + + +@pytest.mark.parametrize("member", SIZING_MEMBERS) +@pytest.mark.parametrize("consumer", ("concat", "equality", "selection", "collection")) +def test_sizing_strings_keep_their_type_in_other_live_consumers(member, consumer): + if consumer == "concat": + declaration = f'sampleName = "Sizing: " + strategy.{member}\n' + elif consumer == "equality": + declaration = ( + f'rightMode = input.string("{member}")\n' + f'sampleName = strategy.{member} == rightMode ? "equal" : "different"\n' + ) + elif consumer == "selection": + declaration = ( + "chooseMode = input.bool(true)\n" + f'sampleName = chooseMode ? strategy.{member} : "other"\n' + ) + else: + declaration = ( + f'modes = array.from(strategy.{member}, "other")\n' + "sampleName = array.get(modes, input.int(0))\n" + ) + cpp = transpile( + HEADER + declaration + "if barstate.islast\n" + " strategy.entry(sampleName, strategy.long)\n" + ) + body = _source_body(cpp) + assert f'std::string("{member}")' in body + assert "strategy_entry(sampleName, true" in body + assert re.search(r"\bstd::string sampleName\b", cpp) + if consumer == "collection": + assert "std::vector" in cpp + + +@pytest.mark.parametrize("member,host_value", (("fixed", 0), ("percent_of_equity", 1), ("cash", 2))) +def test_host_qty_boundary_preserves_codes_without_changing_pine_values(member, host_value): + cpp = transpile( + HEADER + f"modeName = strategy.{member}\n" + f'strategy.entry(modeName, strategy.long, qty=1, qty_type=strategy.{member})\n' + ) + body = _source_body(cpp) + assert f'modeName = std::string("{member}");' in body + assert f", {host_value})" in body + assert sigs.get_variable_type("strategy", member) == PineType.STRING + assert sigs.STRATEGY_VARIABLES[f"strategy.{member}"] == PineType.STRING + assert sigs.get_variable_type_spec("strategy", member) == TypeSpec.primitive("string") + + +@pytest.mark.parametrize("consumer", ("order", "risk")) +def test_computed_sizing_strings_convert_once_at_numeric_host_boundaries(consumer): + source = ( + HEADER + "chooseMode = input.bool(true)\n" + "chosenMode() => chooseMode ? strategy.cash : strategy.percent_of_equity\n" + ) + if consumer == "order": + source += 'strategy.entry("L", strategy.long, qty=1, qty_type=chosenMode())\n' + else: + source += ( + "strategy.risk.max_drawdown(10, chosenMode())\n" + 'strategy.entry("L", strategy.long)\n' + ) + body = _source_body(transpile(source)) + assert body.count("const std::string _pf_qty_type = (chosenMode());") == 1 + assert '_pf_qty_type == std::string("cash")' in body + assert '_pf_qty_type == std::string("percent_of_equity")' in body + assert "strategy_entry(" in body + if consumer == "risk": + assert "set_pine_risk_max_drawdown(" in body + + +@pytest.mark.parametrize("namespace,member", ( + ("strategy", "long"), ("strategy", "short"), + ("strategy.commission", "percent"), + ("strategy.commission", "cash_per_contract"), + ("strategy.commission", "cash_per_order"), + ("strategy.oca", "none"), ("strategy.oca", "cancel"), ("strategy.oca", "reduce"), + *(("display", member) for member in ( + "all", "none", "pane", "data_window", "status_line", "price_scale", + )), +)) +def test_legacy_constant_storage_is_not_a_semantic_operator_proof(namespace, member): + assert sigs.get_variable_type(namespace, member) != PineType.UNKNOWN + assert sigs.get_variable_type_spec(namespace, member) is None + + +@pytest.mark.parametrize("version", (5, 6)) +def test_authored_namespace_receiver_keeps_its_field_type(version): + cpp = transpile( + f'//@version={version}\nstrategy("Sizing receiver")\n' + "type Mode\n int cash\n" + "readMode(Mode strategy) => strategy.cash\n" + "sampleValue = readMode(Mode.new(3))\n" + "if sampleValue > 0\n" + ' strategy.entry("L", strategy.long)\n' + ) + body = _source_body(cpp) + assert "readMode(" in body + assert "strategy_entry(" in body + assert 'std::string("cash")' not in body + + +def test_ordinary_live_numeric_control_keeps_its_operands(): + body = _source_body(transpile( + HEADER + "sampleValue = close + open\n" + "if sampleValue > 0\n" + ' strategy.entry("L", strategy.long)\n' + )) + assert "(current_bar_.close + current_bar_.open)" in body + assert "sampleValue" in body + assert "strategy_entry(" in body + + +@pytest.mark.parametrize("function", ("change", "in_seconds")) +def test_open_numeric_domain_is_not_reported_as_a_guessed_float(function): + source = HEADER + f"value = timeframe.{function}(math.round(close))\n" + ast = Parser(Lexer(source).tokenize(), source=source).parse() + with pytest.raises(CompileError) as refused: + Analyzer(ast).analyze() + diagnostic, = refused.value.diagnostics + assert diagnostic.code == "PF-E2095" + assert diagnostic.args["argument_type"] == "number" + assert public_type_name(TypeSpec.primitive("number")) == "number" From 149cb9b83f614c2d9a4e63c60a677317d89de351 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 20:05:58 +0800 Subject: [PATCH 19/27] Exercise v5 receiver shadowing through an imported library --- tests/test_namespace_sizing_consumers.py | 30 +++++++++++++++++------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/tests/test_namespace_sizing_consumers.py b/tests/test_namespace_sizing_consumers.py index dc1dd15c..89f25603 100644 --- a/tests/test_namespace_sizing_consumers.py +++ b/tests/test_namespace_sizing_consumers.py @@ -176,14 +176,28 @@ def test_legacy_constant_storage_is_not_a_semantic_operator_proof(namespace, mem @pytest.mark.parametrize("version", (5, 6)) def test_authored_namespace_receiver_keeps_its_field_type(version): - cpp = transpile( - f'//@version={version}\nstrategy("Sizing receiver")\n' - "type Mode\n int cash\n" - "readMode(Mode strategy) => strategy.cash\n" - "sampleValue = readMode(Mode.new(3))\n" - "if sampleValue > 0\n" - ' strategy.entry("L", strategy.long)\n' - ) + if version == 5: + libraries = {"pftest/SizingReceiver/1": ( + '//@version=5\nlibrary("SizingReceiver")\n' + "export type Mode\n int cash\n" + "export readMode(Mode strategy) => strategy.cash\n" + )} + source = ( + HEADER + "import pftest/SizingReceiver/1 as L\n" + "sampleValue = L.readMode(L.Mode.new(3))\n" + "if sampleValue > 0\n" + ' strategy.entry("L", strategy.long)\n' + ) + cpp = transpile(source, libraries=libraries) + else: + cpp = transpile( + f'//@version={version}\nstrategy("Sizing receiver")\n' + "type Mode\n int cash\n" + "readMode(Mode strategy) => strategy.cash\n" + "sampleValue = readMode(Mode.new(3))\n" + "if sampleValue > 0\n" + ' strategy.entry("L", strategy.long)\n' + ) body = _source_body(cpp) assert "readMode(" in body assert "strategy_entry(" in body From 95b26d803c0f9f75ae2f530ddb619cdfbda5f38b Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 20:51:57 +0800 Subject: [PATCH 20/27] Preserve integer risk type boundary codes --- pineforge_codegen/codegen/visit_stmt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pineforge_codegen/codegen/visit_stmt.py b/pineforge_codegen/codegen/visit_stmt.py index 352ff880..ffdb77e0 100644 --- a/pineforge_codegen/codegen/visit_stmt.py +++ b/pineforge_codegen/codegen/visit_stmt.py @@ -493,7 +493,7 @@ def _visit_stmt(self, node: ASTNode, lines: list[str], indent: int) -> None: if is_pct is None: # Mutable aliases and computed enums must use # the value at this call, not an initializer. - type_value = self._strategy_qty_type_arg(type_arg) + type_value = self._visit_expr(type_arg) percent_flag = ( f"(({type_value}) == " "static_cast(QtyType::PERCENT_OF_EQUITY))" From 6e9d119014ef5232910343db5bcc8051ae9c50c9 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 21:32:40 +0800 Subject: [PATCH 21/27] Strengthen sizing regression and reserve generated name --- pineforge_codegen/codegen/helpers.py | 2 +- tests/test_e2e_temporary_name_hygiene.py | 4 ++++ tests/test_namespace_sizing_consumers.py | 22 ++++++++++++++++++---- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/pineforge_codegen/codegen/helpers.py b/pineforge_codegen/codegen/helpers.py index 40a79b20..d161d6ca 100644 --- a/pineforge_codegen/codegen/helpers.py +++ b/pineforge_codegen/codegen/helpers.py @@ -533,7 +533,7 @@ def cpp_code_only(cpp: str) -> str: _pf_series_raw _pf_raw _pf_val_t _pf_out _pf_prior _pf_hist_offset_raw _pf_hist_offset_numeric _pf_hist_offset _pf_hist_bar _pf_calendar_ts _pf_calendar_tz - _pf_timeframe_arg + _pf_timeframe_arg _pf_qty_type _tz _yr _mo _dy _hr _min _sc _res _old _old_tz _had_old _secs _last_tz _last_yr _last_mo _last_dy _last_hr _last_min _last_sc _last_res diff --git a/tests/test_e2e_temporary_name_hygiene.py b/tests/test_e2e_temporary_name_hygiene.py index 7f80f775..e019e8c8 100644 --- a/tests/test_e2e_temporary_name_hygiene.py +++ b/tests/test_e2e_temporary_name_hygiene.py @@ -125,6 +125,10 @@ def test_every_temporary_the_emitter_declares_is_reserved(): 'changedValue = timeframe.change(timeframe = chosenFrame())\n' 'plot(secondsValue)\nplot(changedValue ? 1 : 0)\n' ) + sources.append( + HEAD + 'qtyMode = input.string("cash", options = ["cash", "percent_of_equity"])\n' + 'strategy.entry("L", strategy.long, qty = 1, qty_type = qtyMode)\n' + ) for source in sources: authored = {t.value for t in Lexer(source).tokenize() if t.type.name == "IDENT"} for line in transpile(source).splitlines(): diff --git a/tests/test_namespace_sizing_consumers.py b/tests/test_namespace_sizing_consumers.py index 89f25603..4d1e919e 100644 --- a/tests/test_namespace_sizing_consumers.py +++ b/tests/test_namespace_sizing_consumers.py @@ -6,10 +6,11 @@ from pineforge_codegen import signatures as sigs, transpile from pineforge_codegen.analyzer import Analyzer +from pineforge_codegen.codegen.helpers import CPP_TEMPORARY_NAMES from pineforge_codegen.errors import CompileError from pineforge_codegen.lexer import Lexer from pineforge_codegen.parser import Parser -from pineforge_codegen.symbols import PineType, TypeSpec, public_type_name +from pineforge_codegen.symbols import PineType, TypeSpec SIZING_MEMBERS = ("fixed", "cash", "percent_of_equity") @@ -185,6 +186,7 @@ def test_authored_namespace_receiver_keeps_its_field_type(version): source = ( HEADER + "import pftest/SizingReceiver/1 as L\n" "sampleValue = L.readMode(L.Mode.new(3))\n" + 'sizingText = "Sizing: " + strategy.cash\n' "if sampleValue > 0\n" ' strategy.entry("L", strategy.long)\n' ) @@ -195,13 +197,25 @@ def test_authored_namespace_receiver_keeps_its_field_type(version): "type Mode\n int cash\n" "readMode(Mode strategy) => strategy.cash\n" "sampleValue = readMode(Mode.new(3))\n" + 'sizingText = "Sizing: " + strategy.cash\n' "if sampleValue > 0\n" ' strategy.entry("L", strategy.long)\n' ) body = _source_body(cpp) - assert "readMode(" in body + assert ".cash" in cpp + assert re.search(r"sizingText\s*=.*cash", body) assert "strategy_entry(" in body - assert 'std::string("cash")' not in body + + +def test_computed_qty_type_temporary_is_reserved(): + source = ( + HEADER + 'qtyMode = input.string("cash", options = ["cash", "percent_of_equity"])\n' + 'strategy.entry("L", strategy.long, qty = 1, qty_type = qtyMode)\n' + ) + cpp = transpile(source) + assert "_pf_qty_type" in cpp + assert "_pf_qty_type" in CPP_TEMPORARY_NAMES def test_ordinary_live_numeric_control_keeps_its_operands(): @@ -224,4 +238,4 @@ def test_open_numeric_domain_is_not_reported_as_a_guessed_float(function): diagnostic, = refused.value.diagnostics assert diagnostic.code == "PF-E2095" assert diagnostic.args["argument_type"] == "number" - assert public_type_name(TypeSpec.primitive("number")) == "number" + assert str(TypeSpec.primitive("number")) == "number" From d9a643e6bcbc28cd23f29afc9c77642b65efd87e Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 21:52:03 +0800 Subject: [PATCH 22/27] Lower computed risk type arms at numeric boundary --- pineforge_codegen/codegen/visit_stmt.py | 2 +- tests/test_namespace_sizing_consumers.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pineforge_codegen/codegen/visit_stmt.py b/pineforge_codegen/codegen/visit_stmt.py index ffdb77e0..352ff880 100644 --- a/pineforge_codegen/codegen/visit_stmt.py +++ b/pineforge_codegen/codegen/visit_stmt.py @@ -493,7 +493,7 @@ def _visit_stmt(self, node: ASTNode, lines: list[str], indent: int) -> None: if is_pct is None: # Mutable aliases and computed enums must use # the value at this call, not an initializer. - type_value = self._visit_expr(type_arg) + type_value = self._strategy_qty_type_arg(type_arg) percent_flag = ( f"(({type_value}) == " "static_cast(QtyType::PERCENT_OF_EQUITY))" diff --git a/tests/test_namespace_sizing_consumers.py b/tests/test_namespace_sizing_consumers.py index 4d1e919e..58ddd3df 100644 --- a/tests/test_namespace_sizing_consumers.py +++ b/tests/test_namespace_sizing_consumers.py @@ -209,7 +209,7 @@ def test_authored_namespace_receiver_keeps_its_field_type(version): def test_computed_qty_type_temporary_is_reserved(): source = ( - HEADER + HEADER + 'qtyMode = input.string("cash", options = ["cash", "percent_of_equity"])\n' 'strategy.entry("L", strategy.long, qty = 1, qty_type = qtyMode)\n' ) From 57d0db6d762702dd0f441fc1531f9d3fbb3d77de Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 21:59:23 +0800 Subject: [PATCH 23/27] Convert sizing selection arms at host boundaries --- pineforge_codegen/codegen/visit_call.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pineforge_codegen/codegen/visit_call.py b/pineforge_codegen/codegen/visit_call.py index 909efdad..b1f75432 100644 --- a/pineforge_codegen/codegen/visit_call.py +++ b/pineforge_codegen/codegen/visit_call.py @@ -3830,6 +3830,12 @@ def _strategy_qty_type_arg(self, node) -> str: constant = self._builtin_sizing_constant(node) if constant is not None: return str(constant.host_value) + if isinstance(node, Ternary): + return ( + f"(({self._visit_expr(node.condition)}) ? " + f"({self._strategy_qty_type_arg(node.true_val)}) : " + f"({self._strategy_qty_type_arg(node.false_val)}))" + ) value = self._visit_expr(node) if self._infer_type(node) != "std::string": # Retain the legacy numeric host API, including its -1 sentinel. From 6c1ff4f6b4e47286c5e36830c4d94599554de2ad Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:17:41 +0800 Subject: [PATCH 24/27] fix(codegen): preserve established typed method names Keep raw typed-method names on the function channel when they collide with authored data members. Continue allocating encoded names and collisions with generated storage so method definitions, calls and state stay distinct. --- pineforge_codegen/codegen/emit_top.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/pineforge_codegen/codegen/emit_top.py b/pineforge_codegen/codegen/emit_top.py index c23911e4..e1c83673 100644 --- a/pineforge_codegen/codegen/emit_top.py +++ b/pineforge_codegen/codegen/emit_top.py @@ -2358,13 +2358,18 @@ def _emit_udt_method_cpp_name(self, fi: FuncInfo) -> str: allocated = {} for key, name in methods: encoded = cpp_identifier_fragment(name) - preferred = ( - name if encoded == name else - self._allocate_generated_cpp_name(encoded, used) - ) - allocated[key] = self._generated_class_name( - ("typed-method", key), preferred - ) + if encoded == name and name in self._all_member_names: + # Let the function channel separate an authored value + # binding from its established typed-method spelling. + allocated[key] = name + else: + preferred = ( + name if encoded == name else + self._allocate_generated_cpp_name(encoded, used) + ) + allocated[key] = self._generated_class_name( + ("typed-method", key), preferred + ) used.add(allocated[key]) self._escaped_method_cpp_names = allocated return self._func_safe_name(allocated[fi.name]) From a0afaa8ba09e03b9a304e8b15e5ae4fc4de2e3cb Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:37:47 +0800 Subject: [PATCH 25/27] Assert the risk type conversion for reassigned sizing variables (#394) Sizing constants keep their Pine string type for the string consumers this change fixes, so a reassigned sizing variable reaches the risk host boundary as a string and is converted to its integer code there. Keeping the integer comparison would need integer storage for these variables, which the string consumers cannot use. The test still requires the call to read t after its write, never its initializer, and now also checks the code chosen for the assigned value. --- tests/test_source_host_config_codegen.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/test_source_host_config_codegen.py b/tests/test_source_host_config_codegen.py index 7ea6a760..a1cd3a81 100644 --- a/tests/test_source_host_config_codegen.py +++ b/tests/test_source_host_config_codegen.py @@ -259,13 +259,26 @@ def test_risk_reassigned_type_reads_the_current_value( ] assert len(calls) == 1 # The flag must read t after its write, rather than use its initializer. + conversion = ( + r"\(\[&\]\(\) -> int \{ const std::string _pf_qty_type = " + r"\(t(?:\[0\])?\); " + r'if \(_pf_qty_type == std::string\("fixed"\)\) return 0; ' + r'if \(_pf_qty_type == std::string\("percent_of_equity"\)\) return 1; ' + r'if \(_pf_qty_type == std::string\("cash"\)\) return 2; ' + r'throw std::invalid_argument\("Unknown strategy sizing value"\); \}\(\)\)' + ) assert re.fullmatch( rf"set_pine_risk_{risk_func}\(\(double\)\(14\), " - r"\(\(t(?:\[0\])?\) == static_cast\(QtyType::PERCENT_OF_EQUITY\)\)\);", + rf"\(\({conversion}\) == static_cast\(QtyType::PERCENT_OF_EQUITY\)\)\);", calls[0], ) + assert ( + f'if (_pf_qty_type == std::string("{assigned}")) return {assigned_value};' + in calls[0] + ) write = re.search( - rf"\bt(?:\.update\({assigned_value}\)| = {assigned_value});", cpp, + rf'\bt(?:\.update\(std::string\("{assigned}"\)\)' + rf'| = std::string\("{assigned}"\));', cpp, ) assert write is not None assert write.start() < cpp.index(calls[0]) From 45b86624ea53fa0f0c3c8ed9e214a5b6f8b20178 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:45:39 +0800 Subject: [PATCH 26/27] Preserve unresolved script selection history Keep the established admission policy for non-nullable script selections while retaining exact compound value joins inside functions and methods. This avoids introducing collection history for previously unresolved global selections. --- pineforge_codegen/analyzer/types.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/pineforge_codegen/analyzer/types.py b/pineforge_codegen/analyzer/types.py index 3eda26e9..3a173bdf 100644 --- a/pineforge_codegen/analyzer/types.py +++ b/pineforge_codegen/analyzer/types.py @@ -41,8 +41,8 @@ from .. import signatures as sigs from ..ast_nodes import ( - ASTNode, Assignment, BinOp, BoolLiteral, ColorLiteral, ExprStmt, FuncCall, Identifier, IfStmt, - MemberAccess, NaLiteral, NumberLiteral, StringLiteral, Subscript, Ternary, + ASTNode, Assignment, BinOp, BoolLiteral, ColorLiteral, ExprStmt, FuncCall, FuncDef, + Identifier, IfStmt, MemberAccess, MethodDef, NaLiteral, NumberLiteral, StringLiteral, Subscript, Ternary, SwitchStmt, TupleAssign, TupleLiteral, UnaryOp, VarDecl, ) from ..errors import Phase @@ -294,7 +294,7 @@ def _selection_type_spec( *, nullable: bool | None = None, ) -> TypeSpec | None: - """A selection of identical value types retains that exact type.""" + """Keep exact callable joins and the established script selection policy.""" if nullable is None: nullable = any(self._selection_contains_na(node) for node, _ in branches) result: TypeSpec | None = None @@ -307,6 +307,13 @@ def _selection_type_spec( spec = TypeSpec.primitive("color") if spec is None: return None + if (not nullable and spec.kind not in {"map", "matrix"} + and not isinstance( + self._current_top_level_stmt, (FuncDef, MethodDef), + )): + # Callable value inference must not promote unresolved script + # selections into collection-history bindings. + return None result = ( spec if result is None From cbe9f2b005b155bbc32b693c5dd7671f8bbfaaeb Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 23:17:50 +0800 Subject: [PATCH 27/27] fix(codegen): compile qualified declarations and request helper forms Separate persistence from type qualifiers, retain mixed complete indentation levels, and preserve independently typed lower-timeframe tuple arrays. Keep helper bindings and tuple histories on the requested evaluator and omit unreachable forwarded TA instances. Add complete reported-case compilation regressions while preserving existing accepted controls. --- CHANGELOG.md | 9 + docs/reference/support-contracts.md | 24 ++ pineforge_codegen/analyzer/base.py | 12 +- pineforge_codegen/analyzer/call_handlers.py | 48 +++- pineforge_codegen/codegen/base.py | 99 ++++++- pineforge_codegen/codegen/security.py | 231 ++++++++++++++-- pineforge_codegen/codegen/visit_stmt.py | 10 +- pineforge_codegen/lexer.py | 16 +- pineforge_codegen/parser.py | 13 +- tests/test_callable_ta_forwarded_length.py | 14 + tests/test_lower_timeframe_tuple_types.py | 52 ++++ tests/test_mixed_block_indentation.py | 40 +++ .../test_persistent_declaration_qualifiers.py | 88 ++++++ tests/test_requested_na_helper_bindings.py | 52 ++++ tests/test_security_user_tuple_global.py | 24 +- ...test_v6_declaration_request_compilation.py | 260 ++++++++++++++++++ tests/test_v6_reporter_controls.py | 48 ++++ 17 files changed, 994 insertions(+), 46 deletions(-) create mode 100644 tests/test_lower_timeframe_tuple_types.py create mode 100644 tests/test_mixed_block_indentation.py create mode 100644 tests/test_persistent_declaration_qualifiers.py create mode 100644 tests/test_requested_na_helper_bindings.py create mode 100644 tests/test_v6_declaration_request_compilation.py create mode 100644 tests/test_v6_reporter_controls.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 88dac8f9..800c5442 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,15 @@ supported as exact pairs; on the 0.x line they are independent. See the ## Unreleased (next release) +- Persistent declarations accept a type qualifier after `var` or `varip`. + Lower-timeframe requests return independently typed arrays for inline and + helper-returned tuple payloads. +- Complete nested indentation levels can combine tabs and spaces. Requested + `na`/`nz` helper parameters retain their bindings beside time operations; + global tuple declarations and their member histories share one requested + sample. Uncalled forwarding helpers no longer allocate unresolved TA buffers + for their unreachable call variants (#372). + - A zone name the engine rejects now makes calendar-form `timestamp()` stop the run at its first evaluation, instead of silently calculating a UTC value. The paired engine reports a typed timezone error identifying the failed bar. diff --git a/docs/reference/support-contracts.md b/docs/reference/support-contracts.md index 7487bdda..cd4dbe34 100644 --- a/docs/reference/support-contracts.md +++ b/docs/reference/support-contracts.md @@ -1,5 +1,29 @@ ## Support contracts +Persistent typed declarations parse their storage prefix separately from their +qualifier: `var const float`, `varip const int`, and the corresponding `simple` +and `series` forms retain both annotations and their existing initialization. + +`request.security_lower_tf` tuple payloads, written inline or returned by a +helper, produce one array per element. Each intrabar evaluates the tuple once +before distributing values to the independently typed arrays; clearing the +request clears every array together. Tuple-bound arrays retain collection +metadata in global and callable scopes. + +A global tuple declaration read by `request.security` is sampled once in the +requested evaluator, in declaration order. Scalar and history reads share that +sample, including stateful helper results. Member history commits on completed +requested bars even when a lazy payload does not read the member on that bar. +Helper parameter bindings remain active through `na`/`nz` builtin emission in +both evaluator modes. These rules supersede the older tuple refusal limitations +described in the historical helper row below. + +Complete mixed tab/space nested block prefixes use four columns per tab plus +the spaces. Previously accepted incomplete mixed prefixes, skipped first-body +widths and pending logical-line continuations retain their earlier handling +pending additional TradingView observations; they are not newly refused as part +of the nested-block correction. + Builtin `array.slice` on a resolved typed array in the script, a function or method parameter, or a callable-local array opens the view gate and lowers the script's array storage to typed handles: diff --git a/pineforge_codegen/analyzer/base.py b/pineforge_codegen/analyzer/base.py index 772e2426..cd6f1e45 100644 --- a/pineforge_codegen/analyzer/base.py +++ b/pineforge_codegen/analyzer/base.py @@ -5514,6 +5514,9 @@ def _selection_tuple_shape( def _visit_TupleAssign(self, node: TupleAssign) -> PineType: val_type = self._visit(node.value) + lower_types = (node.value.annotations or {}).get( + "lower_tf_tuple_element_types", (), + ) loc = node.loc or SourceLocation(file=self._filename, line=1, col=1, end_col=1) is_program_global = ( self._global_scope and self._symbols.current_scope.name == "global" @@ -5615,7 +5618,14 @@ def _visit_TupleAssign(self, node: TupleAssign) -> PineType: self._record_script_block_binding(sym, outer_symbol) self._note_member_writer(sym, name, node) - if (self._collection_scope_stack + if idx < len(lower_types): + inner = lower_types[idx] + spec = TypeSpec.array( + TypeSpec.primitive("int64") if inner == PineType.INT + else self._pine_type_to_spec(inner) + ) + self._record_collection_type(name, spec) + elif (self._collection_scope_stack and self._block_node_stack and self._symbols.current_scope.name != "global"): owner_id = id(self._block_node_stack[-1]) diff --git a/pineforge_codegen/analyzer/call_handlers.py b/pineforge_codegen/analyzer/call_handlers.py index 8fef0994..38afa02b 100644 --- a/pineforge_codegen/analyzer/call_handlers.py +++ b/pineforge_codegen/analyzer/call_handlers.py @@ -1001,9 +1001,8 @@ def _handle_request_security_lower_tf(self, node: FuncCall) -> PineType: the codegen knows to emit a ``std::vector`` accumulator that gets cleared on sub-bar 0 and pushed-to per sub-bar. - UDT / color / string element types are deliberately rejected here - with a precise diagnostic; the runtime path only knows how to - accumulate ``double`` / ``int`` / ``bool``.""" + Scalar calls retain their existing numeric/bool arrays. Tuple calls + carry each primitive element's type to its own array accumulator.""" param_names = ["symbol", "timeframe", "expression", "ignore_invalid_symbol", "currency", "ignore_invalid_timeframe", "calc_bars_count"] @@ -1033,13 +1032,6 @@ def _handle_request_security_lower_tf(self, node: FuncCall) -> PineType: node.loc, ) - if isinstance(expr_node, TupleLiteral): - self._error( - "request.security_lower_tf does not support tuple expressions yet. " - "Issue separate request.security_lower_tf calls for each series.", - node.loc, - ) - for arg in node.args: if arg is not None and arg is not expr_node: self._visit(arg) @@ -1054,20 +1046,45 @@ def _handle_request_security_lower_tf(self, node: FuncCall) -> PineType: ta_end = len(self._ta_call_sites) security_ta_range = (ta_start, ta_end) if ta_end > ta_start else None + tuple_types = self._tuple_element_types_by_node.get(id(expr_node), ()) + if (isinstance(expr_node, FuncCall) + and isinstance(expr_node.callee, Identifier) + and self._func_returns_tuple.get(expr_node.callee.name, False)): + tuple_types = self._func_tuple_element_types.get(expr_node.callee.name, ()) + if PineType.UNKNOWN in tuple_types: + tuple_types = self._security_callsite_tuple_types( + expr_node.callee.name, expr_node, tuple_types, + ) + elif (isinstance(expr_node, FuncCall) + and isinstance(expr_node.callee, MemberAccess) + and isinstance(expr_node.callee.object, Identifier) + and expr_node.callee.object.name == "ta"): + count = TA_TUPLE_ELEMENT_COUNTS.get(expr_node.callee.member, 0) + if count: + tuple_types = tuple_types or (PineType.FLOAT,) * count + # Cache the resolved element type on the call node so the # ``_type_spec_from_expr`` pass can map ``request.security_lower_tf`` # to ``array`` without re-visiting the expression (which would # double-allocate TA call sites for expressions like ``ta.ema``). cached_anns = getattr(node, "annotations", None) or {} cached_anns["lower_tf_element_pine_type"] = expr_pine_type + if tuple_types: + cached_anns["lower_tf_tuple_element_types"] = tuple_types + self._security_tuple_shapes[id(node)] = len(tuple_types) node.annotations = cached_anns - if expr_pine_type not in (PineType.FLOAT, PineType.INT, PineType.BOOL, - PineType.NA, PineType.UNKNOWN): + element_types = tuple_types or (expr_pine_type,) + supported = {PineType.FLOAT, PineType.INT, PineType.BOOL, + PineType.NA, PineType.UNKNOWN} + if tuple_types: + supported |= {PineType.STRING, PineType.COLOR} + unsupported = next((item for item in element_types if item not in supported), None) + if unsupported is not None: element_label = { PineType.STRING: "string", PineType.COLOR: "color", - }.get(expr_pine_type, str(expr_pine_type)) + }.get(unsupported, str(unsupported)) self._error( "request.security_lower_tf element type '" + element_label + "' is not yet supported. Supported element types: float, int, bool. " @@ -1085,8 +1102,9 @@ def _handle_request_security_lower_tf(self, node: FuncCall) -> PineType: sec_id=sec_id, timeframe=context["timeframe"] if context is not None else tf_node, expression=expr_node, - returns_tuple=False, - tuple_size=0, + returns_tuple=bool(tuple_types), + tuple_size=len(tuple_types), + tuple_element_types=tuple(tuple_types), gaps=None, lookahead=None, ta_range=security_ta_range, diff --git a/pineforge_codegen/codegen/base.py b/pineforge_codegen/codegen/base.py index 6bd0a8ad..d664af82 100644 --- a/pineforge_codegen/codegen/base.py +++ b/pineforge_codegen/codegen/base.py @@ -800,6 +800,7 @@ def func_var_storage(owner: str, raw_name: str) -> str: # authority for nested stateful-helper dispatch (see visit_call). self._current_instance_name: str | None = None self._instance_dispatch: dict[tuple[str | None, int], str] = {} + self._live_func_instances: set[str] | None = None self._fresh_instances: list[dict] = [] self._fresh_var_members: list[tuple[str, str, str]] = [] # Fresh fixnan members for context-sensitive helper instances (nested @@ -2064,6 +2065,7 @@ def natural_name(fname: str, cs_idx: int) -> str: interned: dict[tuple, dict] = {} worklist: list[dict] = [] seen_walk: set[str] = set() + instances: dict[str, dict] = {} fresh_counter = 0 # Seed with the natural clones the flat emission loop produces. @@ -2096,6 +2098,7 @@ def natural_name(fname: str, cs_idx: int) -> str: if inst["name"] in seen_walk: continue seen_walk.add(inst["name"]) + instances[inst["name"]] = inst body = func_bodies.get(inst["fname"]) if not body: continue @@ -2219,6 +2222,84 @@ def natural_name(fname: str, cs_idx: int) -> str: worklist.append(ginst) self._instance_dispatch[(inst["name"], id(callnode))] = ginst["name"] + # Definition-time calls reserve variants too. Only paths rooted in + # executable statements instantiate their chart-context TA buffers. + # Preserve all semantic analysis; select emission after composing the + # same natural/fresh dispatch graph that the call visitor uses. + for fi in ctx.func_infos: + instances.setdefault(self._func_cpp_base_name(fi.name), { + "fname": fi.name, "name": self._func_cpp_base_name(fi.name), + "call_site_idx": None, "ta_remap": {}, + }) + methods: dict[int, set[str]] = {} + for call_id, key in ctx.method_call_bindings: + methods.setdefault(call_id, set()).add(key) + + def targets(call, parent): + dispatch = self._instance_dispatch.get( + (parent["name"] if parent is not None else None, id(call)), + ) + if dispatch is not None: + return [dispatch] + bound = ctx.func_call_cs_map.get(id(call)) + if bound is not None: + names = [bound[0]] + elif id(call) in inherited_names: + names = [inherited_names[id(call)]] + elif isinstance(call.callee, Identifier) and call.callee.name in func_bodies: + names = [call.callee.name] + else: + names = sorted(methods.get(id(call), ())) + result = [] + for name in names: + if name in stateful and ctx.func_call_site_counts.get(name, 0) > 0: + index = ( + bound[1] if bound is not None + else (parent.get("call_site_idx") or 0) if parent is not None + else 0 + ) + result.append(natural_name(name, index)) + else: + result.append(self._func_cpp_base_name(name)) + return result + + roots = [ + stmt for stmt in ctx.ast.body + if not isinstance(stmt, (FuncDef, MethodDef)) + ] + pending = [ + target + for call in self._iter_func_calls(roots) + for target in targets(call, None) + ] + live: set[str] = set() + live_owners: set[str] = set() + used_ta: set[str] = set() + while pending: + if self._budget is not None: + self._budget.check(phase=Phase.CODEGEN) + name = pending.pop() + if name in live: + continue + live.add(name) + instance = instances.get(name) + if instance is None: + continue + owner = instance["fname"] + live_owners.add(owner) + used_ta.update(instance.get("ta_remap", {}).values()) + for call in self._iter_func_calls(func_bodies.get(owner, ())): + pending.extend(targets(call, instance)) + self._live_func_instances = live + self._dead_func_names.update(stateful - live_owners) + self._dead_ta_indices = { + index for index, site in enumerate(ctx.ta_call_sites) + if site.owner_func in stateful and site.member_name not in used_ta + } + self._fresh_instances[:] = [ + instance for instance in self._fresh_instances if instance["name"] in live + ] + def _callable_var_collection_spec( self, name: str, owner_func: str | None = None) -> TypeSpec | None: """Exact TypeSpec for a function-scoped persistent collection member. @@ -5596,17 +5677,27 @@ def _generate_cpp(self) -> str: # emits ``std::vector`` and the eval method pushes the # per-sub-bar value. Element type is inferred from the # expression — analyzer constrained it to int / float / bool. + tuple_types = item.get("tuple_element_types", ()) ctype = self._infer_cpp_type_for_security_elem(expr_node) - if ctype not in ("double", "int", "bool"): + if tuple_types: + accumulators = ", ".join( + f"std::vector<{part}>" + for part in self._lower_tf_tuple_cpp_types(tuple_types) + ) + result_type = f"std::tuple<{accumulators}>" + elif ctype not in ("double", "int", "bool"): # Defensive fallback — analyzer should already have # rejected unsupported types, but keep the codegen # well-defined if a future path slips through. ctype = "double" + result_type = f"std::vector<{ctype}>" + else: + result_type = f"std::vector<{ctype}>" self._security_ohlc_hist_fields_by_sec[sec_id] = ( self._collect_security_ohlc_hist_fields(state_expr) ) lines.append( - f" std::vector<{ctype}> _req_sec_lower_tf_{sec_id}{{}};" + f" {result_type} _req_sec_lower_tf_{sec_id}{{}};" ) for field in sorted( self._security_ohlc_hist_fields_by_sec.get(sec_id, ()) @@ -6279,6 +6370,10 @@ def allocate_legacy_flag(base: str) -> str: if (has_ta or has_series or needs_security_clone) and total_cs > 0: # Emit one variant per call site for cs_idx in range(total_cs): + if (self._live_func_instances is not None + and f"{self._func_cpp_base_name(fi.name)}_cs{cs_idx}" + not in self._live_func_instances): + continue self._emit_func_def(fi, lines, call_site_idx=cs_idx) lines.append("") else: diff --git a/pineforge_codegen/codegen/security.py b/pineforge_codegen/codegen/security.py index 056756ab..c7c64c4a 100644 --- a/pineforge_codegen/codegen/security.py +++ b/pineforge_codegen/codegen/security.py @@ -786,6 +786,24 @@ def _emit_runtime_security_templates(self, lines: list[str]) -> None: " };", )) + @staticmethod + def _lower_tf_tuple_cpp_types(element_types) -> list[str]: + return [ + "int64_t" if item == PineType.INT + else PINE_TYPE_TO_CPP.get(item, "double") + for item in element_types + ] + + @staticmethod + def _lower_tf_clear_cpp(item: dict) -> str: + name = f"_req_sec_lower_tf_{item['sec_id']}" + if item.get("returns_tuple"): + return ( + "std::apply([](auto&... _pf_arrays) { " + f"(_pf_arrays.clear(), ...); }}, {name});" + ) + return f"{name}.clear();" + def _runtime_security_result(self, item: dict, prefix: str) -> str: sec_id = item["sec_id"] if item.get("is_lower_tf_array"): @@ -4604,7 +4622,11 @@ def walk(n) -> None: walk(x) walk(node) - return [n for n in out if id(n) not in in_helper or reached[id(n)] == 1] + return [ + n for n in out + if id(n) not in in_helper or reached[id(n)] == 1 + or self._security_global_history_value(n) is n.object + ] def _security_global_history_value(self, node) -> ASTNode | None: """The value of ``g`` in a payload's ``g[k]`` when ``g`` is a global @@ -4623,6 +4645,10 @@ def _security_global_history_value(self, node) -> ASTNode | None: return None name = node.object.name global_expr_map = getattr(self.ctx, "global_expr_map", {}) or {} + if (self._security_identifier_is_global_binding(node.object) + and name in self._direct_program_tuple_bindings + and name not in self._global_mutable_infos): + return node.object if ( not self._security_identifier_is_global_binding(node.object) or name not in global_expr_map @@ -4634,6 +4660,10 @@ def _security_global_history_value(self, node) -> ASTNode | None: seen = {name} while isinstance(value, Identifier): name = value.name + if (self._security_identifier_is_global_binding(value) + and name in self._direct_program_tuple_bindings + and name not in self._global_mutable_infos): + return node.object if (not self._security_identifier_is_global_binding(value) or name not in global_expr_map or name in seen or name in self._direct_program_tuple_binding_names @@ -4803,6 +4833,8 @@ def _emit_security_expr_hist_members( value = self._security_global_history_value(node) if self._is_session_flag(node.object): cpp_t = "bool" + elif value is node.object: + cpp_t = self._infer_type(value) elif value is not None: # A global's history is a double (a bool's a bool): its na # reads na, where typed from the global's ``int`` the first @@ -4812,12 +4844,14 @@ def _emit_security_expr_hist_members( cpp_t = "bool" if self._infer_type(value) == "bool" else "double" else: cpp_t = self._infer_type(node.object) - if cpp_t not in ("double", "int", "bool"): + if cpp_t not in ("double", "int", "int64_t", "bool", "std::string"): cpp_t = "double" name = f"_sec{sec_id}_expr_hist_{idx}" self._security_expr_hist_by_node[(sec_id, id(node))] = { "name": name, "type": cpp_t, + "object": node.object, + "tuple_global": value is node.object, } lines.append(f" Series<{cpp_t}> {name}{mbb_suffix};") @@ -6103,6 +6137,10 @@ def _security_global_value_inlinable(self, name: str) -> bool: requested bar at each read: its value, through the globals it reads, calls no multi-statement user function, whose statements -- and TA state -- the evaluator would emit again at every read.""" + if name in self._direct_program_tuple_bindings: + # The declaration is evaluated once in the evaluator prologue, + # including a multi-statement tuple helper. + return True cache = getattr(self, "_security_global_value_cache", None) if cache is None: cache = self._security_global_value_cache = {} @@ -6187,6 +6225,79 @@ def _security_pure_call(self, node: FuncCall) -> bool: and len(node.args) == len(info.node.params) and self._security_pure_body(info.node)) + def _security_needed_global_tuples(self, root) -> set[int]: + """Tuple declarations reached through the payload's resolved bindings.""" + needed: set[int] = set() + seen: set[int] = set() + pending = [root] + globals_ = getattr(self.ctx, "global_expr_map", {}) or {} + while pending: + if getattr(self, "_budget", None) is not None: + self._budget.check(phase=Phase.CODEGEN) + node = pending.pop() + if isinstance(node, (list, tuple)): + pending.extend(node) + continue + if not isinstance(node, ASTNode) or id(node) in seen: + continue + seen.add(id(node)) + if isinstance(node, Identifier) and self._security_identifier_is_global_binding(node): + binding = self._direct_program_tuple_bindings.get(node.name) + if binding is not None: + needed.add(id(binding[0])) + value = globals_.get(node.name) + if value is not None: + pending.append(value) + if isinstance(node, FuncCall): + key = self._security_user_call_key(node) + info = self._func_info_map.get(key) + if info is not None and info.node is not None: + pending.extend(info.node.body) + pending.extend(syntax_children(node)) + return needed + + def _security_global_tuple_element( + self, sec_id, name, ta_results, resolving, mutable_names, lines, + ) -> str: + """Sample a declaration once; all member reads share its tuple value.""" + cached = self._security_arg_global_values + if name in cached: + return cached[name] + declaration, _index = self._direct_program_tuple_bindings[name] + key = f"tuple:{id(declaration)}" + if key in resolving: + raise AssertionError("cyclic resolved tuple declaration") + if lines is None: + element = self._security_pure_user_tuple_element(name, declaration.value) + if element is not None: + _info, stack = self._security_bind_helper_args(declaration.value, ()) + return self._build_security_expr( + sec_id, element, None, ta_results, resolving, + mutable_names, stack, lines, + ) + raise AssertionError("tuple declaration requires a statement context") + resolving.add(key) + try: + value = self._build_security_expr( + sec_id, declaration.value, None, ta_results, resolving, + mutable_names, (), lines, + ) + finally: + resolving.remove(key) + temp = "_pf" + self._security_next_inline_name(sec_id, "tuple", "value") + lines.append(f" const auto {temp} = {value};") + site = self._get_ta_site(declaration.value) + fields = TA_TUPLE_FIELDS.get( + self._ta_name_from_site(site) if site is not None else "", (), + ) + for slot, target in enumerate(declaration.names): + if target != "_": + cached[target] = ( + f"{temp}.{fields[slot]}" if slot < len(fields) + else f"std::get<{slot}>({temp})" + ) + return cached[name] + def _security_pure_user_tuple_element(self, name: str, call): """The element expression a payload reads when it reads ``name``, a global of a tuple declaration ``[.., name, ..] = f(args)`` whose call @@ -8712,6 +8823,40 @@ def emit_security_ta(indices: list[int]) -> None: self._emit_security_rebinds(sec_id, info, lines, ta_results, indent=2, emitted_lines=lines) emit_security_ta(post_rebind_ta_indices) + needed_tuples = self._security_needed_global_tuples(expr_node) + for declaration in self.ctx.ast.body: + if isinstance(declaration, TupleAssign) and id(declaration) in needed_tuples: + target = next((name for name in declaration.names if name != "_"), None) + if target is not None: + self._security_global_tuple_element( + sec_id, target, ta_results, set(), security_mutable_names, lines, + ) + # Global tuple declarations run on every requested bar, even when + # a lazy payload does not read their history on that bar. + for (sid, _node_id), meta in self._security_expr_hist_by_node.items(): + obj = meta.get("object") + if (sid != sec_id or not isinstance(obj, Identifier) + or not meta.get("tuple_global")): + continue + value = self._security_arg_global_values.get(obj.name) + if value is None: + value = self._build_security_expr( + sec_id, obj, None, ta_results, set(), + security_mutable_names, (), lines, + ) + hist, cpp_t = meta["name"], meta["type"] + if cpp_t in ("int", "int64_t"): + value = na_preserving_int_cast(value, cpp_t) + elif cpp_t == "double": + value = ( + "[](auto _pf_v) { return is_na(_pf_v) ? na() " + f": static_cast(_pf_v); }}({value})" + ) + if (hist, cpp_t) not in self._security_global_hist_pushes: + self._security_global_hist_pushes.append((hist, cpp_t)) + lines.append( + f" if (is_complete) {{ {hist}_next = {value}; {hist}_set = true; }}" + ) # A global is an unconditional statement of the requested context, # even when the payload reads it only in an untaken selection arm. # Its sampled dependencies keep declaration order and one value. @@ -8727,6 +8872,7 @@ def emit_security_ta(indices: list[int]) -> None: and tuple_size and tuple_size > 0 and isinstance(expr_node, TupleLiteral) + and not item.get("is_lower_tf_array") ): # A tuple body destructures into per-element scalar members # ``_req_sec_{sec_id}_{i}`` (declared in ``base.py`` and reset in @@ -8773,6 +8919,41 @@ def emit_security_ta(indices: list[int]) -> None: emitted_lines=lines, ) if item.get("is_lower_tf_array"): + if returns_tuple: + # Evaluate the tuple once per intrabar, then distribute its + # elements to arrays of their own types. A stateful helper + # must not run once for every destructured element. + temp = f"_pf_lower_tuple_{sec_id}" + lines.append(f" const auto {temp} = {expr_cpp};") + lines.append( + f" if (security_lower_tf_sub_bar_index({sec_id}) == 0) " + f"{{ {self._lower_tf_clear_cpp(item)} }}" + ) + site = self._get_ta_site(expr_node) + fields = TA_TUPLE_FIELDS.get( + self._ta_name_from_site(site) if site is not None else "", (), + ) + for index, ctype in enumerate(self._lower_tf_tuple_cpp_types( + item["tuple_element_types"])): + value = ( + f"{temp}.{fields[index]}" if index < len(fields) + else f"std::get<{index}>({temp})" + ) + if ctype in ("int", "int64_t"): + value = na_preserving_int_cast(value, ctype) + elif ctype == "double": + value = ( + "[](auto _pf_v) { return is_na(_pf_v) ? na() " + f": static_cast(_pf_v); }}({value})" + ) + lines.append( + f" std::get<{index}>(_req_sec_lower_tf_{sec_id})" + f".push_back({value});" + ) + self._emit_security_ohlc_hist_pushes(sec_id, lines) + self._emit_security_ta_hist_pushes(sec_id, info, ta_results, lines) + lines.extend((" }", "")) + return # Match the accumulator's element type before adapting its value. # An integer sentinel must not pass through a floating NaN. ctype = self._infer_cpp_type_for_security_elem(expr_node) @@ -8906,7 +9087,7 @@ def _emit_security_evaluators(self, lines: list[str]) -> None: # gaps mode flushes between completions). Clearing the # vector is the right fallback. lines.append(f" case {sec_id}:") - lines.append(f" _req_sec_lower_tf_{sec_id}.clear();") + lines.append(f" {self._lower_tf_clear_cpp(item)}") for field in sorted(self._security_ohlc_hist_fields_by_sec.get(sec_id, ())): lines.append( f" {self._security_ohlc_hist_series_cpp(sec_id, field)}.clear();" @@ -9357,19 +9538,11 @@ def _build_security_expr( if expr_node.name in self._security_arg_global_values: return self._security_arg_global_values[expr_node.name] value = global_expr_map[expr_node.name] - if (expr_node.name in self._direct_program_tuple_binding_names - and self._security_ta_tuple_element_field(expr_node.name) is None): - # An element of a tuple declaration whose call is a pure - # user function: only that element is lowered, on the - # requested bar, with the call's arguments bound to the - # function's parameters. - element = self._security_pure_user_tuple_element( - expr_node.name, value) - if element is not None: - _info, bound_stack = self._security_bind_helper_args(value, ()) - return self._build_security_expr( - sec_id, element, ta_range, ta_results, resolving, - security_mutable_names, bound_stack, emitted_lines) + if expr_node.name in self._direct_program_tuple_bindings: + return self._security_global_tuple_element( + sec_id, expr_node.name, ta_results, resolving, + security_mutable_names, emitted_lines, + ) # A global bound to a user call is one value per requested # bar, however many reads the payload makes (``g - g[1]``): # every read inlined the call again, which advanced its TA @@ -9679,6 +9852,7 @@ def _build_security_expr( self._security_identifier_is_global_binding(expr_node.object) and expr_node.object.name in self._direct_program_tuple_binding_names and expr_node.object.name not in self._global_mutable_infos + and not self._security_reads_global_history(sec_id, expr_node) ): # Its value is the whole tuple: every earlier build # emitted the tuple's history, which did not compile. @@ -9752,7 +9926,8 @@ def _build_security_expr( # builtin keeps the chart's series and never gets here): # a double, which an integer slot of this evaluator # narrows na-preserving (``_security_emits_double``). - if self._infer_type(expr_node.object) == "std::string": + if (self._infer_type(expr_node.object) == "std::string" + and not meta.get("tuple_global")): # No string history on the requested clock; every # earlier build refused the read. self._codegen_error( @@ -10463,6 +10638,7 @@ def _security_check_tuple_element_history( and self._security_identifier_is_global_binding(n.object) and n.object.name in self._direct_program_tuple_binding_names and n.object.name not in self._global_mutable_infos + and not self._security_reads_global_history(sec_id, n) and self._resolve_security_index_literal(n.index, bindings) != 0): name = n.object.name site = self._get_ta_site(global_expr_map.get(name)) @@ -11233,6 +11409,27 @@ def _security_fallback_owns( history (re-evaluating ``g`` on the requested bar would pair it with the chart's ``g[1]``), and ``timeframe.*`` (beside the chart's ``timeframe.in_seconds()``).""" + # A helper's parameter is bound in both evaluator modes. The legacy + # fallback may keep a surrounding builtin's rendering, but it cannot + # resolve that parameter in the chart function's lexical scope. + if (isinstance(node, Identifier) + and not self._security_identifier_is_global_binding(node)): + binding = self._security_lookup_helper_binding_context( + node.name, helper_binding_stack, + ) + if binding is not None and self._security_bound_is_scalar(*binding): + return True + if (isinstance(node, Identifier) + and self._security_identifier_is_global_binding(node) + and node.name in self._security_arg_global_values): + return True + if (isinstance(node, Subscript) + and isinstance(node.object, Identifier) + and self._security_expr_hist_by_node.get( + (sec_id, id(node)), {}, + ).get("tuple_global") + and self._security_reads_global_history(sec_id, node)): + return True selection = getattr(self, "_security_selection_context", None) if selection is not None: if id(node) in selection["reads"]: diff --git a/pineforge_codegen/codegen/visit_stmt.py b/pineforge_codegen/codegen/visit_stmt.py index ffdb77e0..eea70d37 100644 --- a/pineforge_codegen/codegen/visit_stmt.py +++ b/pineforge_codegen/codegen/visit_stmt.py @@ -1707,7 +1707,7 @@ def emit_call_tuple(call_expr: str, *, decompose: bool = False) -> None: # User-defined function returning a tuple: use C++17 structured bindings if isinstance(node.value, FuncCall): func_name, namespace = self._resolve_callee(node.value.callee) - if namespace == "request" and func_name == "security": + if namespace == "request" and func_name in ("security", "security_lower_tf"): # A request whose names are reassigned is read where it is # evaluated (external_requests): its data-missing stop. if UNPINNED_ANNOTATION in (node.value.annotations or {}): @@ -1764,6 +1764,14 @@ def _tuple_call_info(self, call: FuncCall): def _tuple_binding_cpp_types(self, node: TupleAssign) -> list[str]: """Exact supported tuple element types for later lexical operations.""" count = len(node.names) + lower_types = (node.value.annotations or {}).get( + "lower_tf_tuple_element_types", (), + ) + if lower_types: + return [ + f"std::vector<{ctype}>" + for ctype in self._lower_tf_tuple_cpp_types(lower_types) + ] if isinstance(node.value, (IfStmt, SwitchStmt)): return self._infer_selection_tuple_types(node.value, count) if not isinstance(node.value, FuncCall): diff --git a/pineforge_codegen/lexer.py b/pineforge_codegen/lexer.py index aba38033..79f3e6d1 100644 --- a/pineforge_codegen/lexer.py +++ b/pineforge_codegen/lexer.py @@ -386,12 +386,24 @@ def _tokenize_line(self) -> None: indent_level += 1 raw = self.source[line_start: self.pos] - if " \t" in raw: + columns = raw.count("\t") * 4 + raw.count(" ") + mixed = "\t" in raw and " " in raw + aligned_block = ( + columns % 4 == 0 + and columns // 4 <= self.indent_stack[-1] + 1 + ) + if " \t" in raw and not aligned_block: self._emit_diagnostic( "Indentation cannot contain spaces followed by a tab.", self.line, self.col, self.col + 1, ) - if "\t" in raw: + if mixed and aligned_block and not self._in_continuation: + indent_level = columns // 4 + elif "\t" in raw: + # Keep the established treatment of incomplete mixed prefixes + # and first-body skipped widths, including pending logical-line + # continuations. Their TV grammar is not established by the + # nested, complete-level layouts. indent_level = raw.count("\t") else: indent_level = len(raw) // 4 diff --git a/pineforge_codegen/parser.py b/pineforge_codegen/parser.py index 2645bf0f..2ea987d0 100644 --- a/pineforge_codegen/parser.py +++ b/pineforge_codegen/parser.py @@ -835,12 +835,19 @@ def _looks_like_call_template_args(self) -> bool: return False def _parse_var_keyword_decl(self) -> VarDecl: - """Parse: var [type] name = expr or varip [type] name = expr""" + """Parse: (var | varip) [[qualifier] type] name = expr.""" start_tok = self._current() is_var = self._current().type == TokenType.VAR is_varip = self._current().type == TokenType.VARIP self._advance() + qualifier = None + if (self._check(TokenType.IDENT) + and self._current().value in ("const", "series", "simple") + and (self._peek().type in TYPE_KEYWORDS + or self._is_ident_typed_var_decl(offset=1))): + qualifier = self._advance().value + type_hint = None if self._current().type in TYPE_KEYWORDS: type_hint = self._parse_type_hint_string() @@ -873,6 +880,10 @@ def _parse_var_keyword_decl(self) -> VarDecl: name=name_tok.value, value=value, is_var=is_var, is_varip=is_varip, type_hint=type_hint, ) + if qualifier is not None: + node.annotations = {"qualifier": qualifier} + if self._library and qualifier == "const": + node.annotations["declared_const"] = True self._retain_declaration_names(node, name_tok) return self._set_loc(node, start_tok) diff --git a/tests/test_callable_ta_forwarded_length.py b/tests/test_callable_ta_forwarded_length.py index 10e82dca..77833618 100644 --- a/tests/test_callable_ta_forwarded_length.py +++ b/tests/test_callable_ta_forwarded_length.py @@ -11,6 +11,7 @@ from pineforge_codegen import transpile from pineforge_codegen.errors import CompileError +from tests._compile import compile_cpp from tests.test_runtime_var_initialization import _compile_and_run @@ -640,3 +641,16 @@ def test_recursive_stateful_callable_is_rejected_before_instance_expansion() -> CompileError, match="Recursive stateful callable cycle" ): transpile(source) + + +@pytest.mark.parametrize("average", ["rma", "ema", "sma"]) +def test_unreachable_forwarding_keeps_live_input_backed_lengths(average) -> None: + source = f'''//@version=6 +strategy("reachable input length") +filtered(float sample, int window) => ta.{average}(sample, window) +unreached(float sample, int window) => filtered(filtered(sample, window), window) +period = input.int(7, "Period", minval = 1) +observed = filtered(window = period, sample = close) +plot(observed) +''' + compile_cpp(transpile(source, check_support=True), label="reachable-input-backed-ta-length") diff --git a/tests/test_lower_timeframe_tuple_types.py b/tests/test_lower_timeframe_tuple_types.py new file mode 100644 index 00000000..00fac391 --- /dev/null +++ b/tests/test_lower_timeframe_tuple_types.py @@ -0,0 +1,52 @@ +"""Lower-timeframe tuple elements retain independent array types.""" + +import pytest + +from pineforge_codegen import transpile +from pineforge_codegen.analyzer import Analyzer +from pineforge_codegen.lexer import Lexer +from pineforge_codegen.parser import Parser +from pineforge_codegen.symbols import TypeSpec +from tests._compile import compile_cpp + + +@pytest.mark.parametrize("helper", [False, True], ids=["inline", "helper"]) +@pytest.mark.parametrize("named", [False, True], ids=["positional", "named"]) +def test_lower_timeframe_tuple_collection_types_are_per_element(helper, named): + payload = '[close, bar_index, close > open, "sample", color.blue]' + declaration = "" + if helper: + declaration = ( + "pack(float price, int ordinal, bool rising) =>\n" + ' [price, ordinal, rising, "sample", color.blue]\n' + ) + payload = "pack(close, bar_index, close > open)" + arguments = ( + f'expression = {payload}, timeframe = "15", symbol = syminfo.tickerid' + if named else f'syminfo.tickerid, "15", {payload}' + ) + source = ( + '//@version=6\nstrategy("typed lower tuple")\n' + declaration + + f"[prices, positions, rising, labels, colors] = request.security_lower_tf({arguments})\n" + + "plot(array.size(prices) + array.size(positions) + array.size(rising)" + + " + array.size(labels) + array.size(colors))\n" + ) + ast = Parser(Lexer(source).tokenize(), source=source).parse() + context = Analyzer(ast).analyze() + for name, inner in zip( + ("prices", "positions", "rising", "labels", "colors"), + ("float", "int64", "bool", "string", "color"), + ): + assert context.collection_types[name] == TypeSpec.array(TypeSpec.primitive(inner)) + compile_cpp(transpile(source, check_support=True), label="lower-tuple-per-element-types") + + +def test_lower_timeframe_tuple_arrays_keep_lexical_method_dispatch(): + source = """//@version=6 +strategy("local lower tuple") +readSize() => + [prices, rising] = request.security_lower_tf(syminfo.tickerid, "15", [close, close > open]) + prices.size() + rising.size() +plot(readSize()) +""" + compile_cpp(transpile(source, check_support=True), label="lower-tuple-lexical-array-dispatch") diff --git a/tests/test_mixed_block_indentation.py b/tests/test_mixed_block_indentation.py new file mode 100644 index 00000000..23d0790b --- /dev/null +++ b/tests/test_mixed_block_indentation.py @@ -0,0 +1,40 @@ +"""Mixed indentation preserves nested block structure and accepted wraps.""" + +import pytest + +from pineforge_codegen.ast_nodes import IfStmt, VarDecl +from pineforge_codegen.lexer import Lexer, TokenType +from pineforge_codegen.parser import Parser + + +@pytest.mark.parametrize( + "outer,inner", + [("\t", "\t "), (" ", " \t"), ("\t", "\t\t"), (" ", " ")], +) +def test_mixed_complete_levels_keep_nested_ast_and_dedent(outer, inner): + source = ( + "if close > open\n" + outer + "if high > close\n" + + inner + "observed = close\n" + + "after = open\n" + ) + tokens = Lexer(source).tokenize() + assert [token.line for token in tokens if token.type == TokenType.INDENT] == [2, 3] + assert [token.line for token in tokens if token.type == TokenType.DEDENT] == [4, 4] + program = Parser(tokens, source=source).parse() + outer_node, after = program.body + assert isinstance(outer_node, IfStmt) + (inner_node,) = outer_node.body + assert isinstance(inner_node, IfStmt) + (observed,) = inner_node.body + assert isinstance(observed, VarDecl) + assert observed.name == "observed" + assert isinstance(after, VarDecl) + assert after.name == "after" + + +@pytest.mark.parametrize("extra_spaces", [1, 2, 3]) +def test_previously_accepted_tab_first_incomplete_widths_keep_their_block(extra_spaces): + source = "if close > open\n\t" + " " * extra_spaces + "observed = close\n" + program = Parser(Lexer(source).tokenize(), source=source).parse() + (declaration,) = program.body[0].body + assert declaration.name == "observed" diff --git a/tests/test_persistent_declaration_qualifiers.py b/tests/test_persistent_declaration_qualifiers.py new file mode 100644 index 00000000..c47db1d6 --- /dev/null +++ b/tests/test_persistent_declaration_qualifiers.py @@ -0,0 +1,88 @@ +"""Persistence and qualifiers occupy separate positions in the declaration AST.""" + +import pytest + +from pineforge_codegen.ast_nodes import VarDecl +from pineforge_codegen.lexer import Lexer +from pineforge_codegen.parser import Parser + + +@pytest.mark.parametrize("storage", ["var", "varip"]) +@pytest.mark.parametrize( + "qualifier,hint,initializer,expected_hint", + [ + ("const", "string", '"x"', "string"), + ("const", "float", "1.5", "float"), + ("const", "int", "3", "int"), + ("const", "bool", "true", "bool"), + ("const", "color", "color.blue", "color"), + ("simple", "int", "3", "int"), + ("series", "float", "close", "float"), + ("series", "float[]", "array.new_float(0)", "array"), + ("series", "array", "array.new_float(0)", "array"), + ("series", "Sample", "Sample.new()", "Sample"), + ("series", "lib.Sample", "lib.newSample()", "lib.Sample"), + ], +) +def test_persistence_then_qualifier_keeps_the_declaration( + storage, qualifier, hint, initializer, expected_hint, +): + source = f"{storage} {qualifier} {hint} held = {initializer}\n" + program = Parser(Lexer(source).tokenize(), source=source).parse() + assert len(program.body) == 1 + declaration = program.body[0] + assert isinstance(declaration, VarDecl) + assert declaration.name == "held" + assert declaration.type_hint == expected_hint + assert (declaration.is_var, declaration.is_varip) == ( + storage == "var", storage == "varip", + ) + assert declaration.annotations["qualifier"] == qualifier + assert (declaration.loc.line, declaration.loc.col) == (1, 1) + ((name, location),) = declaration.annotations["declaration_names"] + assert name == "held" + assert (location.line, location.col) == (1, source.index("held") + 1) + + +@pytest.mark.parametrize("storage", ["var", "varip"]) +@pytest.mark.parametrize("word", ["const", "simple", "series"]) +@pytest.mark.parametrize("hint", ["", "int ", "Sample "]) +def test_contextual_qualifier_words_in_the_name_slot_are_names(storage, word, hint): + source = f"{storage} {hint}{word} = 3\n" + declaration = Parser(Lexer(source).tokenize(), source=source).parse().body[0] + assert isinstance(declaration, VarDecl) + assert declaration.name == word + assert declaration.type_hint == (hint.strip() or None) + assert "qualifier" not in declaration.annotations + + +@pytest.mark.parametrize("storage", ["var", "varip"]) +@pytest.mark.parametrize("word", ["const", "simple", "series"]) +def test_contextual_qualifier_word_as_a_type_keeps_its_type(storage, word): + source = f"{storage} {word} held = {word}.new()\n" + declaration = Parser(Lexer(source).tokenize(), source=source).parse().body[0] + assert declaration.type_hint == word + assert declaration.name == "held" + assert "qualifier" not in declaration.annotations + + +@pytest.mark.parametrize("storage", ["var", "varip"]) +@pytest.mark.parametrize("header", ["if close > open", "make() =>"]) +def test_local_persistent_qualified_declaration_keeps_storage(storage, header): + source = f"{header}\n {storage} const float held = 1.5\n" + declaration = Parser(Lexer(source).tokenize(), source=source).parse().body[0].body[0] + assert isinstance(declaration, VarDecl) + assert declaration.annotations["qualifier"] == "const" + assert (declaration.is_var, declaration.is_varip) == ( + storage == "var", storage == "varip", + ) + assert (declaration.loc.line, declaration.loc.col) == (2, 5) + + +@pytest.mark.parametrize("storage", ["var", "varip"]) +def test_library_persistent_const_keeps_constant_provenance(storage): + source = f"{storage} const float held = 1.5\n" + program = Parser(Lexer(source).tokenize(), source=source, library=True).parse() + declaration = program.body[0] + assert declaration.annotations["qualifier"] == "const" + assert declaration.annotations["declared_const"] is True diff --git a/tests/test_requested_na_helper_bindings.py b/tests/test_requested_na_helper_bindings.py new file mode 100644 index 00000000..22de8183 --- /dev/null +++ b/tests/test_requested_na_helper_bindings.py @@ -0,0 +1,52 @@ +"""Requested builtin observers retain their helper's parameter bindings.""" + +import pytest + +from pineforge_codegen import transpile +from tests._compile import compile_cpp +from tests.test_v6_declaration_request_compilation import ( + GLOBAL_TUPLE_HELPER, GLOBAL_TUPLE_HISTORY, TIMEFRAME_HELPER, TIME_HELPER, +) + + +@pytest.mark.parametrize( + "source", [TIMEFRAME_HELPER, GLOBAL_TUPLE_HELPER], + ids=["timeframe-change", "global-tuple"], +) +@pytest.mark.parametrize( + "definition", + [ + "valid(x) => nz(x)", + "valid(float x) => na(x) ? 0 : 1", + "valid(float x) => nz(source = x, replacement = 0.0)", + "valid(x) => x > 0 ? 1 : 0", + "valid(x) => math.abs(x)", + ], + ids=["nz", "typed-na", "typed-named-nz", "comparison-control", "math-control"], +) +def test_observer_bindings_compose_with_requested_context(source, definition): + source = source.replace("valid(x) => na(x) ? 0 : 1", definition, 1) + compile_cpp(transpile(source, check_support=True), label="requested-observer-binding") + + +@pytest.mark.parametrize( + "payload", + ["valid(close)", 'time("W")', 'timeframe.change("W") ? 1 : 0'], + ids=["helper-alone", "time-alone", "change-alone"], +) +def test_independent_requested_payload_parts_remain_compilable(payload): + source = TIME_HELPER.replace('valid(close) * time("W")', payload, 1) + compile_cpp(transpile(source, check_support=True), label="independent-requested-payload") + + +@pytest.mark.parametrize( + "declaration", + [ + '[upper, lower, ignored] = ta.macd(close, 12, 26, 9)', + '[upper, lower] = ta.supertrend(3.0, 10)', + ], + ids=["macd-control", "supertrend-control"], +) +def test_builtin_tuple_member_history_keeps_requested_storage(declaration): + source = GLOBAL_TUPLE_HISTORY.replace("[upper, lower] = channel()", declaration, 1) + compile_cpp(transpile(source, check_support=True), label="requested-builtin-tuple-history") diff --git a/tests/test_security_user_tuple_global.py b/tests/test_security_user_tuple_global.py index 4d856dfa..e0047fa6 100644 --- a/tests/test_security_user_tuple_global.py +++ b/tests/test_security_user_tuple_global.py @@ -4,8 +4,8 @@ bar when a request.security payload reads `g`, directly or through a function that reads the global: the global is a plain value of the requested context. A function with state (a ta.* call, a `var`, a request) advances that state once -per evaluation, so reading its tuple once per element would advance it twice; -that case, and the history of an element, stay refused. +per evaluation. Every element shares its declaration's requested-context +sample, including when the payload reads a member more than once. The value rows hold the requested series to numbers written here from the source and the 1-day alignment of a 15-minute chart (96 chart bars per day), @@ -16,11 +16,12 @@ from __future__ import annotations import math +import re import pytest from pineforge_codegen import transpile -from pineforge_codegen.errors import CompileError +from tests._compile import compile_cpp from tests._e2e import ( Build, chart_feed_head, execute_all, per_bar_mismatches, skip_unless_e2e_env, ) @@ -68,10 +69,19 @@ def test_a_pure_user_tuple_element_translates_in_a_payload(name, declarations, o @pytest.mark.parametrize("name,declarations,payload", STATEFUL, ids=[row[0] for row in STATEFUL]) -def test_a_stateful_user_tuple_element_stays_refused(name, declarations, payload): - with pytest.raises(CompileError, match="an element of a tuple declaration it cannot re-evaluate") as caught: - transpile(_source(declarations, payload, "close", "60")) - assert [d.code for d in caught.value.diagnostics] == ["PF-E3044"] +def test_a_stateful_user_tuple_declaration_is_sampled_once(name, declarations, payload): + cpp = transpile( + _source(declarations, f"{payload} + {payload} + b", "close", "60"), + check_support=True, + ) + evaluator = cpp.split( + "void _eval_security_0(const Bar& bar, bool is_complete) {", 1, + )[1].split("\n }\n", 1)[0] + samples = re.findall(r"const auto (_pf_sec0_tuple_\d+_value) = ", evaluator) + assert len(samples) == 1 + assert evaluator.count(f"std::get<0>({samples[0]})") == 2 + assert f"std::get<1>({samples[0]})" in evaluator + compile_cpp(cpp, label=f"requested-tuple-{name}") def _column(outcome, name): diff --git a/tests/test_v6_declaration_request_compilation.py b/tests/test_v6_declaration_request_compilation.py new file mode 100644 index 00000000..1b9ae392 --- /dev/null +++ b/tests/test_v6_declaration_request_compilation.py @@ -0,0 +1,260 @@ +"""Complete strategy compilation for qualified declarations and request payloads. + +These are compile regressions. TradingView value comparisons belong to the +parity population and are separate from this translation-unit check. +""" + +import pytest + +from pineforge_codegen import transpile +from tests._compile import compile_cpp, skip_if_no_compile_env + + +COLOR_DECLARATION = """//@version=6 +strategy("var const declaration", overlay = true, initial_capital = 10000) +var const color LINE_COLOR = color.blue +fast = ta.ema(close, 10) +slow = ta.ema(close, 30) +plot(fast, color = LINE_COLOR) +if ta.crossover(fast, slow) + strategy.entry("Long", strategy.long, qty = 0.01) +if ta.crossunder(fast, slow) + strategy.close("Long") +""" + +FLOAT_DECLARATION = """//@version=6 +strategy("var const float in a calculation", overlay = true, initial_capital = 10000) +var const float BAND = 0.002 +fast = ta.ema(close, 10) +slow = ta.ema(close, 30) +if fast > slow * (1 + BAND) + strategy.entry("Long", strategy.long, qty = 0.01) +if fast < slow + strategy.close("Long") +""" + +LOWER_TUPLE = """//@version=6 +strategy("security_lower_tf with a tuple", overlay = true, initial_capital = 10000) +[closes, opens] = request.security_lower_tf(syminfo.tickerid, "15", [close, open]) +float body = array.size(closes) > 0 ? array.last(closes) - array.first(opens) : close - open +fast = ta.ema(body, 5) +slow = ta.ema(body, 20) +if ta.crossover(fast, slow) + strategy.entry("Long", strategy.long, qty = 0.01) +if ta.crossunder(fast, slow) + strategy.close("Long") +""" + +MIXED_BLOCKS = """//@version=6 +strategy("Repro", overlay = true, initial_capital = 10000, default_qty_value = 0.01) +fast = ta.ema(close, 9) +slow = ta.ema(close, 21) +if ta.crossover(fast, slow) + if close > open + strategy.entry("Long", strategy.long) +if ta.crossunder(fast, slow) + strategy.entry("Short", strategy.short) +""" + +TIME_HELPER = """//@version=6 +strategy("Repro", overlay = true, initial_capital = 10000, default_qty_value = 0.01) +valid(x) => na(x) ? 0 : 1 +weekStart = request.security(syminfo.tickerid, "D", valid(close) * time("W")) +fast = ta.ema(close, 9) +slow = ta.ema(close, 21) +if ta.crossover(fast, slow) and time >= weekStart + strategy.entry("Long", strategy.long) +if ta.crossunder(fast, slow) + strategy.entry("Short", strategy.short) +""" + +TIMEFRAME_HELPER = """//@version=6 +strategy("Repro", overlay = true, initial_capital = 10000, default_qty_value = 0.01) +valid(x) => na(x) ? 0 : 1 +dailyScore = request.security(syminfo.tickerid, "D", valid(close) + (timeframe.change("W") ? 1 : 0)) +fast = ta.ema(close, 9) +slow = ta.ema(close, 21) +if ta.crossover(fast, slow) and dailyScore == 1 + strategy.entry("Long", strategy.long) +if ta.crossunder(fast, slow) + strategy.entry("Short", strategy.short) +""" + +GLOBAL_TUPLE_HELPER = """//@version=6 +strategy("Repro", overlay = true, initial_capital = 10000, default_qty_value = 0.01) +channel() => + upper = ta.highest(high, 20) + lower = ta.lowest(low, 20) + [upper, lower] +[upper, lower] = channel() +valid(x) => na(x) ? 0 : 1 +score() => valid(close) + valid(upper) +dailyScore = request.security(syminfo.tickerid, "D", score()) +fast = ta.ema(close, 9) +slow = ta.ema(close, 21) +if ta.crossover(fast, slow) and dailyScore == 2 + strategy.entry("Long", strategy.long) +if ta.crossunder(fast, slow) + strategy.entry("Short", strategy.short) +""" + +GLOBAL_TUPLE_HISTORY = """//@version=6 +strategy("Repro", overlay = true, initial_capital = 10000, default_qty_value = 0.01) +channel() => + upper = ta.highest(high, 20) + lower = ta.lowest(low, 20) + [upper, lower] +[upper, lower] = channel() +dailyAbove = request.security(syminfo.tickerid, "D", close > upper[1]) +fast = ta.ema(close, 9) +slow = ta.ema(close, 21) +if ta.crossover(fast, slow) and dailyAbove + strategy.entry("Long", strategy.long) +if ta.crossunder(fast, slow) + strategy.entry("Short", strategy.short) +""" + +UNUSED_FORWARDER = """//@version=6 +strategy("Repro", overlay = true, initial_capital = 10000, default_qty_value = 0.01) +smooth(src, len) => ta.rma(src, len) +smoothTwice(src, len) => smooth(smooth(src, len), len) +fast = smooth(close, 9) +slow = ta.ema(close, 21) +if ta.crossover(fast, slow) + strategy.entry("Long", strategy.long) +if ta.crossunder(fast, slow) + strategy.entry("Short", strategy.short) +""" + + +@pytest.mark.parametrize( + "source", + [ + pytest.param(COLOR_DECLARATION, id="color"), + pytest.param(FLOAT_DECLARATION, id="float"), + pytest.param( + COLOR_DECLARATION.replace("\nvar const ", "\nconst ", 1), + id="color-control", + ), + pytest.param( + FLOAT_DECLARATION.replace("\nvar const ", "\nconst ", 1), + id="float-control", + ), + pytest.param( + COLOR_DECLARATION.replace( + "var const color LINE_COLOR = color.blue", + "varip const int C = 3", + ).replace("color = LINE_COLOR", "color = color.blue"), + id="varip-int", + ), + ], +) +def test_persistent_qualified_declaration_compiles(source): + skip_if_no_compile_env() + compile_cpp(transpile(source, check_support=True), label="persistent-qualified-declaration") + + +@pytest.mark.parametrize( + "declaration", + [ + pytest.param( + '[closes, opens] = request.security_lower_tf(syminfo.tickerid, "15", [close, open])', + id="inline", + ), + pytest.param( + 'closeOpen() =>\n [close, open]\n' + '[closes, opens] = request.security_lower_tf(syminfo.tickerid, "15", closeOpen())', + id="helper", + ), + pytest.param( + 'closes = request.security_lower_tf(syminfo.tickerid, "15", close)\n' + 'opens = request.security_lower_tf(syminfo.tickerid, "15", open)', + id="separate-control", + ), + ], +) +def test_lower_timeframe_tuple_arrays_compile(declaration): + skip_if_no_compile_env() + source = LOWER_TUPLE.replace(LOWER_TUPLE.splitlines()[2], declaration, 1) + compile_cpp(transpile(source, check_support=True), label="lower-timeframe-tuple-arrays") + + +@pytest.mark.parametrize( + "outer,inner", + [ + pytest.param("\t", "\t ", id="tab-then-spaces"), + pytest.param(" ", " \t", id="spaces-then-tab"), + pytest.param("\t", "\t\t", id="tabs-control"), + pytest.param(" ", " ", id="spaces-control"), + ], +) +def test_mixed_nested_block_compiles(outer, inner): + skip_if_no_compile_env() + source = MIXED_BLOCKS.replace( + "\tif close > open\n\t strategy.entry", + outer + "if close > open\n" + inner + "strategy.entry", + 1, + ) + compile_cpp(transpile(source, check_support=True), label="mixed-nested-block") + + +@pytest.mark.parametrize( + "source", + [ + pytest.param(TIME_HELPER, id="time-na"), + pytest.param( + TIME_HELPER.replace("valid(x) => na(x) ? 0 : 1", "valid(x) => nz(x)"), + id="time-nz", + ), + pytest.param( + TIME_HELPER.replace("valid(x)", "valid(float x)", 1), + id="time-typed", + ), + pytest.param( + TIME_HELPER.replace('valid(close) * time("W")', '(na(close) ? 0 : 1) * time("W")'), + id="time-inline-control", + ), + pytest.param(TIMEFRAME_HELPER, id="timeframe-change"), + pytest.param(GLOBAL_TUPLE_HELPER, id="global-tuple-helper"), + pytest.param( + GLOBAL_TUPLE_HELPER.replace( + "[upper, lower] = channel()\nvalid(x)", "valid(x)", 1, + ).replace( + "score() => valid(close) + valid(upper)", + "score() =>\n [upper, lower] = channel()\n valid(close) + valid(upper)", + 1, + ), + id="local-tuple-control", + ), + pytest.param(GLOBAL_TUPLE_HISTORY, id="global-tuple-history"), + pytest.param( + GLOBAL_TUPLE_HISTORY.replace( + "[upper, lower] = channel()", "upper = ta.highest(high, 20)", 1, + ), + id="direct-history-control", + ), + ], +) +def test_requested_helpers_keep_parameter_and_tuple_bindings(source): + skip_if_no_compile_env() + compile_cpp(transpile(source, check_support=True), label="requested-helper-bindings") + + +@pytest.mark.parametrize( + "source", + [ + pytest.param(UNUSED_FORWARDER, id="uncalled"), + pytest.param( + UNUSED_FORWARDER.replace( + "smoothTwice(src, len) => smooth(smooth(src, len), len)\n", "", 1, + ), + id="removed-control", + ), + pytest.param( + UNUSED_FORWARDER + "plot(smoothTwice(close, 9))\n", id="called-control", + ), + ], +) +def test_unreachable_length_forwarding_does_not_allocate_ta(source): + skip_if_no_compile_env() + compile_cpp(transpile(source, check_support=True), label="unused-length-forwarder") diff --git a/tests/test_v6_reporter_controls.py b/tests/test_v6_reporter_controls.py new file mode 100644 index 00000000..2c03dcea --- /dev/null +++ b/tests/test_v6_reporter_controls.py @@ -0,0 +1,48 @@ +"""Previously accepted control forms keep their native compilation contract.""" + +import pytest + +from pineforge_codegen import transpile +from tests._compile import compile_cpp +from tests.test_i372_form_1 import BUILTIN_CONTROL, BUILTIN_SCRIPT, LOCAL_SCRIPT +from tests.test_input_string_var_default import ( + NONDEFAULT_BRANCH, NONDEFAULT_CONTROL, OPTIONS_CONTROL, VARIABLE_OPTIONS, +) +from tests.test_v6_declaration_request_compilation import COLOR_DECLARATION, UNUSED_FORWARDER + + +def test_conditional_history_call_control_keeps_both_written_sites(): + source = LOCAL_SCRIPT.replace( + "wrapper(x) =>\n s = x\n change(s)\nv = wrapper(close)", + "v = close > open ? change(close) : change(open)", + 1, + ) + compile_cpp(transpile(source, check_support=True), label="conditional-history-call-control") + + +@pytest.mark.parametrize("source", [BUILTIN_SCRIPT, BUILTIN_CONTROL]) +def test_builtin_derived_history_control_has_native_types(source): + compile_cpp(transpile(source, check_support=True), label="builtin-derived-history-control") + + +@pytest.mark.parametrize( + "source", + [NONDEFAULT_BRANCH, NONDEFAULT_CONTROL, VARIABLE_OPTIONS, OPTIONS_CONTROL], + ids=["default-var", "default-control", "options-var", "options-control"], +) +def test_reported_string_inputs_keep_native_compilation(source): + compile_cpp(transpile(source, check_support=True), label="string-input-var-control") + + +def test_persistent_declaration_without_qualifier_keeps_its_storage(): + source = COLOR_DECLARATION.replace("\nvar const color ", "\nvar color ", 1) + compile_cpp(transpile(source, check_support=True), label="persistent-color-control") + + +def test_uncalled_direct_ta_length_control_keeps_compiling(): + source = UNUSED_FORWARDER.replace( + "smoothTwice(src, len) => smooth(smooth(src, len), len)", + "unused(src, len) => ta.rma(src, len) * 2", + 1, + ) + compile_cpp(transpile(source, check_support=True), label="unused-direct-length-control")