Class ToJsonGenerator
java.lang.Object
org.ek9lang.compiler.phase7.synthesis.AbstractSyntheticGenerator
org.ek9lang.compiler.phase7.synthesis.ToJsonGenerator
Generates synthetic IR for the _json ($$) operator.
The generated code follows this pattern:
JSON _json():
// Check if any field is set using Bits._empty()
status = this._fieldSetStatus() // Returns Bits
isEmpty = status._empty() // Returns Boolean (true if no fields set)
if isEmpty._true(): goto return_unset
// has_data: at least one field is set
jsonTemp = JSON()
result = jsonTemp.object()
// For each field:
fieldValue = LOAD this.field
fieldJson = fieldValue._json() // Recursive JSON conversion
nameString = "fieldName" (literal)
pair = JSON(nameString, fieldJson) // Create name/value pair
result._merge(pair) // Merge into result
return result
return_unset:
return new JSON() // Unset JSON
Semantic: If ALL fields are unset, the object has no meaningful data, so we return an unset JSON. If ANY field is set, we proceed with normal JSON construction. This is consistent with _string and _hashcode semantics.
Key semantic requirements:
- Returns unset JSON if all fields are unset
- Returns set JSON if any field is set
- Format: {"field1": value1, "field2": value2}
- Each field value is converted via $$ (_json) recursively
- Unset field values become JSON null (handled by JSON constructor)
- Lists produce JSON arrays, Dicts produce nested objects
Uses Bits._empty() instead of N individual _isSet() calls for efficiency.
-
Field Summary
Fields inherited from class AbstractSyntheticGenerator
stackContext -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongenerate(MethodSymbol operatorSymbol, AggregateSymbol aggregateSymbol) Generate the _json operator IR for the given aggregate.Methods inherited from class AbstractSyntheticGenerator
createDebugInfo, generateBooleanReturnBlock, generateConstructorCall, generateConstructorCallWithArgs, generateFieldLoad, generateIsSetGuard, generateLabelName, generateMethodCall, generateStringLiteralLoad, generateTempName, generateThisIsSetGuard, generateUnsetReturnBlock, generateUnsetReturnBlockWithLabel, getBitsTypeName, getBooleanTypeName, getIntegerTypeName, getStringTypeName, getSuperTypeName, getSyntheticFields, getTypeName, getVoidTypeName, isAnyType, superHasOperator
-
Constructor Details
-
ToJsonGenerator
ToJsonGenerator(IRGenerationContext stackContext)
-
-
Method Details
-
generate
Generate the _json operator IR for the given aggregate.- Parameters:
operatorSymbol- The _json operator symbolaggregateSymbol- The aggregate containing the operator- Returns:
- List of IR instructions implementing the operator
-