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

91.38% Statements 350/383
73.17% Branches 90/123
100% Functions 34/34
91.38% Lines 350/383

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 3841x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4592x 4592x 4592x 4592x 4592x 4592x 4592x 4592x 4592x 4592x 4592x 4592x 4592x 4592x 1x 1x 4592x 4592x 4592x 4592x 13776x 13776x 4592x 1x 1x 4592x 4592x 4592x 4592x 1x 1x 1x 1x 14091x     14091x 14091x 1x 1x 14104x     14104x     14104x 14104x 2x 2x 14102x 14102x 1x 1x 754x 754x 1x 1x 141x     141x 141x 1x 1x 173x 173x 1x 1x 121360x     121360x 121360x 2x 2x 121358x 121358x 1x 1x 121324x     121324x 121324x 1x 1x 3x     3x 3x 1x 1x 276x 32x 32x 276x 1x 1x 6497x 119610x 119610x 6497x 1x 1x 2487x 2487x 2487x 2487x 2487x 2487x 2487x     2487x 2487x 2487x 2487x 1x 1x 1x 1x 355x 39x 39x 316x 355x 208x 208x 108x 355x 45x 45x 63x 63x 1x 1x 53x 53x 1x 1x 1x 1x 1x   52x 52x 1x 1x 1x 218x 218x     218x 218x 218x 2x 2x 216x 218x 218x 89x 89x 127x 127x 218x 15x 15x 112x 218x 218x 17x 17x 95x 95x 218x 10x 10x 85x 85x 85x 1x 1x 1x 1x 1x 2190x 322x 322x 1868x 2190x 2190x 994x 994x 874x 874x 2190x 724x 724x 150x 150x 150x 1x 1x 250x     250x 250x 250x 250x 249x 249x 1x 1x 1x 250x 250x 250x 1x 1x 164x     164x 164x 164x 164x 163x 163x 1x 1x 1x 164x 164x 164x 1x 1x 1x 437x 437x 230x 230x 207x 207x 437x 119x 119x 88x 88x 88x 1x 1x 12x 12x 1x 1x 408x     408x 408x 1x 1x 1089x     1089x 1089x 839x 839x 250x 250x 1x 1x 1x 1x 1714x 1714x 1x 1x 193x 193x 1x 1x 193x 193x 1x 1x 332x     332x 332x 1x 1x 459x     459x 459x 1x 1x 11941x 11941x 11941x 4592x 4592x 11941x 7349x 7349x 7349x 7349x 11941x 1x 1x 9x 9x 23x 23x 23x 23x 1x 1x 22x 22x 8x 8x 1x 1x 5x 5x 5x 1x 1x 5x 5x 1x 1x 5195x 5195x     5195x 5195x 5195x 5195x 5195x 5195x 1x  
import {TypedIdentifier} from "../types/_typed_identifier";
import {BuiltIn} from "./_builtin";
import {DDIC} from "../../ddic";
import {Position} from "../../position";
import {SpaghettiScope, SpaghettiScopeNode} from "./spaghetti_scope";
import {Token} from "../1_lexer/tokens/_token";
import {Identifier} from "../4_file_information/_identifier";
import {ScopeType} from "./_scope_type";
import {IRegistry} from "../../_iregistry";
import {IClassDefinition} from "../types/_class_definition";
import {IInterfaceDefinition} from "../types/_interface_definition";
import {IFormDefinition} from "../types/_form_definition";
import {IScopeIdentifier} from "./_spaghetti_scope";
import {ReferenceType, IReferenceExtras} from "./_reference";
import {AbstractType} from "../types/basic/_abstract_type";
 
import {IObject} from "../../objects/_iobject";
import {Class} from "../../objects/class";
import {Interface} from "../../objects/interface";
import {EnhancementSpot} from "../../objects/enhancement_spot";
import {TypePool} from "../../objects/type_pool";
import {SyntaxLogic} from "./syntax";
import {IDDICReferences} from "../../_iddic_references";
 
export class CurrentScope {
  protected readonly reg: IRegistry;
  protected current: SpaghettiScopeNode | undefined;
  protected allowHeaderUse: string | undefined;
  protected parentObj: IObject;
 
  public static buildDefault(reg: IRegistry, obj: IObject): CurrentScope {
    const s = new CurrentScope(reg, obj);
 
    s.push(ScopeType.BuiltIn, ScopeType.BuiltIn, new Position(1, 1), BuiltIn.filename);
    this.addBuiltIn(s, reg.getConfig().getSyntaxSetttings().globalConstants!);
 
    let name: string = ScopeType.Global;
    if (obj) {
      name = name + "_" + obj.getName();
    }
 
    s.push(ScopeType.Global, name, new Position(1, 1), name);
 
    return s;
  }
 
  private static addBuiltIn(s: CurrentScope, extras: string[]) {
    const b = new BuiltIn();
    const builtin = b.get(extras);
    s.addList(builtin);
    for (const t of b.getTypes()) {
      s.addType(t);
    }
  }
 
  private constructor(reg: IRegistry, obj: IObject) {
    this.current = undefined;
    this.parentObj = obj;
    this.reg = reg;
  }
 
///////////////////////////
 
  public addType(type: TypedIdentifier | undefined) {
    if (type === undefined) {
      return;
    }
    this.addTypeNamed(type.getName(), type);
  }
 
  public addTypeNamed(name: string, type: TypedIdentifier | undefined) {
    if (type === undefined) {
      return;
    }
    if (this.current === undefined) {
      return;
    }
    const upper = name.toUpperCase();
    if (this.current.getData().types[upper] !== undefined) {
      throw new Error(`Type name "${name}" already defined`);
    }
    this.current.getData().types[upper] = type;
  }
 
  public addClassDefinition(c: IClassDefinition) {
    this.current?.getData().cdefs.push(c);
  }
 
  public addFormDefinitions(f: readonly IFormDefinition[]) {
    if (this.current === undefined) {
      return;
    }
    this.current.getData().forms.push(...f);
  }
 
  public addInterfaceDefinition(i: IInterfaceDefinition) {
    this.current?.getData().idefs.push(i);
  }
 
  public addNamedIdentifier(name: string, identifier: TypedIdentifier) {
    if (this.current === undefined) {
      return;
    }
    const upper = name.toUpperCase();
    if (this.current.getData().vars[upper] !== undefined) {
      throw new Error(`Variable name "${name}" already defined`);
    }
    this.current.getData().vars[upper] = identifier;
  }
 
  public addIdentifier(identifier: TypedIdentifier | undefined) {
    if (identifier === undefined) {
      return;
    }
    this.addNamedIdentifier(identifier.getName(), identifier);
  }
 
  public addDeferred(token: Token | undefined) {
    if (token === undefined) {
      return;
    }
    this.current?.getData().deferred.push(token);
  }
 
  public addListPrefix(identifiers: readonly TypedIdentifier[], prefix: string) {
    for (const id of identifiers) {
      this.addNamedIdentifier(prefix + id.getName(), id);
    }
  }
 
  public addList(identifiers: readonly TypedIdentifier[]) {
    for (const id of identifiers) {
      this.addIdentifier(id);
    }
  }
 
  public addReference(
    usage: Token | undefined,
    referencing: Identifier | undefined,
    type: ReferenceType | undefined,
    filename: string,
    extra?: IReferenceExtras) {
 
    if (usage === undefined || type === undefined) {
      return;
    }
 
    const position = new Identifier(usage, filename);
    this.current?.getData().references.push({position, resolved: referencing, referenceType: type, extra});
  }
 
///////////////////////////
 
  public findObjectDefinition(name: string | undefined): IClassDefinition | IInterfaceDefinition | undefined {
    if (name === undefined) {
      return undefined;
    }
    const clas = this.findClassDefinition(name);
    if (clas) {
      return clas;
    }
    const intf = this.findInterfaceDefinition(name);
    if (intf) {
      return intf;
    }
    return undefined;
  }
 
  public isBadiDef(name: string): boolean {
    const upper = name.toUpperCase();
    for (const enhs of this.reg.getObjectsByType("ENHS")) {
      for (const def of (enhs as EnhancementSpot).listBadiDefinitions()) {
        if (def.name.toUpperCase() === upper) {
          return true;
        }
      }
    }
    return false;
  }
 
  // todo, found + type can be removed from method output?
  public existsObject(name: string | undefined): {found: boolean, id?: Identifier, type?: ReferenceType,
    ooType?: IReferenceExtras["ooType"]} {
    if (name === undefined) {
      return {found: false};
    }
 
    const def = this.current?.findDeferred(name);
    if (def !== undefined) {
      return {found: true, id: def};
    }
 
    const findLocalClass = this.current?.findClassDefinition(name);
    if (findLocalClass) {
      return {found: true, id: findLocalClass, type: ReferenceType.ObjectOrientedReference, ooType: "CLAS"};
    }
 
    const globalClas = this.reg.getObject("CLAS", name);
    if (globalClas) {
      return {found: true, id: globalClas.getIdentifier(), type: ReferenceType.ObjectOrientedReference, ooType: "CLAS"};
    }
 
    const findLocalInterface = this.current?.findInterfaceDefinition(name);
    if (findLocalInterface) {
      return {found: true, id: findLocalInterface, type: ReferenceType.ObjectOrientedReference, ooType: "INTF"};
    }
 
    const globalIntf = this.reg.getObject("INTF", name);
    if (globalIntf) {
      return {found: true, id: globalIntf.getIdentifier(), type: ReferenceType.ObjectOrientedReference, ooType: "INTF"};
    }
 
    return {found: false};
  }
 
///////////////////////////
 
  /** Lookup class in local and global scope */
  public findClassDefinition(name: string | undefined): IClassDefinition | undefined {
    if (name === undefined) {
      return undefined;
    }
 
    const clocal = this.current?.findClassDefinition(name);
    if (clocal) {
      return clocal;
    }
 
    const cglobal = this.reg.getObject("CLAS", name) as Class | undefined;
    if (cglobal) {
      return cglobal.getDefinition();
    }
 
    return undefined;
  }
 
  public findTypePoolConstant(name: string | undefined): TypedIdentifier | undefined {
    if (name === undefined || name.includes("_") === undefined) {
      return undefined;
    }
 
    const typePoolName = name.split("_")[0];
    const typePool = this.reg.getObject("TYPE", typePoolName) as TypePool | undefined;
    if (typePool === undefined) {
      return undefined;
    }
 
    const spag = new SyntaxLogic(this.reg, typePool).run().spaghetti.getFirstChild();
 
    const found = spag?.findVariable(name);
    return found;
  }
 
  public findTypePoolType(name: string): AbstractType | undefined {
    if (name.includes("_") === undefined) {
      return undefined;
    }
 
    const typePoolName = name.split("_")[0];
    const typePool = this.reg.getObject("TYPE", typePoolName) as TypePool | undefined;
    if (typePool === undefined) {
      return undefined;
    }
 
    const spag = new SyntaxLogic(this.reg, typePool).run().spaghetti.getFirstChild();
 
    const found = spag?.findType(name)?.getType();
    return found;
  }
 
  /** Lookup interface in local and global scope */
  public findInterfaceDefinition(name: string): IInterfaceDefinition | undefined {
    const ilocal = this.current?.findInterfaceDefinition(name);
    if (ilocal) {
      return ilocal;
    }
 
    const iglobal = this.reg.getObject("INTF", name) as Interface | undefined;
    if (iglobal) {
      return iglobal.getDefinition();
    }
 
    return undefined;
  }
 
  public findFormDefinition(name: string): IFormDefinition | undefined {
    return this.current?.findFormDefinition(name);
  }
 
  public findType(name: string | undefined): TypedIdentifier | undefined {
    if (name === undefined) {
      return undefined;
    }
    return this.current?.findType(name);
  }
 
  public findVariable(name: string | undefined): TypedIdentifier | undefined {
    if (name === undefined) {
      return undefined;
    }
    const found = this.current?.findVariable(name);
    if (found) {
      return found;
    }
    return this.findTypePoolConstant(name);
  }
 
///////////////////////////
 
  public getDDIC(): DDIC {
    return new DDIC(this.reg);
  }
 
  public getDDICReferences(): IDDICReferences {
    return this.reg.getDDICReferences();
  }
 
  public getParentObj(): IObject {
    return this.parentObj;
  }
 
  public getName(): string {
    if (this.current === undefined) {
      throw new Error("error, getName");
    }
    return this.current.getIdentifier().sname;
  }
 
  public getType(): ScopeType {
    if (this.current === undefined) {
      throw new Error("error, getType");
    }
    return this.current.getIdentifier().stype;
  }
 
  public push(stype: ScopeType, sname: string, start: Position, filename: string): void {
    const identifier: IScopeIdentifier = {stype, sname, start, filename, end: undefined};
 
    if (this.current === undefined) {
      // the top node
      this.current = new SpaghettiScopeNode(identifier, undefined);
    } else {
      const parent = this.current;
      this.current = new SpaghettiScopeNode(identifier, parent);
      parent.addChild(this.current);
    }
  }
 
  public isOO(): boolean {
    let curr = this.current;
    while (curr !== undefined) {
      const stype = curr.getIdentifier().stype;
      if (stype === ScopeType.ClassDefinition
          || stype === ScopeType.ClassImplementation
          || stype === ScopeType.Interface) {
        return true;
      }
      curr = curr.getParent();
    }
    return false;
  }
 
  public setAllowHeaderUse(name: string) {
// workaround for SELECT FOR ALL ENTRIES
    this.allowHeaderUse = name;
  }
 
  public isAllowHeaderUse(name: string) {
    return name.toUpperCase() === this.allowHeaderUse?.toUpperCase();
  }
 
  public pop(end: Position): SpaghettiScope {
    this.allowHeaderUse = undefined;
    if (this.current === undefined) {
      throw new Error("something wrong, top scope popped");
    }
    this.current.setEnd(end);
 
    const current = this.current;
    this.current = this.current.getParent();
    return new SpaghettiScope(current);
  }
}