Class OrdinalWithinSource

java.lang.Object
org.ek9lang.compiler.support.OrdinalWithinSource
All Implemented Interfaces:
ToIntBiFunction<org.antlr.v4.runtime.ParserRuleContext, Class<? extends org.antlr.v4.runtime.ParserRuleContext>>

public final class OrdinalWithinSource extends Object implements ToIntBiFunction<org.antlr.v4.runtime.ParserRuleContext, Class<? extends org.antlr.v4.runtime.ParserRuleContext>>
THE position-independent identity for a dynamic construct: its index among the constructs of the same KIND in the same source file, in document order. Hold one and apply it (the result feeds LocationBasedInternalName).

Hash inputs are rename triggers - every extra input is another way for a name to change while the construct itself did not. So the pair (relativeFile, ordinal) is deliberately the WHOLE identity: it is the smallest thing that is both unique and reproducible.

  • Unique - one source file is parsed sequentially, so ordinals are distinct within it; the file name separates files; a module is a set of files, so no intra-module collision. Note this is why the ENCLOSING scope is NOT an input: method FQNs are not unique under OVERLOADING, so (enclosingFqn, ordinal) would collide for a dynamic construct declared at the same index in two overloads of one method.
  • Reproducible - a pure function of the parse tree, so it does not depend on the order sources are compiled in (they are processed in parallel), nor on any mutable counter. Being idempotent matters: a counter would hand out a DIFFERENT ordinal if a symbol were ever created twice for one context, whereas re-deriving from the tree cannot.
  • Position-independent - no line or column, so inserting, resizing or deleting anything above the declaration cannot rename it. That is the whole point: an @IR directive for a dynamic construct lives in the SAME file ABOVE it, so a line-based name is self-referential - regenerating the directive moves the declaration, which renames the symbol the directive names, which can never settle.
  • Constructor Details

    • OrdinalWithinSource

      public OrdinalWithinSource()
  • Method Details

    • applyAsInt

      public int applyAsInt(org.antlr.v4.runtime.ParserRuleContext ctx, Class<? extends org.antlr.v4.runtime.ParserRuleContext> kind)
      The ordinal.
      Specified by:
      applyAsInt in interface ToIntBiFunction<org.antlr.v4.runtime.ParserRuleContext, Class<? extends org.antlr.v4.runtime.ParserRuleContext>>
      Parameters:
      ctx - the dynamic construct's parse context
      kind - the context class to count - dynamic functions and dynamic classes are counted separately, so adding one cannot renumber the other
      Returns:
      the 0-based index of ctx among same-kind contexts in its source