Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ supported as exact pairs; on the 0.x line they are independent. See the
method calls, including named arguments. Numeric `na` values sort last in
ascending order and first in descending order; string arrays use lexical
ordering. The source array is unchanged. (#392)
- `request.security` keeps the requested-bar history and declaration-time
value of top-level scalar copies of persistent or reassigned globals,
including copy chains. A declared float copy preserves an integer source's
missing value, and replayed updates use the requested `bar_index`.
Compound assignments in requested selections retain the lexical binding
of their target and its request-owned `var` or `varip` storage.
See [#393](https://github.com/pineforge-4pass/pineforge-codegen-oss/issues/393).

## 1.12.0 — 2026-11-12

Expand Down
171 changes: 166 additions & 5 deletions pineforge_codegen/analyzer/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from __future__ import annotations

from dataclasses import replace
from typing import Any

from ..ast_nodes import (
Expand Down Expand Up @@ -36,7 +37,7 @@
from ..input_keys import assign_input_keys
from ..loop_types import FLOAT_REASSIGNMENT, LoopTypeSources, for_counter_type
from ..security_contexts import (
PASS_WARNINGS_ANNOTATION, context_key, reads_bar_series,
GLOBAL_ANNOTATION, PASS_WARNINGS_ANNOTATION, context_key, reads_bar_series,
)
from ..session_reads import emitted_session_reads
from ..collection_history import (
Expand Down Expand Up @@ -182,6 +183,8 @@ def __init__(self, ast: Program, filename: str = "<stdin>",
self._session_unresolved_method_calls: dict[int, set[str]] = {}
self._symbols = SymbolTable()
self._ta_call_sites: list[TACallSite] = []
# Final analysis consumes this inventory even when no request was seen.
self._security_calls: list[SecurityCallInfo] = []
self._series_vars: set[str] = set()
self._series_var_members: set[str] = set()
self._series_decl_nodes: set[int] = set()
Expand Down Expand Up @@ -731,6 +734,7 @@ def _analyze(self) -> AnalyzerContext:
for name, info in self._global_binding_infos.items()
if info.is_var or name in self._global_reassigned_names
}
self._retain_security_value_copies(mutable_global_infos)

pure_global_expr_map = {
k: v for k, v in self._global_expr_map.items() if k not in mutable_global_infos
Expand Down Expand Up @@ -2467,9 +2471,15 @@ def _collect_security_mutable_globals(
out: set[str] = set()

if isinstance(node, Identifier):
scope = self._identifier_binding_scopes.get(id(node))
if scope != "global" and not (
scope is None and (node.annotations or {}).get(GLOBAL_ANNOTATION)
):
# Parameters and locals do not read a same-spelled global.
return out
name = node.name
if name in self._global_binding_infos:
info = self._global_binding_infos[name]
info = self._global_binding_infos.get(name)
if info is not None:
if info.is_var or name in self._global_reassigned_names:
out.add(name)
if name in resolving:
Expand All @@ -2479,9 +2489,15 @@ def _collect_security_mutable_globals(
out |= self._collect_security_mutable_globals(stmt, resolving)
resolving.remove(name)
return out
if name in self._global_expr_map and name not in resolving:
value = self._global_expr_map.get(name)
if value is None and info is not None and isinstance(info.decl_node, VarDecl):
# The legacy series-name set can omit this global from the
# expression map after a helper parameter reads its history.
# Its declaration still records the call-time dependency.
value = info.decl_node.value
if value is not None and name not in resolving:
resolving.add(name)
out |= self._collect_security_mutable_globals(self._global_expr_map[name], resolving)
out |= self._collect_security_mutable_globals(value, resolving)
resolving.remove(name)
return out

Expand Down Expand Up @@ -2530,6 +2546,147 @@ def _collect_security_mutable_globals(

return out

def _retain_security_value_copies(
self, mutable_infos: dict[str, MutableGlobalInfo],
) -> None:
"""Replay scalar copies whose value cannot be substituted in a request.

A history read needs the copy's own requested-bar samples. A copy made
before a later store to its source also needs its declaration replayed
at its original position, even without history. Chart storage and
helper-local bindings keep their independently analyzed identities.
"""
if not self._security_calls or not mutable_infos:
return
budget = getattr(self, "_budget", None)
positions = {id(stmt): index for index, stmt in enumerate(self._ast.body)}

def is_global(node):
scope = self._identifier_binding_scopes.get(id(node))
annotations = getattr(node, "annotations", None) or {}
return scope == "global" or (
scope is None and bool(annotations.get(GLOBAL_ANNOTATION))
)

aliases = {}
last_write = {}
primitive = {PineType.FLOAT, PineType.INT, PineType.BOOL,
PineType.STRING, PineType.COLOR}
for name, info in self._global_binding_infos.items():
if budget is not None:
budget.check(phase=Phase.ANALYZER)
last_write[name] = max(
(positions.get(id(stmt), -1) for stmt in info.source_stmts),
default=-1,
)
decl = info.decl_node
if (name in mutable_infos or info.pine_type not in primitive
or not isinstance(decl, VarDecl) or id(decl) not in positions
or not isinstance(decl.value, Identifier) or not is_global(decl.value)):
continue
source = self._global_binding_infos.get(decl.value.name)
if source is None or source.pine_type not in primitive:
continue
binding = self._identifier_decl_bindings.get(id(decl.value))
if (binding is not None and source.decl_node is not None
and binding[0] != id(source.decl_node)):
continue
aliases[name] = source.name

# Memoize chains rather than walking once per path through a diamond.
rooted = dict.fromkeys(mutable_infos, True)
for name in aliases:
path, seen = [], set()
current = name
while current in aliases and current not in rooted and current not in seen:
if budget is not None:
budget.check(phase=Phase.ANALYZER)
seen.add(current)
path.append(current)
current = aliases[current]
reaches_state = rooted.get(current, False)
for alias in path:
rooted[alias] = reaches_state
aliases = {name: source for name, source in aliases.items() if rooted[name]}
if not aliases:
return
snapshots = {
name for name, source in aliases.items()
if last_write[source] > positions[id(self._global_binding_infos[name].decl_node)]
}

def dependencies(root):
pending, seen, names, history = [root], set(), set(), set()
while pending:
if budget is not None:
budget.check(phase=Phase.ANALYZER)
node = pending.pop()
if not isinstance(node, ASTNode) or id(node) in seen:
continue
seen.add(id(node))
if (isinstance(node, Subscript) and isinstance(node.object, Identifier)
and is_global(node.object) and node.object.name in aliases):
history.add(node.object.name)
if isinstance(node, Identifier) and is_global(node):
name = node.name
if name in aliases or name in mutable_infos:
names.add(name)
pending.extend(self._global_binding_infos[name].source_stmts)
elif name in self._global_expr_map:
pending.append(self._global_expr_map[name])
if isinstance(node, FuncCall):
if self._is_input_func_call(node):
# Cosmetic input arguments are not data dependencies.
pending.append(node.args[0] if node.args else node.kwargs.get("defval"))
continue
if isinstance(node.callee, Identifier):
function = self._func_defs.get(node.callee.name)
if function is not None:
pending.extend(function.body)
pending.extend(syntax_children(node))
return names, history

required, histories = set(), set()
requests = []
for call in self._security_calls:
names, history = dependencies(call.expression)
histories.update(history)
required.update(names & snapshots)
requests.append((call, names))
required.update(histories)
if not required:
return
for name in sorted(required):
info = self._global_binding_infos[name]
mutable_infos[name] = replace(info, is_series=info.is_series or name in histories)
sources = {}
for name in sorted(required):
chain = set()
source = name
while source in aliases and source not in chain:
if budget is not None:
budget.check(phase=Phase.ANALYZER)
chain.add(source)
source = aliases[source]
chain.add(source)
# The original call-time inventory already bounds which inputs
# of a source's updates are replayed. Retaining a copy must not
# widen that boundary to writes discovered later in the script.
sources[name] = chain & mutable_infos.keys()
for call, names in requests:
# Preserve each request's original replay boundary. A helper read
# elsewhere in its payload must not pull later chart-only writes
# into the request merely because another operand copies state.
original = set(getattr(call, "mutable_globals", ()) or ())
additions = set()
for name in names & required:
# Newly retained aliases need their own replay. A pre-existing
# mutable source participates only if this call already read
# it as mutable; its first later write is not retroactive.
additions.update(sources[name] & (original | required))
call.mutable_globals = tuple(sorted(original | additions))
call.depends_on_mutable_globals = bool(call.mutable_globals)

def _session_history_unsafe_functions(
self, func_defs: dict, find_calls, known_func_names: set[str],
calls_by_parent: dict, call_edges: list) -> dict[str, str]:
Expand Down Expand Up @@ -5588,6 +5745,10 @@ def _visit_Assignment(self, node: Assignment) -> PineType:
node.loc or node.target.loc,
)
else:
# Retain the target's lexical binding just like a read.
# A compound store may have no other read of its target
# to expose the dependency to a requested expression.
self._visit(node.target)
if (node.op in (":=", "+=", "-=", "*=", "/=", "%=")
and self._pine_version == 6
and not self._in_v5_library_definition(node)
Expand Down
9 changes: 3 additions & 6 deletions pineforge_codegen/analyzer/call_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
index used to mint unique ``_ta_<func>_<n>`` member names.
- ``self._series_bar_fields`` (``set[str]``): bar-field identifiers
used as TA inputs anywhere in the program.
- ``self._security_calls`` (``list[SecurityCallInfo]``): created on
first ``request.security(...)`` via ``getattr(..., "_security_calls",
[])`` and stored back on ``self`` -- the analyzer also reads this
attribute via ``getattr`` in ``analyze()``.
- ``self._security_calls`` (``list[SecurityCallInfo]``): a per-analyzer
inventory initialized empty by the host constructor. Both security
handlers append to it; final analysis also consumes the empty inventory.
- ``self._fixnan_counter`` / ``self._fixnan_sites``: ``fixnan(...)``
member-name counter + site list.
- ``self._symbols`` (``SymbolTable``): consulted by
Expand Down Expand Up @@ -859,7 +858,6 @@ def _handle_request_call(self, func_name: str, node: FuncCall) -> PineType:
security_ta_range = (ta_start, ta_end) if ta_end > ta_start else None

# Assign ID and record the call
self._security_calls = getattr(self, "_security_calls", [])
sec_id = len(self._security_calls)

returns_tuple = isinstance(expr_node, TupleLiteral)
Expand Down Expand Up @@ -1095,7 +1093,6 @@ def _handle_request_security_lower_tf(self, node: FuncCall) -> PineType:
node.loc,
)

self._security_calls = getattr(self, "_security_calls", [])
sec_id = len(self._security_calls)

mutable_globals = tuple(sorted(self._collect_security_mutable_globals(expr_node)))
Expand Down
70 changes: 44 additions & 26 deletions pineforge_codegen/codegen/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -4830,7 +4830,8 @@ def _security_expr_hist_series_names(self, sec_id: int) -> list[str]:
def _emit_security_expr_hist_members(
self, sec_id: int, expr_node, lines: list[str], mbb_suffix: str
) -> None:
if self._security_reads_bar_index(expr_node):
mutable_names = self._security_eval_info[sec_id].get("mutable_globals") or ()
if self._security_reads_bar_index(expr_node, mutable_names):
# The requested bar's ``bar_index``: one count per requested bar,
# advanced where the evaluator opens its slot.
self._security_bar_index_secs.add(sec_id)
Expand Down Expand Up @@ -7878,6 +7879,11 @@ def _emit_security_rebind_var_decl(
emitted_lines=emitted_lines,
)
expr_cpp = self._security_copy_store_cpp(node.name, info, node.value, expr_cpp)
cpp_type = self._security_cpp_type_for_mutable(node.name, info)
if node.value is None or isinstance(node.value, NaLiteral) or cpp_type == "double":
# A replayed declaration stores into its declared destination:
# bare na has that type, and a float copy preserves integer na.
expr_cpp = self._security_local_value(expr_cpp, node.value, cpp_type, ())

if getattr(info, "is_var", False):
if getattr(info, "is_series", False):
Expand Down Expand Up @@ -10783,8 +10789,7 @@ def walk(value, env):
types[current] = ("std::string" if series in self._security_string_series
else "double")
elif value.target.name in args[4] and bound is None:
# Assignment targets are resolved directly by the analyzer,
# so unlike reads they need not carry identifier provenance.
# The checked outer binding writes its request-owned state.
info = self._global_mutable_infos[value.target.name]
state = self._security_state_name(args[0], value.target.name)
current = f"{state}[0]" if info.is_series else state
Expand Down Expand Up @@ -11697,35 +11702,48 @@ def _security_is_bar_index(self, node) -> bool:
return (isinstance(node, Identifier) and node.name == "bar_index"
and "bar_index" not in getattr(self, "_safe_name_bound", ()))

def _security_reads_bar_index(self, expr_node) -> bool:
def _security_reads_bar_index(self, expr_node, mutable_names=()) -> bool:
"""Whether a payload reads ``bar_index``: in itself, the globals it
reads or the user functions it calls. TradingView evaluates it on
the requested bar -- the count of requested bars before it -- where
the evaluator used to read the chart's (``pine_bar_index()``)."""
from ..limits import iter_ast_nodes

reads, replayed state or the user functions it calls. Follow only
this request's mutable inventory, so retaining a copy does not
discover updates outside its original source boundary."""
global_expr_map = getattr(self.ctx, "global_expr_map", {}) or {}
mutable_infos = getattr(self, "_global_mutable_infos", None) or {}
mutable_names = set(mutable_names or ())
func_names = getattr(self, "_func_names", None) or ()
func_infos = getattr(self, "_func_info_map", None) or {}
budget = getattr(self, "_budget", None)
seen: set[tuple[str, str]] = set()
visited: set[int] = set()
pending = [expr_node]
while pending:
root = pending.pop()
if not isinstance(root, ASTNode):
node = pending.pop()
if not isinstance(node, ASTNode) or id(node) in visited:
continue
for n, _depth in iter_ast_nodes(root):
if self._security_is_bar_index(n):
return True
if (isinstance(n, Identifier) and n.name in global_expr_map
and ("global", n.name) not in seen
and self._security_identifier_is_global_binding(n)):
seen.add(("global", n.name))
pending.append(global_expr_map[n.name])
if (isinstance(n, FuncCall) and isinstance(n.callee, Identifier)
and n.callee.name in self._func_names
and ("func", n.callee.name) not in seen):
seen.add(("func", n.callee.name))
info = self._func_info_map.get(n.callee.name)
if info is not None and getattr(info, "node", None) is not None:
pending.extend(info.node.body)
visited.add(id(node))
if budget is not None:
budget.check(getattr(node, "loc", None), Phase.CODEGEN)
if self._security_is_bar_index(node):
return True
if (isinstance(node, Identifier)
and self._security_identifier_is_global_binding(node)):
key = ("global", node.name)
if key not in seen:
seen.add(key)
if node.name in mutable_names:
info = mutable_infos.get(node.name)
pending.extend(getattr(info, "source_stmts", None) or ())
elif node.name in global_expr_map:
pending.append(global_expr_map[node.name])
if (isinstance(node, FuncCall) and isinstance(node.callee, Identifier)
and node.callee.name in func_names
and ("func", node.callee.name) not in seen):
seen.add(("func", node.callee.name))
info = func_infos.get(node.callee.name)
function = getattr(info, "node", None)
if function is not None:
pending.extend(function.body or ())
pending.extend(syntax_children(node))
return False

def _security_variant_order_key(self, signature: tuple, binding_stack) -> tuple:
Expand Down
Loading
Loading