All files / src/abap/5_syntax/global_definitions find_global_definitions.ts

99.32% Statements 148/149
94.02% Branches 63/67
100% Functions 5/5
99.32% Lines 148/149

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 1491x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7171x 7171x 1x 1x 7170x 7170x 7170x 7170x 7170x 7170x 7537x 453x 7537x 7084x 7084x 7084x 7084x 7084x 247x 247x 7537x 7170x 7170x 7170x 7170x 7170x 7279x 7279x 7279x 7279x 601x 601x 601x 601x 230x 230x 601x 601x 7279x 7279x 7279x 7279x 7170x 7170x 109x 109x 7170x 1x 1x 1x 1x 602x 602x 173x 173x 429x 429x 602x 57x 57x 602x 80x 80x 602x 90x 90x 429x 602x 123x 64x 64x 123x 602x 3x 1x 1x 3x 429x 429x 429x 1x 1x 401x 94x 401x 17x 307x   290x 48x 48x 92x 92x 48x 48x 242x 242x 1x 1x 601x 601x 601x 601x 101x 101x 83x 83x 83x 83x 4x 4x 101x 18x 18x 601x 500x 500x 426x 426x 426x 426x 77x 77x 500x 74x 74x 500x 601x 1x
import {IRegistry} from "../../../_iregistry";
import {InterfaceDefinition} from "../../types/interface_definition";
import {ClassDefinition} from "../../types/class_definition";
import {CurrentScope} from "../_current_scope";
import * as Structures from "../../3_structures/structures";
import {Interface} from "../../../objects/interface";
import {Class} from "../../../objects/class";
import * as BasicTypes from "../../types/basic";
import {AbstractType} from "../../types/basic/_abstract_type";
import {IProgress} from "../../../progress";
import {TypedIdentifier} from "../../types/_typed_identifier";
import {AuthorizationCheckField, DataElement, LockObject, Table, TableType, View} from "../../../objects";
 
// todo: rewrite all of this to use a graph based deterministic approach instead
 
// this makes sure to cache global interface and class definitions in the corresponding object
export class FindGlobalDefinitions {
  private readonly reg: IRegistry;
 
  public constructor(reg: IRegistry) {
    this.reg = reg;
  }
 
  public run(progress?: IProgress) {
    const MAX_PASSES = 10;
    let lastPass = Number.MAX_SAFE_INTEGER;
 
    // the setDirty method in the objects clears the definitions
    let candidates: (Class | Interface)[] = [];
    for (const o of this.reg.getObjects()) {
      if ((o instanceof Interface || o instanceof Class) && o.getDefinition() === undefined) {
        candidates.push(o);
      } else if (o instanceof DataElement
          || o instanceof View
          || o instanceof TableType
          || o instanceof LockObject
          || o instanceof AuthorizationCheckField
          || o instanceof Table) {
        o.parseType(this.reg); // make sure the references are set after parsing finishes
      }
    }
    // make sure the sequence is always the same, disregarding the sequence they were added to the registry
    // this will hopefully make it easier to debug
    candidates.sort((a, b) => {return a.getName().localeCompare(b.getName());});
 
    for (let i = 1; i <= MAX_PASSES; i++) {
      progress?.set(candidates.length, "Global OO types, pass " + i);
      let thisPass = 0;
      const next: (Class | Interface)[] = [];
      for (const o of candidates) {
        progress?.tickSync("Global OO types(pass " + i + "), next pass: " + next.length);
        this.update(o);
        const untypedCount = this.countUntyped(o);
        if (untypedCount > 0) {
          next.push(o);
        }
        thisPass = thisPass + untypedCount;
      }
 
      candidates = next;
 
      if (lastPass === thisPass || thisPass === 0) {
        break;
      }
      lastPass = thisPass;
    }
  }
 
/////////////////////////////
 
  public countUntyped(obj: Interface | Class): number {
    const def = obj.getDefinition();
    if (def === undefined) {
      return 1;
    }
 
    let count = 0;
    for (const t of def.getTypeDefinitions().getAll()) {
      count = count + this.count(t.type.getType());
    }
    for (const a of def.getAttributes().getAll()) {
      count = count + this.count(a.getType());
    }
    for (const a of def.getAttributes().getConstants()) {
      count = count + this.count(a.getType());
    }
 
    for (const m of def.getMethodDefinitions().getAll()) {
      for (const p of m.getParameters().getAll()) {
        count = count + this.count(p.getType());
      }
    }
    for (const e of def.getEvents() || []) {
      for (const p of e.getParameters()) {
        count = count + this.count(p.getType());
      }
    }
 
    return count;
  }
 
  private count(type: TypedIdentifier | AbstractType): number {
    if (type instanceof BasicTypes.UnknownType || type instanceof BasicTypes.VoidType) {
      return 1;
    } else if (type instanceof BasicTypes.TableType) {
      return this.count(type.getRowType());
    } else if (type instanceof BasicTypes.DataReference) {
      return this.count(type.getType());
    } else if (type instanceof BasicTypes.StructureType) {
      let count = 0;
      for (const c of type.getComponents()) {
        count = count + this.count(c.type);
      }
      return count;
    }
    return 0;
  }
 
  private update(obj: Interface | Class) {
    const file = obj.getMainABAPFile();
    const struc = file?.getStructure();
 
    if (obj instanceof Interface) {
      const found = struc?.findFirstStructure(Structures.Interface);
      if (struc && file && found) {
        try {
          const def = new InterfaceDefinition(found, file.getFilename(), CurrentScope.buildDefault(this.reg, obj));
          obj.setDefinition(def);
        } catch {
          obj.setDefinition(undefined);
        }
      } else {
        obj.setDefinition(undefined);
      }
    } else {
      const found = struc?.findFirstStructure(Structures.ClassDefinition);
      if (struc && file && found) {
        try {
          const def = new ClassDefinition(found, file.getFilename(), CurrentScope.buildDefault(this.reg, obj));
          obj.setDefinition(def);
        } catch {
          obj.setDefinition(undefined);
        }
      } else {
        obj.setDefinition(undefined);
      }
    }
  }
}