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 | 47x 47x 1482x 1482x 1527x 45x 1482x 1482x 1482x 1482x 47x 2020x 2020x 2020x 92x 92x 1928x 2x 2x 2x 2x 1926x 1926x 46x 46x 1880x 2x 2x 1878x 1x 1x 1877x 2x 1877x 2020x 1482x 1482x 7x 1475x 1256x 1877x | /**
* Literal Generator
*
* Generates C code for literal values:
* - Boolean literals (true/false) → needs stdbool.h
* - Float literals with C-Next suffixes (f32 → f, f64 → no suffix)
* - Integer literals with C-Next suffixes (u64 → ULL, i64 → LL, strip 8/16/32)
* - MISRA Rule 7.2: Unsigned suffix for unsigned integer types
* - String and numeric literals pass through unchanged
*/
import IGeneratorOutput from "../IGeneratorOutput";
import TGeneratorEffect from "../TGeneratorEffect";
import IGeneratorState from "../IGeneratorState";
import NarrowingCastHelper from "../../helpers/NarrowingCastHelper";
import LiteralUtils from "../../../../../utils/LiteralUtils";
import type TranspileState from "../../../../TranspileState";
/**
* Unsigned type patterns for MISRA Rule 7.2 compliance.
* Includes both C-Next types (u8, u16, u32, u64) and C types (uint8_t, etc.)
*/
const UNSIGNED_64_TYPES = new Set(["u64", "uint64_t"]);
const UNSIGNED_TYPES = new Set([
"u8",
"u16",
"u32",
"uint8_t",
"uint16_t",
"uint32_t",
"size_t", // Array indices (MISRA 7.2)
]);
/**
* Resolve typedef aliases to their underlying type.
* For C typedef'd types (e.g., "byte_t" -> "uint8_t"), look up the symbol table.
*/
function resolveTypedef(typeName: string, state: TranspileState): string {
const underlyingType = state.getTypedefType(typeName);
return underlyingType ?? typeName;
}
/**
* Check if a literal is a numeric integer (decimal, hex, octal, or binary).
* Excludes strings, floats, and booleans.
*/
function isNumericIntegerLiteral(text: string): boolean {
// Exclude strings (quoted)
if (text.startsWith('"') || text.startsWith("'")) {
return false;
}
// Exclude booleans
Iif (text === "true" || text === "false") {
return false;
}
// Exclude floats -- the one decision of what a floating literal is (#1668)
Iif (LiteralUtils.floatLiteralWidth(text) !== null) {
return false;
}
// Must start with digit or be hex/binary/octal
return /^\d/.test(text) || /^0[xXbBoO]/.test(text);
}
/**
* Check if a literal already has a C unsigned suffix (U, UL, ULL).
*/
function hasUnsignedSuffix(text: string): boolean {
return /[uU]([lL]{0,2})$/.test(text);
}
/**
* Generate C code for a literal value.
*
* Takes the literal's TEXT, not its parse node (#1445). Every branch below
* reads the text and `state.expectedType` and nothing else -- the node was
* only ever `node.getText()` -- so naming `LiteralContext` here bought a
* dependency on the grammar for a string. This is the first render-layer
* module to stop holding a parse context, and the shape the rest follow: the
* IR node for a literal IS its text, so the caller extracts it and this
* function can be reached, and tested, without a parser.
*
* @param text - The literal's source text, e.g. `3.14f32`, `true`, `'A'`
* @param state - Current generation state (contains expectedType)
* @returns Generated code and effects (stdbool include for bool literals)
*/
const generateLiteral = (
text: string,
state: IGeneratorState,
transpileState: TranspileState,
): IGeneratorOutput => {
const effects: TGeneratorEffect[] = [];
let literalText = text;
// Track boolean literal usage to include stdbool.h
if (literalText === "true" || literalText === "false") {
effects.push({ type: "include", header: "stdbool" });
return { code: literalText, effects };
}
// MISRA 10.3: Character literals have type int in C
// When assigning to narrower types (u8, i8, u16, i16), add explicit cast
if (literalText.startsWith("'")) {
const expectedType = state?.expectedType;
Eif (expectedType) {
const wrappedCode = NarrowingCastHelper.wrap(
literalText,
"int",
expectedType,
transpileState,
);
return { code: wrappedCode, effects };
}
return { code: literalText, effects };
}
// ADR-024: Transform C-Next float suffixes to standard C syntax
// 3.14f32 -> 3.14f (C float)
// 3.14f64 -> 3.14 (C double, no suffix needed)
// #1668: asked of the whole literal, not of its last three characters --
// the hex integer 0xFF32 ends in `F32` too, and was emitted as 0xFf.
const floatWidth = LiteralUtils.floatLiteralWidth(literalText);
if (floatWidth !== null) {
literalText = literalText.replace(
/[fF](32|64)$/,
floatWidth === 32 ? "f" : "",
);
return { code: literalText, effects };
}
// Issue #130: Transform C-Next integer suffixes to standard C syntax
// u8/u16/u32 and i8/i16/i32 suffixes are stripped (C infers from context)
// u64 -> ULL suffix for 64-bit unsigned
// i64 -> LL suffix for 64-bit signed
if (/[uU]64$/.test(literalText)) {
literalText = literalText.replace(/[uU]64$/, "ULL");
return { code: literalText, effects };
}
if (/[iI]64$/.test(literalText)) {
literalText = literalText.replace(/[iI]64$/, "LL");
return { code: literalText, effects };
}
if (/[uUiI](8|16|32)$/.test(literalText)) {
// Strip 8/16/32-bit suffixes - will add U below if needed for MISRA
literalText = literalText.replace(/[uUiI](8|16|32)$/, "");
}
// MISRA Rule 7.2: Add unsigned suffix based on expectedType
// Only applies to numeric integer literals without existing unsigned suffix
const expectedType = state?.expectedType;
if (
expectedType &&
isNumericIntegerLiteral(literalText) &&
!hasUnsignedSuffix(literalText)
) {
// Resolve typedef aliases (e.g., "byte_t" -> "uint8_t")
const resolvedType = resolveTypedef(expectedType, transpileState);
if (UNSIGNED_64_TYPES.has(resolvedType)) {
literalText = literalText + "ULL";
} else if (UNSIGNED_TYPES.has(resolvedType)) {
literalText = literalText + "U";
}
}
return { code: literalText, effects };
};
export default generateLiteral;
|