All files / src/abap/5_syntax spaghetti_scope.ts

97.96% Statements 433/442
97.47% Branches 116/119
100% Functions 41/41
97.96% Lines 433/442

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 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 4421x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 110254x 110254x 110254x 110254x 110254x 110254x 110254x 110254x 110254x 110254x 110254x 110254x 1x 1x 1129880x 1129880x 1x 1x 1x 1x 576556x 87252x 87252x 576556x 576556x 1x 1x 80750x 14485x 14485x 80750x 80750x 1x 1x 8130x 2320x 2320x 8130x 8130x 1x 1x 18x 18x 18x 18x 18x 1x 1x 6250x 4175x 4175x 6250x 6250x 1x 1x 2287x 2257x 2257x 2287x 2287x 1x 1x 4282x 2357x 2357x 4282x 4282x 1x 1x 69781x 43155x 43155x 69781x 69781x 1x 1x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x     1x 1x 110254x 110254x 110254x 110254x 110254x 1x 1x 1540699x 1540699x 1x 1x 97134x 97134x 1x 1x 86888x 86888x 1x 1x 75x 75x 1x 1x 1612162x 1612162x 1x 1x 40291x     40291x 40291x 1x 1x 96643x 96643x 1x 1x 552x 552x 552x 2284x 2284x 18x 18x 18x 18x 18x 2266x 2266x 534x 534x 534x 1x 1x 61957x 61957x 61957x 61957x 143130x 143130x 58540x 58540x 84590x 84590x 3417x 3417x 3417x 1x 1x 98x 98x 98x 98x 366x 366x 366x 98x 98x 98x 1x 1x 98x 98x 98x 98x 366x 366x 366x 98x 98x 98x 1x 1x 156x 156x 156x 156x 343x 164x 154x 154x 164x 189x 189x 2x 2x 2x 1x 1x 28966x 28966x 28966x 28966x 103082x 103082x 23892x 23892x 79190x 79190x 5074x 5074x 5074x 1x 1x 11100x 11100x 11100x 11100x 33624x 33624x 8462x 8462x 25162x 25162x 2638x 2638x 2638x 1x 1x 27x 27x 27x 27x 85x 85x 1x 1x 84x 84x 26x 26x 26x 1x 1x 27181x 27181x 27181x 27181x 43577x 43577x 24323x 24323x 19254x 19254x 2858x 2858x 2858x 1x 1x 5x 8x 8x 3x 3x 3x 3x 8x 2x 2x 2x 1x 1x 25x 36x 36x 36x 18x 18x 36x 7x 7x 7x 1x 1x 3x 7x 7x 2x 2x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 96643x 96643x 1x 1x 1x 3x 3x 3x 9x 3x 3x 3x 3x 3x 3x 3x 9x 3x 3x 3x 1x 1x 5x 5x 5x 31x 21x 27x 7x 7x 27x 21x 31x 5x 5x 5x 1x 1x 3x 3x 3x 9x 3x 6x 4x 4x 6x 3x 9x 3x 3x 3x 1x 1x 4319x     4319x 4319x 1x 1x 7x 7x 1x 1x 22551x 22551x 1x 1x 1x 1x 11x 11x 11x 11x 49x 49x 49x 49x 11x 11x 11x 1x 1x 40190x 40190x 21397x 21397x 18793x 18793x 40190x 35871x 35871x 14375x 14375x 35871x 4418x 4418x 40190x 4316x 4316x 102x 102x 102x 1x 1x
import {Position} from "../../position";
import {TypedIdentifier} from "../types/_typed_identifier";
import {Identifier} from "../4_file_information/_identifier";
import {IClassDefinition} from "../types/_class_definition";
import {IInterfaceDefinition} from "../types/_interface_definition";
import {IFormDefinition} from "../types/_form_definition";
import {DeferredInformation, IScopeData, IScopeIdentifier, IScopeVariable, ISpaghettiScopeNode, ISpaghettiScope} from "./_spaghetti_scope";
import {IReference, ReferenceType} from "./_reference";
import {AssertError} from "./assert_error";
 
// most scopes only use a few of the containers, empty ones are shared and replaced on first write
const EMPTY_OBJECT: unknown = Object.freeze({});
const EMPTY_ARRAY: unknown = Object.freeze([]);
 
abstract class ScopeData {
  private readonly data: IScopeData;
 
  public constructor() {
    this.data = {
      vars: EMPTY_OBJECT as IScopeData["vars"],
      cdefs: EMPTY_OBJECT as IScopeData["cdefs"],
      idefs: EMPTY_OBJECT as IScopeData["idefs"],
      forms: EMPTY_ARRAY as IScopeData["forms"], // todo, refactor to object
      types: EMPTY_OBJECT as IScopeData["types"],
      extraLikeTypes: EMPTY_OBJECT as IScopeData["extraLikeTypes"],
      deferred: EMPTY_OBJECT as IScopeData["deferred"],
      references: EMPTY_ARRAY as IScopeData["references"],
      sqlConversion: EMPTY_ARRAY as IScopeData["sqlConversion"],
    };
  }
 
  public getData(): IScopeData {
    return this.data;
  }
 
  // the shared empty containers are frozen, the methods below replace them on first write
 
  public setVariable(name: string, identifier: TypedIdentifier): void {
    if (this.data.vars === EMPTY_OBJECT) {
      this.data.vars = {};
    }
    this.data.vars[name] = identifier;
  }
 
  public setType(name: string, type: TypedIdentifier): void {
    if (this.data.types === EMPTY_OBJECT) {
      this.data.types = {};
    }
    this.data.types[name] = type;
  }
 
  public setExtraLikeType(name: string, type: TypedIdentifier): void {
    if (this.data.extraLikeTypes === EMPTY_OBJECT) {
      this.data.extraLikeTypes = {};
    }
    this.data.extraLikeTypes[name] = type;
  }
 
  public setDeferred(name: string, deferred: DeferredInformation): void {
    if (this.data.deferred === EMPTY_OBJECT) {
      this.data.deferred = {};
    }
    this.data.deferred[name] = deferred;
  }
 
  public setClassDefinition(name: string, cdef: IClassDefinition): void {
    if (this.data.cdefs === EMPTY_OBJECT) {
      this.data.cdefs = {};
    }
    this.data.cdefs[name] = cdef;
  }
 
  public setInterfaceDefinition(name: string, idef: IInterfaceDefinition): void {
    if (this.data.idefs === EMPTY_OBJECT) {
      this.data.idefs = {};
    }
    this.data.idefs[name] = idef;
  }
 
  public addFormDefinitions(forms: readonly IFormDefinition[]): void {
    if (this.data.forms === EMPTY_ARRAY) {
      this.data.forms = [];
    }
    this.data.forms.push(...forms);
  }
 
  public addReference(reference: IReference): void {
    if (this.data.references === EMPTY_ARRAY) {
      this.data.references = [];
    }
    this.data.references.push(reference);
  }
 
  public addSQLConversion(conversion: IScopeData["sqlConversion"][number]): void {
    if (this.data.sqlConversion === EMPTY_ARRAY) {
      this.data.sqlConversion = [];
    }
    this.data.sqlConversion.push(conversion);
  }
}
 
export class SpaghettiScopeNode extends ScopeData implements ISpaghettiScopeNode {
  private readonly identifier: IScopeIdentifier;
  private readonly children: SpaghettiScopeNode[];
  private readonly parent: SpaghettiScopeNode | undefined;
  public [Symbol.for("debug.description")](){
    return `SpaghettiSN ${this.identifier.sname} ${this.identifier.stype}`;
  }
 
  public constructor(identifier: IScopeIdentifier, parent: SpaghettiScopeNode | undefined) {
    super();
    this.identifier = identifier;
    this.parent = parent;
    this.children = [];
  }
 
  public getParent(): SpaghettiScopeNode | undefined {
    return this.parent;
  }
 
  public addChild(node: SpaghettiScopeNode) {
    this.children.push(node);
  }
 
  public getChildren(): SpaghettiScopeNode[] {
    return this.children;
  }
 
  public getFirstChild(): SpaghettiScopeNode | undefined {
    return this.children[0];
  }
 
  public getIdentifier(): IScopeIdentifier {
    return this.identifier;
  }
 
  public calcCoverage(): {start: Position, end: Position} {
    if (this.identifier.end === undefined) {
      throw new AssertError("internal error, caclCoverage");
    }
    return {start: this.identifier.start, end: this.identifier.end};
  }
 
  public setEnd(end: Position): void {
    this.identifier.end = end;
  }
 
  public findDeferred(name: string): {id: Identifier | undefined, ooType: "CLAS" | "INTF"} | undefined {
    let search: SpaghettiScopeNode | undefined = this;
 
    while (search !== undefined) {
      const found = search.getData().deferred[name.toUpperCase()];
      if (found) {
        return {
          id: new Identifier(found.token, search.identifier.filename),
          ooType: found.ooType,
        };
      }
      search = search.getParent();
    }
 
    return undefined;
  }
 
  public findClassDefinition(name: string): IClassDefinition | undefined {
    let search: SpaghettiScopeNode | undefined = this;
 
    const upper = name.toUpperCase();
    while (search !== undefined) {
      const c = search.getData().cdefs[upper];
      if (c !== undefined) {
        return c;
      }
      search = search.getParent();
    }
 
    return undefined;
  }
 
  public listClassDefinitions(): IClassDefinition[] {
    let search: SpaghettiScopeNode | undefined = this;
    const ret: IClassDefinition[] = [];
 
    while (search !== undefined) {
      ret.push(...Object.values(search.getData().cdefs));
      search = search.getParent();
    }
 
    return ret;
  }
 
  public listInterfaceDefinitions(): IInterfaceDefinition[] {
    let search: SpaghettiScopeNode | undefined = this;
    const ret: IInterfaceDefinition[] = [];
 
    while (search !== undefined) {
      ret.push(...Object.values(search.getData().idefs));
      search = search.getParent();
    }
 
    return ret;
  }
 
  public findFormDefinition(name: string): IFormDefinition | undefined {
    let search: SpaghettiScopeNode | undefined = this;
 
    const upper = name.toUpperCase();
    while (search !== undefined) {
      for (const form of search.getData().forms) {
        if (form.getName().toUpperCase() === upper) {
          return form;
        }
      }
      search = search.getParent();
    }
 
    return undefined;
  }
 
  public findInterfaceDefinition(name: string): IInterfaceDefinition | undefined {
    let search: SpaghettiScopeNode | undefined = this;
 
    const upper = name.toUpperCase();
    while (search !== undefined) {
      const idef = search.getData().idefs[upper];
      if (idef) {
        return idef;
      }
      search = search.getParent();
    }
 
    return undefined;
  }
 
  public findType(name: string): TypedIdentifier | undefined {
    let search: SpaghettiScopeNode | undefined = this;
 
    const upper = name.toUpperCase();
    while (search !== undefined) {
      const found = search.getData().types[upper];
      if (found) {
        return found;
      }
      search = search.getParent();
    }
 
    return undefined;
  }
 
  public findExtraLikeType(name: string): TypedIdentifier | undefined {
    let search: SpaghettiScopeNode | undefined = this;
 
    const upper = name.toUpperCase();
    while (search !== undefined) {
      const data = search.getData();
      if (data.extraLikeTypes[upper]) {
        return data.extraLikeTypes[upper];
      }
      search = search.getParent();
    }
 
    return undefined;
  }
 
  public findVariable(name: string): TypedIdentifier | undefined {
    let search: SpaghettiScopeNode | undefined = this;
 
    const upper = name.toUpperCase();
    while (search !== undefined) {
      const data = search.getData();
      if (data.vars[upper]) {
        return data.vars[upper];
      }
      search = search.getParent();
    }
 
    return undefined;
  }
 
  public findWriteReference(pos: Position): TypedIdentifier | undefined {
    for (const r of this.getData().references) {
      if (r.referenceType === ReferenceType.DataWriteReference
          && r.position.getStart().equals(pos)) {
        if (r.resolved instanceof TypedIdentifier) {
          return r.resolved;
        }
      }
    }
 
    return undefined;
  }
 
  public findTableReference(pos: Position): string | undefined {
    for (const r of this.getData().references) {
      if (r.referenceType === ReferenceType.TableReference
          && r.position.getStart().equals(pos)
          && r.resolved) {
        return r.resolved.getName();
      }
    }
 
    return undefined;
  }
 
  public findTableVoidReference(pos: Position): boolean {
    for (const r of this.getData().references) {
      if (r.referenceType === ReferenceType.TableVoidReference
          && r.position.getStart().equals(pos)) {
        return true;
      }
    }
 
    return false;
  }
 
  // this method is used in the transpiler
  public findScopeForVariable(name: string): IScopeIdentifier | undefined {
    let search: SpaghettiScopeNode | undefined = this;
    const upper = name.toUpperCase();
 
    while (search !== undefined) {
      if (search.getData().vars[upper] !== undefined) {
        return search.getIdentifier();
      }
      search = search.getParent();
    }

    return undefined;
  }
 
}
 
export class SpaghettiScope implements ISpaghettiScope {
  private readonly node: SpaghettiScopeNode;
 
  public constructor(top: SpaghettiScopeNode) {
    this.node = top;
  }
 
  // list variable definitions across all nodes
  public listDefinitions(filename: string): IScopeVariable[] {
    const ret: IScopeVariable[] = [];
 
    for (const n of this.allNodes()) {
      if (n.getIdentifier().filename === filename) {
        const vars = n.getData().vars;
        for (const v in vars) {
          if (vars[v].getFilename() === filename) {
            ret.push({name: v, identifier: vars[v]});
          }
        }
      }
    }
 
    return ret;
  }
 
  public listReadPositions(filename: string): Identifier[] {
    const ret: Identifier[] = [];
 
    for (const n of this.allNodes()) {
      if (n.getIdentifier().filename === filename) {
        for (const v of n.getData().references) {
          if (v.referenceType === ReferenceType.DataReadReference && v.position.getFilename() === filename) {
            ret.push(v.position);
          }
        }
      }
    }
 
    return ret;
  }
 
  public listWritePositions(filename: string): Identifier[] {
    const ret: Identifier[] = [];
 
    for (const n of this.allNodes()) {
      if (n.getIdentifier().filename === filename) {
        for (const v of n.getData().references) {
          if (v.referenceType === ReferenceType.DataWriteReference && v.position.getFilename() === filename) {
            ret.push(v.position);
          }
        }
      }
    }
 
    return ret;
  }
 
  public lookupPosition(p: Position | undefined, filename: string | undefined): SpaghettiScopeNode | undefined {
    if (p === undefined || filename === undefined) {
      return undefined;
    }
    return this.lookupPositionTraverse(p, filename, this.node);
  }
 
  public getFirstChild() {
    return this.node.getFirstChild();
  }
 
  public getTop(): SpaghettiScopeNode {
    return this.node;
  }
 
/////////////////////////////
 
  private allNodes(): SpaghettiScopeNode[] {
    const ret: SpaghettiScopeNode[] = [];
    const stack: SpaghettiScopeNode[] = [this.node];
 
    while (stack.length > 0) {
      const current = stack.pop()!;
      ret.push(current);
      stack.push(...current.getChildren());
    }
 
    return ret;
  }
 
  private lookupPositionTraverse(p: Position, filename: string, node: SpaghettiScopeNode): SpaghettiScopeNode | undefined {
    const coverage = node.calcCoverage();
    if (node.getIdentifier().filename === filename && p.isBetween(coverage.start, coverage.end) === false) {
      return undefined;
    }
 
    // possible optimization: binary search the nodes
    for (const c of node.getChildren()) {
      const result = this.lookupPositionTraverse(p, filename, c);
      if (result !== undefined) {
        return result;
      }
    }
 
    if (node.getIdentifier().filename === filename
        && p.isBetween(coverage.start, coverage.end)) {
      return node;
    }
 
    return undefined;
  }
 
}