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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | 2576x 2576x 159x 2417x 7x 2410x 2408x 2576x 2576x 16x 2x 14x 10x 23x 3x 20x 18x 20x 253x 2x 251x 2x 249x 9x 4x 5x 4x 2x 2x 1x 52x 3072x 1283x 1283x 1283x 1789x 51x 1738x 14x 14x 1724x 9x 9x 1715x 18x 36x 18x 18x 1697x 250x 250x 250x 1447x 3x 3x 3x 3x 3x 1444x 1443x 1x 3061x 1284x 1284x 1777x 51x 1726x | /**
* TypeGenerationHelper
*
* Helper class for generating C type strings from C-Next type contexts.
* Handles primitive types, scoped types, qualified types, user types, and array types.
*
* Extracted from CodeGenerator._generateType for improved testability.
*/
import * as Parser from "../../../logic/parser/grammar/CNextParser.js";
import TYPE_MAP from "../types/TYPE_MAP.js";
import TIncludeHeader from "../generators/TIncludeHeader.js";
/**
* Result of generating a primitive type.
*/
interface IPrimitiveTypeResult {
cType: string;
include: TIncludeHeader | null;
}
/**
* Dependencies required for type generation that involve external state.
*/
interface ITypeGenerationDeps {
currentScope: string | null;
isCppScopeSymbol: (name: string) => boolean;
checkNeedsStructKeyword: (name: string) => boolean;
validateCrossScopeVisibility: (scope: string, member: string) => void;
}
class TypeGenerationHelper {
/**
* Generate C type for a primitive type.
* Returns the C type and any required include header.
*/
static generatePrimitiveType(type: string): IPrimitiveTypeResult {
let include: TIncludeHeader | null = null;
if (type === "bool") {
include = "stdbool";
} else if (type === "ISR") {
include = "isr";
} else if (type in TYPE_MAP && type !== "void") {
include = "stdint";
}
const cType = TYPE_MAP[type] || type;
return { cType, include };
}
/**
* Generate C type for a scoped type (this.Type).
* Throws if called outside a scope context.
*/
static generateScopedType(
typeName: string,
currentScope: string | null,
): string {
if (!currentScope) {
throw new Error("Cannot use 'this.Type' outside of a scope");
}
return `${currentScope}_${typeName}`;
}
/**
* Generate C type for a global type (global.Type).
*/
static generateGlobalType(typeName: string): string {
return typeName;
}
/**
* Generate C type for a qualified type (Scope.Type or Namespace::Type).
*
* @param identifiers - Array of identifier names in the qualified path
* @param isCppNamespace - Whether the first identifier is a C++ namespace
* @param validateVisibility - Optional callback to validate cross-scope visibility
* @returns The C/C++ type string
*/
static generateQualifiedType(
identifiers: string[],
isCppNamespace: boolean,
validateVisibility?: (scope: string, member: string) => void,
): string {
if (isCppNamespace) {
return identifiers.join("::");
}
// C-Next scoped type - validate visibility for 2-part types
if (identifiers.length === 2 && validateVisibility) {
validateVisibility(identifiers[0], identifiers[1]);
}
return identifiers.join("_");
}
/**
* Generate C type for a user-defined type.
*
* @param typeName - The type name
* @param needsStructKeyword - Whether to prefix with 'struct'
* @returns The C type string
*/
static generateUserType(
typeName: string,
needsStructKeyword: boolean,
): string {
// ADR-046: cstring maps to char* for C library interop
if (typeName === "cstring") {
return "char*";
}
if (needsStructKeyword) {
return `struct ${typeName}`;
}
return typeName;
}
/**
* Generate base type for an array type.
*
* @param primitiveText - The primitive type text (if primitive)
* @param userTypeName - The user type name (if user type)
* @param needsStructKeyword - Whether to prefix with 'struct'
* @returns The C base type string
*/
static generateArrayBaseType(
primitiveText: string | null,
userTypeName: string | null,
needsStructKeyword: boolean,
): string {
if (primitiveText) {
return TYPE_MAP[primitiveText] || primitiveText;
}
if (userTypeName) {
if (needsStructKeyword) {
return `struct ${userTypeName}`;
}
return userTypeName;
}
throw new Error("Array type must have either primitive or user type");
}
/**
* Generate string type (bounded strings).
* Returns the base type for char arrays.
*/
static generateStringType(): string {
return "char";
}
/**
* Full type generation using all dependencies.
* This is the main entry point that handles all type contexts.
*/
static generate(ctx: Parser.TypeContext, deps: ITypeGenerationDeps): string {
// Primitive type
if (ctx.primitiveType()) {
const type = ctx.primitiveType()!.getText();
const result = TypeGenerationHelper.generatePrimitiveType(type);
// Note: caller is responsible for handling the include
return result.cType;
}
// Bounded string type
if (ctx.stringType()) {
return TypeGenerationHelper.generateStringType();
}
// Scoped type (this.Type)
if (ctx.scopedType()) {
const typeName = ctx.scopedType()!.IDENTIFIER().getText();
return TypeGenerationHelper.generateScopedType(
typeName,
deps.currentScope,
);
}
// Global type (global.Type)
if (ctx.globalType()) {
const typeName = ctx.globalType()!.IDENTIFIER().getText();
return TypeGenerationHelper.generateGlobalType(typeName);
}
// Qualified type (Scope.Type or Namespace::Type)
if (ctx.qualifiedType()) {
const identifiers = ctx.qualifiedType()!.IDENTIFIER();
const identifierNames = identifiers.map((id) => id.getText());
const isCpp = deps.isCppScopeSymbol(identifierNames[0]);
return TypeGenerationHelper.generateQualifiedType(
identifierNames,
isCpp,
deps.validateCrossScopeVisibility,
);
}
// User type
if (ctx.userType()) {
const typeName = ctx.userType()!.getText();
const needsStruct = deps.checkNeedsStructKeyword(typeName);
return TypeGenerationHelper.generateUserType(typeName, needsStruct);
}
// Array type
if (ctx.arrayType()) {
const arrCtx = ctx.arrayType()!;
const primitiveText = arrCtx.primitiveType()?.getText() ?? null;
const userTypeName = arrCtx.userType()?.getText() ?? null;
const needsStruct = userTypeName
? deps.checkNeedsStructKeyword(userTypeName)
: false;
return TypeGenerationHelper.generateArrayBaseType(
primitiveText,
userTypeName,
needsStruct,
);
}
// Void or fallback
if (ctx.getText() === "void") {
return "void";
}
return ctx.getText();
}
/**
* Get the required include header for a type context.
* Used by the caller to track includes separately from type generation.
*/
static getRequiredInclude(ctx: Parser.TypeContext): TIncludeHeader | null {
if (ctx.primitiveType()) {
const type = ctx.primitiveType()!.getText();
return TypeGenerationHelper.generatePrimitiveType(type).include;
}
if (ctx.stringType()) {
return "string";
}
return null;
}
}
export default TypeGenerationHelper;
|