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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | 50x 572x 572x 572x 572x 572x 584x 105x 105x 105x 104x 102x 102x 102x 113x 6x 96x 103x 105x 10x 10x 10x 110x 110x 1884x 1884x 1775x 1x 16125x 16125x 576x 752x 752x 752x 705x 673x 109x 109x 1542x 1542x 1542x 1542x 14241x 14241x 12699x 12699x 12699x 14241x 14241x 1542x 1542x 730x 730x 730x 812x 304x 508x 33x 33x 475x 322x 153x 153x 1x 305x 305x 305x 305x 297x 297x 322x 322x 754x 322x 153x 153x 153x 153x 153x 153x | /**
* The struct a `{ field: value }` initializer builds.
*
* #1322. An explicit `Point { x: 1 }` says so; an inferred `{ x: 1 }` is typed
* by where it stands -- a declaration's type, the field it initializes in an
* enclosing initializer, the element type of the array it sits in, the
* assignment target, the parameter it is passed to, or the enclosing
* function's return type. Codegen typed the first three through its
* `expectedType` control flow and could not type the last (#1277); pass 2.1
* reads the parse tree, where every establishing node is a parent away.
*
* Answers with the C name the per-file view keys struct fields by, so a
* caller can go straight to `structFields`.
*/
import { ParserRuleContext } from "antlr4ng";
import * as Parser from "../../../PARSE/2-Parse/grammar/CNextParser";
import TypeResolver from "../../../utils/TypeResolver";
import OperandTyper from "../../../utils/OperandTyper";
import TypeCheckUtils from "../../../utils/TypeCheckUtils";
import DeclaredTypeFacts from "../../../utils/DeclaredTypeFacts";
import ForeignTypeFacts from "../../../utils/ForeignTypeFacts";
import FunctionReference from "./FunctionReference";
import TypeText from "./TypeText";
import invariant from "../../../utils/invariant";
import type IAnalysisContext from "../types/IAnalysisContext";
/** What an establishing position gives the value under it */
interface IPositionType {
/** The type's spelling -- a field's or parameter's element type; null
* when the position cannot name one */
readonly typeText: string | null;
/** Whether the position takes a whole array (#1760 second review: only a
* declaration's spelling carried its dimensions) */
readonly isArray: boolean;
/** A declaration, whose whole-array initializer ADR-035's rule checks */
readonly isDeclaration: boolean;
}
const NO_TYPE: IPositionType = {
typeText: null,
isArray: false,
isDeclaration: false,
};
class StructInitializerType {
/** The struct's C name, or null when nothing establishes one. */
static of(
init: Parser.StructInitializerContext,
context: IAnalysisContext,
): string | null {
const typeText = StructInitializerType.establishedTypeText(init, context);
return typeText === null
? null
: StructInitializerType.structNamed(
typeText,
OperandTyper.scopePathAt(init, context),
context,
);
}
/** The struct a type spelling names, as keyed in `structFields`, or null. */
static structNamed(
typeText: string,
scopePath: string,
context: IAnalysisContext,
): string | null {
const fields = context.symbols.structFields;
Iif (!fields) return null;
return (
FunctionReference.candidatesForTypeText(typeText, scopePath).find((c) =>
fields.has(c),
) ?? null
);
}
/**
* Whether ANY enclosing position supplies a type for this initializer,
* without asking what that type resolves to.
*
* ADR-014's two diagnostics need only this. Asking for the resolved NAME
* conflates "no position supplies a type" with "a position supplies one and
* this pass cannot name it", and those are opposite answers: the first is
* E0357, the second must stay silent. The distinction is not hypothetical --
* a field of a struct declared in an included C header resolves through the
* C symbols rather than the C-Next view, so `{ flag_a: 1 }` inside
* `SimpleConfig cfg <- { flags: { flag_a: 1 } }` has a position supplying a
* type that this pass cannot name, and nine `tests/interop` fixtures say so.
*/
/**
* #1802: the type a struct initializer gives a value to, when that type is
* not a struct. Null when it is a struct, or when this pass does not know
* the type. An element of an array's list is checked at the element's
* type, while an array's whole initializer is E0866's.
*
* The positive question, asked of each type's one home (#1760 second
* review): a bitmap first, since `DeclaredTypeFacts.isStruct` counts it
* struct-like (#551); then a C-Next struct, and a header's struct, union
* or opaque type. Any other type this pass knows is not a struct. Asking
* "known NOT to be a struct" let a header's pointer or function-pointer
* typedef and an ADR-029 function type through, since none has a category.
*/
static nonStructTarget(
init: Parser.StructInitializerContext,
context: IAnalysisContext,
): { readonly typeName: string; readonly isBitmap: boolean } | null {
const established = StructInitializerType.establish(init, context);
const typeText = established.typeText;
if (typeText === null) return null;
if (established.isArray && !established.inList) return null;
const typeName = TypeText.withoutDimensions(typeText);
const symbols = context.symbols;
const candidates = FunctionReference.candidatesForTypeText(
typeName,
OperandTyper.scopePathAt(init, context),
);
if (candidates.some((c) => DeclaredTypeFacts.isBitmap(symbols, c))) {
return { typeName, isBitmap: true };
}
const isStruct =
candidates.some((c) =>
DeclaredTypeFacts.isStruct(symbols, context.symbolTable, c),
) ||
ForeignTypeFacts.isStructType(
typeName,
context.symbolTable,
OperandTyper.target(context),
);
if (isStruct) return null;
return StructInitializerType.isKnownType(typeName, candidates, context)
? { typeName, isBitmap: false }
: null;
}
/**
* Whether this pass knows a type that is not a struct: a primitive or a
* string, an enum or an ADR-029 function type this file sees, or a type a
* header declares.
*/
private static isKnownType(
typeName: string,
candidates: readonly string[],
context: IAnalysisContext,
): boolean {
const symbols = context.symbols;
return (
OperandTyper.categoryOf(typeName) !== "none" ||
TypeCheckUtils.isSizedStringName(typeName) ||
candidates.some(
(c) =>
DeclaredTypeFacts.isEnum(symbols, c) ||
symbols.functionReturnTypes.has(c),
) ||
ForeignTypeFacts.isForeignType(
typeName,
context.symbolTable,
OperandTyper.target(context),
)
);
}
static hasEstablishingPosition(
init: Parser.StructInitializerContext,
): boolean {
let cursor: ParserRuleContext | null = init.parent;
while (cursor) {
const position = StructInitializerType.positionOf(cursor);
if (position !== "neither") return position === "supplies";
cursor = cursor.parent;
}
return false;
}
/**
* What an ancestor is to an initializer below it: a position that supplies
* its type, a subscript, or neither. A subscript's expression is reached
* before any declaration above it and supplies no struct type, so it ends
* the search. The one list of positions, which E0357's structural question
* and the typed walk both ask. #1802: each held its own copy.
*/
private static positionOf(
cursor: ParserRuleContext,
): "supplies" | "subscript" | "neither" {
Iif (cursor instanceof Parser.PostfixOpContext) return "subscript";
return cursor instanceof Parser.VariableDeclarationContext ||
cursor instanceof Parser.ForVarDeclContext ||
cursor instanceof Parser.FieldInitializerContext ||
cursor instanceof Parser.AssignmentStatementContext ||
cursor instanceof Parser.ReturnStatementContext ||
cursor instanceof Parser.ArgumentListContext
? "supplies"
: "neither";
}
/**
* The type an inferred initializer must take, read off the nearest
* establishing ancestor, or null when it stands somewhere no type reaches
* it (a subscript, a bare expression statement).
*
* Public because ADR-014's two diagnostics ask exactly this and nothing
* else: a written type where a position already supplies one is redundant
* (E0356), and no written type where no position supplies one cannot be
* resolved (E0357). Codegen asks the same question by threading
* `expectedType` DOWN as it generates; this walks UP from the literal. Two
* mechanisms, one rule -- the boundary is stated at both ends, because the
* renderer may not import an analyzer and the analyzer must not read
* codegen state.
*/
static establishedTypeText(
init: Parser.StructInitializerContext,
context: IAnalysisContext,
): string | null {
return StructInitializerType.establish(init, context).typeText;
}
/**
* The type a value's position gives it, the way it gives one to an
* initializer: a declaration's (a `for` header's too), the field it
* initializes, the element of the array list it sits in, the assignment
* target's, the parameter it is passed to, or the enclosing function's
* return type. Null when no position types it, and for a whole-array
* position. #1760 second review: E0891 asks this of every value, so a
* float reaches an integer parameter, return, field or element only
* through a cast, as it reaches a declaration.
*/
static valueType(
value: ParserRuleContext,
context: IAnalysisContext,
): string | null {
const established = StructInitializerType.establish(value, context);
const typeText = established.typeText;
if (typeText === null) return null;
if (established.isArray && !established.inList) return null;
return TypeText.withoutDimensions(typeText);
}
/**
* Whether a struct initializer stands where a whole array is taken, other
* than a declaration's initializer, which ADR-035's rule checks itself: a
* field, an assignment target or a parameter that is an array. ADR-014:
* an array's whole initializer is ADR-035's list (E0866). #1760 second
* review: these positions typed the element, so the initializer was
* E0358's -- or, for an array of structs, emitted as one struct.
*/
static takesWholeArray(
init: Parser.StructInitializerContext,
context: IAnalysisContext,
): boolean {
const established = StructInitializerType.establish(init, context);
return (
established.isArray && !established.inList && !established.isDeclaration
);
}
/**
* The one walk up from a value: what its nearest establishing position
* gives, and whether an array's list stood between the two. An element of
* `[{ a: 1 }, 2]` is typed by the element, not the array (#1802).
*/
private static establish(
value: ParserRuleContext,
context: IAnalysisContext,
): IPositionType & { readonly inList: boolean } {
let inList = false;
let child: ParserRuleContext = value;
let cursor: ParserRuleContext | null = value.parent;
while (cursor) {
const established = StructInitializerType.typeEstablishedBy(
cursor,
child,
context,
);
if (established !== undefined) return { ...established, inList };
if (cursor instanceof Parser.ArrayInitializerContext) inList = true;
child = cursor;
cursor = cursor.parent;
}
return { ...NO_TYPE, inList };
}
/**
* What one ancestor says, `NO_TYPE` for "nothing can reach here", or
* undefined for "not an establishing node -- keep climbing".
*/
private static typeEstablishedBy(
cursor: ParserRuleContext,
child: ParserRuleContext,
context: IAnalysisContext,
): IPositionType | undefined {
const position = StructInitializerType.positionOf(cursor);
if (position === "neither") return undefined;
Iif (position === "subscript") return NO_TYPE; // no struct is expected there
if (
cursor instanceof Parser.VariableDeclarationContext ||
cursor instanceof Parser.ForVarDeclContext
) {
const typeText = cursor.type()?.getText() ?? null;
const postfixDimensions =
cursor instanceof Parser.ForVarDeclContext &&
cursor.arrayDimension().length > 0;
return {
typeText,
isArray:
postfixDimensions ||
(typeText !== null &&
TypeText.withoutDimensions(typeText) !== typeText),
isDeclaration: true,
};
}
if (cursor instanceof Parser.FieldInitializerContext) {
return StructInitializerType.fieldPosition(cursor, context);
}
if (cursor instanceof Parser.AssignmentStatementContext) {
const target = OperandTyper.typeOfTarget(
cursor.assignmentTarget(),
context,
);
return {
typeText: target?.typeName ?? null,
isArray: (target?.dimensions.length ?? 0) > 0,
isDeclaration: false,
};
}
if (cursor instanceof Parser.ReturnStatementContext) {
return {
typeText: StructInitializerType.enclosingReturnType(cursor),
isArray: false,
isDeclaration: false,
};
}
invariant(
cursor instanceof Parser.ArgumentListContext,
"positionOf supplies only the positions typed above",
);
return StructInitializerType.parameterPosition(cursor, child, context);
}
/** The declared type of the field an enclosing initializer is setting. */
static fieldType(
field: Parser.FieldInitializerContext,
context: IAnalysisContext,
): string | null {
return StructInitializerType.fieldPosition(field, context).typeText;
}
/** The field an enclosing initializer is setting: its type, and whether
* it is an array */
private static fieldPosition(
field: Parser.FieldInitializerContext,
context: IAnalysisContext,
): IPositionType {
const outer = field.parent?.parent;
Iif (!(outer instanceof Parser.StructInitializerContext)) return NO_TYPE;
const structName = StructInitializerType.of(outer, context);
if (structName === null) return NO_TYPE;
const name = field.IDENTIFIER().getText();
return {
typeText: context.symbols.structFields.get(structName)?.get(name) ?? null,
isArray:
context.symbols.structFieldArrays.get(structName)?.has(name) ?? false,
isDeclaration: false,
};
}
private static enclosingReturnType(
at: Parser.ReturnStatementContext,
): string | null {
let cursor: ParserRuleContext | null = at.parent;
while (cursor && !(cursor instanceof Parser.FunctionDeclarationContext)) {
cursor = cursor.parent;
}
return cursor?.type().getText() ?? null;
}
/** The parameter an argument is passed to, if the callee is known */
private static parameterPosition(
args: Parser.ArgumentListContext,
argument: ParserRuleContext,
context: IAnalysisContext,
): IPositionType {
// `indexOf` needs the array's own element type; an argument that is not
// an expression is not in the list at all.
const index =
argument instanceof Parser.ExpressionContext
? args.expression().indexOf(argument)
: -1;
const postfix = args.parent?.parent;
Iif (index < 0 || !(postfix instanceof Parser.PostfixExpressionContext)) {
return NO_TYPE;
}
const callee = FunctionReference.ofCall(
postfix,
OperandTyper.scopePathAt(postfix, context),
context,
);
const param = callee?.parameters[index];
return param === undefined
? NO_TYPE
: {
typeText: TypeResolver.getTypeName(param.type),
isArray: param.isArray,
isDeclaration: false,
};
}
}
export default StructInitializerType;
|