All files / PARSE/3-Declare/cnext/collectors LexicalScopeCollector.ts

100% Statements 41/41
100% Branches 15/15
100% Functions 14/14
100% Lines 41/41

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                                                                                          3340x 3340x   3340x 3340x       23004x                 7597x               7597x 7597x     7597x 7597x       3477x     569x     569x                 3340x   3313x     3313x 3313x                   3313x 3313x         3313x 716x                                       3340x   3661x 3661x     3340x   54x 54x     3340x   4902x     4902x   4902x 2183x   2719x     42x 42x             2761x     2761x         2761x                                                                   3340x               3340x       3340x          
/**
 * #1668 / #1664: a file's lexical frames -- every scope, function, braced
 * block and `for` header, what each declares, and how they nest.
 *
 * 2.1 and 2.2 each answered "what does this name mean HERE" from their own
 * walk: 2.1 from per-analyzer frame maps keyed on parse nodes, 2.2 from one
 * flat registry keyed by name, which a shadowing local overwrote. This is the
 * one answer, built once by 1.3 from one file's tree and holding no parse
 * node, so it rides on `IFileSymbols` and 1.4 settles it with the rest.
 *
 * Globals and scope members are not recorded: they are `IVariableSymbol`s.
 * A local's facts come from `VariableCollector.declaredFacts` and a
 * parameter's from `FunctionCollector.collectParameters`, the functions that
 * already derive them for symbols, so a declaration means the same thing
 * wherever it is written.
 */
import { ParserRuleContext, ParseTreeWalker, TerminalNode } from "antlr4ng";
 
import { CNextListener } from "../../../2-Parse/grammar/CNextListener";
import * as Parser from "../../../2-Parse/grammar/CNextParser";
import FunctionCollector from "./FunctionCollector";
import VariableCollector from "./VariableCollector";
import type SymbolRegistry from "../../SymbolRegistry";
import OverflowBehaviorUtils from "../../../../utils/OverflowBehaviorUtils";
import ParserUtils from "../../../../utils/ParserUtils";
import ScopeUtils from "../../../../utils/ScopeUtils";
import type ILexicalFrame from "../../../../types/ILexicalFrame";
import type ILocalDeclaration from "../../../../types/ILocalDeclaration";
import type ISourceSpan from "../../../../types/ISourceSpan";
 
/** A frame while it is being filled */
interface IFrameBuilder {
  readonly kind: ILexicalFrame["kind"];
  readonly span: ISourceSpan;
  readonly scopePath: string;
  readonly functionCName: string | null;
  readonly declarations: ILocalDeclaration[];
  readonly children: IFrameBuilder[];
}
 
class FrameListener extends CNextListener {
  private readonly stack: IFrameBuilder[];
 
  constructor(
    root: IFrameBuilder,
    private readonly registry: SymbolRegistry,
    private readonly isScopeType: (qualifiedName: string) => boolean,
  ) {
    super();
    this.stack = [root];
  }
 
  private top(): IFrameBuilder {
    return this.stack.at(-1)!;
  }
 
  private push(
    kind: ILexicalFrame["kind"],
    ctx: ParserRuleContext,
    scopePath: string,
    functionCName: string | null = null,
  ): void {
    const frame: IFrameBuilder = {
      kind,
      span: ParserUtils.getSpan(ctx),
      scopePath,
      functionCName,
      declarations: [],
      children: [],
    };
    this.top().children.push(frame);
    this.stack.push(frame);
  }
 
  private readonly pop = (): void => {
    this.stack.pop();
  };
 
  private static spanOf(node: TerminalNode): ISourceSpan {
    return ParserUtils.getSpan({ start: node.symbol, stop: node.symbol });
  }
 
  override enterScopeDeclaration = (
    ctx: Parser.ScopeDeclarationContext,
  ): void => {
    this.push(
      "scope",
      ctx,
      ScopeUtils.pathOf(
        this.registry.getOrCreateScope(ctx.IDENTIFIER().getText()),
      ),
    );
  };
 
  override exitScopeDeclaration = this.pop;
 
  override enterFunctionDeclaration = (
    ctx: Parser.FunctionDeclarationContext,
  ): void => {
    const scopePath = this.top().scopePath;
    this.push(
      "function",
      ctx,
      scopePath,
      ScopeUtils.getTranspiledCName({
        name: ctx.IDENTIFIER().getText(),
        scopePath,
      }),
    );
 
    const params = ctx.parameterList()?.parameter() ?? [];
    const infos = FunctionCollector.collectParameters(
      params,
      scopePath,
      this.isScopeType,
    );
    infos.forEach((info, index) => {
      this.top().declarations.push({
        name: info.name,
        kind: "parameter",
        span: FrameListener.spanOf(params[index].IDENTIFIER()),
        type: info.type,
        arrayDimensions: info.arrayDimensions ?? [],
        arrayDimensionExprs: info.arrayDimensionExprs ?? [],
        isConst: info.isConst,
        isAtomic: false,
        isVolatile: false,
        // A parameter has no overflow modifier in the grammar
        overflowBehavior: OverflowBehaviorUtils.fromModifier(null),
        initialValue: null,
        initialValueExpr: null,
        initializerCallee: null,
        constValue: null,
      });
    });
  };
 
  override exitFunctionDeclaration = this.pop;
 
  override enterBlock = (ctx: Parser.BlockContext): void => {
    this.push("block", ctx, this.top().scopePath);
  };
 
  override exitBlock = this.pop;
 
  override enterForStatement = (ctx: Parser.ForStatementContext): void => {
    this.push("for", ctx, this.top().scopePath);
  };
 
  override exitForStatement = this.pop;
 
  override enterVariableDeclaration = (
    ctx: Parser.VariableDeclarationContext,
  ): void => {
    const frame = this.top();
    // At file or scope level this is a global or member: a symbol, not a local
    if (frame.kind === "file" || frame.kind === "scope") {
      return;
    }
    this.record(ctx, ctx.constructorArgumentList() ? "constructor" : "local");
  };
 
  override enterForVarDecl = (ctx: Parser.ForVarDeclContext): void => {
    this.record(ctx, "for");
  };
 
  private record(
    ctx: Parser.VariableDeclarationContext | Parser.ForVarDeclContext,
    kind: ILocalDeclaration["kind"],
  ): void {
    const frame = this.top();
    // #1664 box 7: a local's `u8[N]` keeps `N` as written, so 1.4 folds it
    // where it is declared -- after a local `const N` that shadows a global one.
    const facts = VariableCollector.declaredFacts(
      ctx,
      frame.scopePath,
      this.isScopeType,
    );
    frame.declarations.push({
      name: ctx.IDENTIFIER().getText(),
      kind,
      span: FrameListener.spanOf(ctx.IDENTIFIER()),
      type: facts.type,
      arrayDimensions: facts.arrayDimensions,
      arrayDimensionExprs: facts.arrayDimensionExprs,
      isConst: facts.isConst,
      isAtomic: facts.isAtomic,
      isVolatile: facts.isVolatile,
      overflowBehavior: facts.overflowBehavior,
      initialValue: facts.initialValue ?? null,
      initialValueExpr: facts.initialValueExpr,
      initializerCallee: facts.initializerCallee,
      constValue: null,
    });
  }
}
 
class LexicalScopeCollector {
  /**
   * The file frame of `tree`, with every frame nested in it.
   *
   * Array dimensions are left for 1.4 to fold in each declaration's lexical
   * environment, except a literal or `sizeof`, which needs no const.
   *
   * @param isScopeType ADR-057: is this QUALIFIED name a scope type this file
   *   declares? A bare type it cannot settle is deferred, as for symbols
   */
  static collect(
    tree: Parser.ProgramContext,
    registry: SymbolRegistry,
    isScopeType: (qualifiedName: string) => boolean,
  ): ILexicalFrame {
    const root: IFrameBuilder = {
      kind: "file",
      span: ParserUtils.getSpan(tree),
      scopePath: "",
      functionCName: null,
      declarations: [],
      children: [],
    };
    ParseTreeWalker.DEFAULT.walk(
      new FrameListener(root, registry, isScopeType),
      tree,
    );
    return root;
  }
}
 
export default LexicalScopeCollector;