All files / cnx/utils ConstLiteral.ts

94.11% Statements 48/51
95% Branches 57/60
100% Functions 12/12
95.74% Lines 45/47

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                                          124x 124x         145x           5x   140x                 126x   22x     2x         102x                           140x 84x           56x                             16x 16x               72x   9x           12x 12x     11x   25x 25x     15x                         16x 16x           6x   10x           2x   8x         84x 84x                   12x 12x 11x   11x     12x 12x         7x 7x 7x               25x 25x 24x 24x       34x   24x          
import type IProgram from "../types/IProgram";
import type IVariableSymbol from "../types/symbols/IVariableSymbol";
import type TConstLiteral from "../types/TConstLiteral";
import type TSettledConst from "../types/TSettledConst";
import ConstantFold from "./ConstantFold";
 
/**
 * The C constant a const is written as where its value is used, or null when
 * it has none C can take: the one answer to "is this a const with a value".
 * Render spells the use from it (`PrivateConstValue`), and 2.1 asks it of a
 * case label (E0717), so a label 2.1 accepts is one render can write (#2051
 * review: the two decided it separately, and a string const passed E0717 to
 * reach gcc as `case Panel__S:`).
 *
 * An integer is its literal's notation or its settled digits, a float its
 * value held at its type, a bool `true`/`false`, an enum-typed const its
 * member, a char literal that literal and a header macro named alone that
 * macro. A string, an array or an initializer 1.4 could not settle is null.
 */
class ConstLiteral {
  static textOf(program: IProgram, symbol: IVariableSymbol): string | null {
    const literal = ConstLiteral.of(program, symbol);
    return literal === null ? null : ConstLiteral.spell(literal);
  }
 
  /** The const's answer before C spells it, or null when it has none */
  static of(program: IProgram, symbol: IVariableSymbol): TConstLiteral | null {
    if (
      !ConstantFold.hasCompileTimeValue(symbol) ||
      symbol.isArray ||
      symbol.initialValueExpr === undefined ||
      symbol.initialValue === undefined
    ) {
      return null;
    }
    return ConstLiteral.literalOf(
      program,
      symbol,
      program.settledConstOf(symbol.fullyQualifiedCName),
    );
  }
 
  /** How C writes the answer */
  private static spell(literal: TConstLiteral): string {
    switch (literal.kind) {
      case "member":
        return literal.cName;
      case "macro":
        // parenthesized, as its expansion may not be one operand
        return `(${literal.name})`;
      case "integer":
      case "float":
      case "bool":
      case "char":
        return literal.text;
      default: {
        const unhandled: never = literal;
        return unhandled;
      }
    }
  }
 
  /** The literal the const's settled value is */
  private static literalOf(
    program: IProgram,
    symbol: IVariableSymbol,
    settled: TSettledConst | null,
  ): TConstLiteral | null {
    if (settled?.kind === "value") {
      return {
        kind: "integer",
        digits: settled.digits,
        text: ConstLiteral.integerText(symbol, settled.digits),
      };
    }
    return (
      ConstLiteral.valueLiteral(program, settled) ??
      ConstLiteral.unsettledLiteral(symbol, settled)
    );
  }
 
  /**
   * #1735: how C writes a settled value that has no declaration of its own,
   * such as one value in a static-storage initializer: an integer is its
   * digits. Null when it has no value C can take.
   */
  static textOfValue(
    program: IProgram,
    settled: TSettledConst | null,
  ): string | null {
    const literal = ConstLiteral.valueLiteral(program, settled);
    return literal === null ? null : ConstLiteral.spell(literal);
  }
 
  /** The literal a value is, or null when it has none */
  private static valueLiteral(
    program: IProgram,
    settled: TSettledConst | null,
  ): TConstLiteral | null {
    switch (settled?.kind) {
      case "value":
        return {
          kind: "integer",
          digits: settled.digits,
          text: settled.digits,
        };
      case "float": {
        const text = ConstLiteral.floatText(settled);
        return text === null ? null : { kind: "float", text };
      }
      case "bool":
        return { kind: "bool", text: settled.value ? "true" : "false" };
      case "named": {
        const cName = ConstLiteral.enumMember(program, settled);
        return cName === null ? null : { kind: "member", cName };
      }
      default:
        return null;
    }
  }
 
  /**
   * A constant 1.4 has no value for, which C does: a char literal, and a
   * header macro named alone (parenthesized, as its expansion may not be one
   * operand). Anything else is null.
   */
  private static unsettledLiteral(
    symbol: IVariableSymbol,
    settled: TSettledConst | null,
  ): TConstLiteral | null {
    const expr = symbol.initialValueExpr!;
    if (
      settled?.kind === "notConstant" &&
      settled.reason === "character" &&
      expr.kind === "other" &&
      expr.what === "character"
    ) {
      return { kind: "char", text: symbol.initialValue! };
    }
    if (
      settled?.kind === "foreign" &&
      expr.kind === "name" &&
      expr.root === null &&
      expr.path.length === 1
    ) {
      return { kind: "macro", name: expr.path[0] };
    }
    return null;
  }
 
  /** A literal initializer keeps its notation (`0xAB`); the rest are digits */
  private static integerText(symbol: IVariableSymbol, digits: string): string {
    const expr = symbol.initialValueExpr!;
    return expr.kind === "literal" ? expr.text : digits;
  }
 
  /**
   * The shortest decimal that is the value at its type: an f32's is suffixed,
   * so C reads it as the float the const holds, not as a double.
   */
  private static floatText(
    settled: Extract<TSettledConst, { kind: "float" }>,
  ): string | null {
    const value = Number(settled.digits);
    if (!Number.isFinite(value)) return null;
    Iif (Object.is(value, -0)) return `-0.0${settled.typeName}`;
    const shortest =
      settled.typeName === "f32"
        ? ConstLiteral.shortestF32(value)
        : String(value);
    const spelled = /[.eE]/.test(shortest) ? shortest : `${shortest}.0`;
    return `${spelled}${settled.typeName}`;
  }
 
  /** Nine significant digits always reach a float, so the loop ends */
  private static shortestF32(value: number): string {
    let digits = 1;
    while (Math.fround(Number(value.toPrecision(digits))) !== value) digits++;
    return String(Number(value.toPrecision(digits)));
  }
 
  /** The member of the const's enum that has its value */
  private static enumMember(
    program: IProgram,
    settled: Extract<TSettledConst, { kind: "named" }>,
  ): string | null {
    const enumSymbol = program.symbolByCName(settled.typeName);
    if (enumSymbol?.kind !== "enum") return null;
    const members = [...enumSymbol.members.values()];
    const index = program
      .enumMemberValues(settled.typeName)
      .findIndex(
        (member) =>
          member.kind === "value" && member.value.toString() === settled.digits,
      );
    return index < 0 ? null : members[index].fullyQualifiedCName;
  }
}
 
export default ConstLiteral;