diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fe3ef1c..2be5e189 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,14 @@ supported as exact pairs; on the 0.x line they are independent. See the 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. +- 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. @@ -67,6 +75,12 @@ supported as exact pairs; on the 0.x line they are independent. See the bytes: the timezone macro block and newly reserved names remain. Regenerate C++ and relink affected strategies. +- 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) +- 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/docs/reference/support-contracts.md b/docs/reference/support-contracts.md index cf55d2f5..c1278091 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 6c1ec40c..d30159be 100644 --- a/pineforge_codegen/analyzer/base.py +++ b/pineforge_codegen/analyzer/base.py @@ -311,6 +311,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 @@ -771,6 +778,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() @@ -3372,6 +3397,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({ @@ -3445,6 +3516,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 @@ -3452,6 +3526,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 @@ -5540,6 +5627,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" @@ -5589,6 +5679,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 = ( @@ -5638,10 +5734,24 @@ 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) - 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]) @@ -5849,6 +5959,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, { @@ -6998,6 +7120,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] @@ -7052,7 +7182,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 ), ) @@ -7060,7 +7190,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 @@ -7068,7 +7198,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"}: @@ -7090,7 +7220,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: @@ -7112,7 +7242,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 @@ -7150,7 +7280,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. @@ -7163,7 +7293,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 = { @@ -8416,35 +8546,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), @@ -8453,6 +8589,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): @@ -8497,7 +8635,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, @@ -8551,10 +8689,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 @@ -8564,6 +8711,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": @@ -8590,7 +8747,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 @@ -8598,6 +8757,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..fb44adac 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, @@ -1924,6 +1942,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 +2190,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 5e6d7a99..1c19d353 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 @@ -317,7 +317,7 @@ def _selection_type_spec( *, nullable: bool | None = None, ) -> TypeSpec | None: - """Keep exact collection joins; extend other families only around ``na``.""" + """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 @@ -330,7 +330,12 @@ def _selection_type_spec( spec = TypeSpec.primitive("color") if spec is None: return None - if not nullable and spec.kind not in {"map", "matrix"}: + 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 @@ -545,6 +550,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 @@ -904,6 +914,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, @@ -1469,14 +1500,121 @@ 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) + 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): + 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._operand_type(value) + return spec + def _check_node(self, node): if not isinstance(node, ASTNode): if isinstance(node, (tuple, list)): @@ -1571,11 +1709,34 @@ 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): + 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 if (isinstance(value, MemberAccess) and isinstance(value.object, Identifier) and self._pine_version == 6 @@ -1598,7 +1759,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) @@ -1607,7 +1768,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 21e0785a..ca0dc0db 100644 --- a/pineforge_codegen/codegen/base.py +++ b/pineforge_codegen/codegen/base.py @@ -806,6 +806,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 @@ -2075,6 +2076,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. @@ -2107,6 +2109,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 @@ -2156,6 +2159,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 @@ -2166,6 +2189,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 @@ -2185,6 +2209,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: @@ -2232,6 +2257,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, @@ -2241,6 +2270,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. @@ -2547,6 +2654,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 ): @@ -2568,7 +2676,14 @@ def _register_global_aggregate_member_types(self) -> None: self._array_vars.add(stmt.name) self._collection_types.setdefault(stmt.name, declared_spec) continue + 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"): @@ -2584,7 +2699,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): @@ -5624,17 +5738,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, ()) @@ -6311,6 +6435,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/emit_top.py b/pineforge_codegen/codegen/emit_top.py index 675ff411..53ea10ee 100644 --- a/pineforge_codegen/codegen/emit_top.py +++ b/pineforge_codegen/codegen/emit_top.py @@ -2422,6 +2422,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 @@ -2669,6 +2700,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( @@ -2682,8 +2718,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/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/types.py b/pineforge_codegen/codegen/types.py index 62f35edd..0a649e1b 100644 --- a/pineforge_codegen/codegen/types.py +++ b/pineforge_codegen/codegen/types.py @@ -3073,6 +3073,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/pineforge_codegen/codegen/visit_stmt.py b/pineforge_codegen/codegen/visit_stmt.py index e367346c..b09f4c4b 100644 --- a/pineforge_codegen/codegen/visit_stmt.py +++ b/pineforge_codegen/codegen/visit_stmt.py @@ -1711,7 +1711,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 {}): @@ -1768,6 +1768,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_compound_type_flow.py b/tests/test_callable_compound_type_flow.py new file mode 100644 index 00000000..791e82a6 --- /dev/null +++ b/tests/test_callable_compound_type_flow.py @@ -0,0 +1,297 @@ +"""Structured values retain their types across callable boundaries.""" + +import re + +import pytest + +from pineforge_codegen import transpile +from tests._compile import compile_cpp, run_emitted_tu + + +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") + # 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", + ] + 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 + 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(): + 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+(PineMatrix|double) working(?: = [^\n;]*)?;$", + cpp, re.MULTILINE, + ) + 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(): + 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") 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")