Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | 10x 1x 1x 9x 231x 231x 231x 231x 231x 231x 17x 16x 215x 2x 2x 213x 231x 231x 231x 231x 14x 150x 150x 150x 150x 150x 150x 241x 241x 241x 241x 241x 10x 10x 10x 10x 231x 150x 150x 9x 150x 9x 9x 9x 10x 10x 10x 9x 9x | /**
* StructGenerator - Struct Declaration Generation
*
* Generates C typedef struct declarations from C-Next struct syntax.
*
* Example:
* struct Point { i32 x; i32 y; }
* ->
* typedef struct {
* int32_t x;
* int32_t y;
* } Point;
*
* ADR-029: Structs with callback fields get an auto-generated init function.
* ADR-036: Multi-dimensional array support in struct fields.
*/
import * as Parser from "../../../../logic/parser/grammar/CNextParser";
import IGeneratorInput from "../IGeneratorInput";
import IGeneratorState from "../IGeneratorState";
import IGeneratorOutput from "../IGeneratorOutput";
import IOrchestrator from "../IOrchestrator";
import TGeneratorFn from "../TGeneratorFn";
import TGeneratorEffect from "../TGeneratorEffect";
import ICodeGenSymbols from "../../../../types/ICodeGenSymbols";
import ArrayDimensionUtils from "./ArrayDimensionUtils";
/**
* Generate a callback field declaration for a struct.
*/
function generateCallbackField(
fieldName: string,
callbackInfo: { typedefName: string },
isArray: boolean,
arrayDims: Parser.ArrayDimensionContext[],
orchestrator: IOrchestrator,
): string {
if (isArray) {
const dims = orchestrator.generateArrayDimensions(arrayDims);
return ` ${callbackInfo.typedefName} ${fieldName}${dims};`;
}
return ` ${callbackInfo.typedefName} ${fieldName};`;
}
/**
* Generate a regular (non-callback) field declaration for a struct.
*/
function generateRegularField(
fieldName: string,
structName: string,
member: Parser.StructMemberContext,
isArray: boolean,
arrayDims: Parser.ArrayDimensionContext[],
input: IGeneratorInput,
orchestrator: IOrchestrator,
): string {
const type = orchestrator.generateType(member.type());
// Check for arrayType syntax: u8[16] data -> member.type().arrayType()
// Use optional chaining for mock compatibility in tests
const arrayTypeCtx = member.type().arrayType?.() ?? null;
const arrayTypeDimStr = ArrayDimensionUtils.generateArrayTypeDimension(
arrayTypeCtx,
orchestrator,
);
const hasArrayTypeSyntax = arrayTypeCtx !== null;
// Check if we have tracked dimensions for this field (includes string capacity for string arrays)
const fieldDims = getTrackedFieldDimensions(
input.symbols,
structName,
fieldName,
);
if (fieldDims !== undefined) {
// Use tracked dimensions (includes string capacity for string arrays)
const dimsStr = fieldDims.map((d) => `[${d}]`).join("");
return ` ${type} ${fieldName}${dimsStr};`;
}
if (hasArrayTypeSyntax || isArray) {
// Combine arrayType dimension (if any) with arrayDimension dimensions
const dims = orchestrator.generateArrayDimensions(arrayDims);
return ` ${type} ${fieldName}${arrayTypeDimStr}${dims};`;
}
return ` ${type} ${fieldName};`;
}
/**
* Get tracked field dimensions from symbols if available.
*/
function getTrackedFieldDimensions(
symbols: ICodeGenSymbols | null,
structName: string,
fieldName: string,
): readonly number[] | undefined {
Iif (!symbols) {
return undefined;
}
const trackedDimensions = symbols.structFieldDimensions.get(structName);
const fieldDims = trackedDimensions?.get(fieldName);
return fieldDims && fieldDims.length > 0 ? fieldDims : undefined;
}
/**
* Generate a C typedef struct from a C-Next struct declaration.
*
* Handles:
* - Regular fields with primitive types
* - Callback function pointer fields (ADR-029)
* - Array fields with tracked dimensions (ADR-036)
* - String array fields with capacity tracking
*/
const generateStruct: TGeneratorFn<Parser.StructDeclarationContext> = (
node: Parser.StructDeclarationContext,
input: IGeneratorInput,
_state: IGeneratorState,
orchestrator: IOrchestrator,
): IGeneratorOutput => {
const effects: TGeneratorEffect[] = [];
const name = node.IDENTIFIER().getText();
const callbackFields: Array<{ fieldName: string; callbackType: string }> = [];
const lines: string[] = [];
// Issue #296: Use named struct for forward declaration compatibility
lines.push(`typedef struct ${name} {`);
for (const member of node.structMember()) {
const fieldName = member.IDENTIFIER().getText();
const typeName = orchestrator.getTypeName(member.type());
// ADR-036: arrayDimension() now returns an array for multi-dimensional support
const arrayDims = member.arrayDimension();
const isArray = arrayDims.length > 0;
// ADR-029: Check if this is a callback type field
if (input.callbackTypes.has(typeName)) {
const callbackInfo = input.callbackTypes.get(typeName)!;
callbackFields.push({ fieldName, callbackType: typeName });
// Track callback field for assignment validation via effect
effects.push({
type: "register-callback-field",
key: `${name}.${fieldName}`,
typeName,
});
lines.push(
generateCallbackField(
fieldName,
callbackInfo,
isArray,
arrayDims,
orchestrator,
),
);
} else {
// Regular field handling
lines.push(
generateRegularField(
fieldName,
name,
member,
isArray,
arrayDims,
input,
orchestrator,
),
);
}
}
lines.push(`} ${name};`, "");
// ADR-029: Generate init function if struct has callback fields
if (callbackFields.length > 0) {
lines.push(generateStructInitFunction(name, callbackFields));
}
return {
code: lines.join("\n"),
effects,
};
};
/**
* ADR-029: Generate init function for structs with callback fields.
* Sets all callback fields to their default functions.
*
* Example:
* struct Handler { onEvent callback; }
* ->
* Handler Handler_init(void) {
* return (Handler){
* .onEvent = callback
* };
* }
*/
function generateStructInitFunction(
structName: string,
callbackFields: Array<{ fieldName: string; callbackType: string }>,
): string {
const lines: string[] = [];
lines.push(
`${structName} ${structName}_init(void) {`,
` return (${structName}){`,
);
for (let i = 0; i < callbackFields.length; i++) {
const field = callbackFields[i];
const comma = i < callbackFields.length - 1 ? "," : "";
lines.push(` .${field.fieldName} = ${field.callbackType}${comma}`);
}
lines.push(` };`, `}`, "");
return lines.join("\n");
}
export default generateStruct;
|