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 1/2] 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 2/2] 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":