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

88.63% Statements 593/669
76.06% Branches 178/234
94.82% Functions 55/58
88.63% Lines 593/669

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 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 6701x 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 1x 1x 1x 1x 13120x 13120x 13120x 13120x 13120x 13120x 13120x 13120x 13120x 13120x 13120x 13120x 13120x 13120x 1x 1x 13120x 13120x 13120x 13120x 78720x 78720x 13120x 1x 1x 13120x 13120x 13120x 13120x 1x 1x 1x 1x 217x 217x 1x 1x     1x 1x 624x 624x 1x 1x 2323x 2323x 1x 1x 183x 183x 1x 1x 80725x     80725x 80725x 1x 1x 80754x     80754x     80754x 80754x 2x 80754x 2x 2x 80750x 80750x 1x 1x 8130x     8130x 8130x 1x 1x 8130x     8130x     8130x 8130x     8130x 8130x 1x 1x 6251x     6251x 6251x 1x 1x 6250x 6250x 1x 1x 4282x     4282x 4282x 1x 1x 1x 1x 4282x 4282x 12846x 12846x 6207x 4282x 4282x 4282x 6207x 8564x 8564x 4282x 1x 1x 2287x     2287x 2287x     2287x 2287x 1x 1x 576560x     576560x 576560x 3x 3x 576560x 2x 2x 576555x 576555x 1x 1x 1x     1x 1x     1x 1x 1x     1x     1x 1x 1x 1x 570710x 8x 8x 570702x 570702x 1x 1x 18x     18x 18x 1x 1x 17961x 5813x 5813x 17961x 1x 1x 76124x 492863x 492863x 76124x 1x 1x 69702x 69702x 69702x 69702x 69702x 69702x 69702x     69702x 69702x 69702x 7013x 7092x 7092x 69702x 62689x 62689x 69702x 1x 1x 4x 4x 1x 1x 1x 1x 1x     1x           1x 1x 1x 1x 3669x 203x 203x 3466x 3669x 3237x 3237x 229x 3669x 110x 110x 119x 119x 1x 1x 1x 51x 42x 42x 51x 51x 51x 1x 1x 16x 6x 6x 10x 10x 1x 1x 19x 19x 1x 1x 1x 1x 1x   18x 18x 1x 1x 15315x     15315x 15315x 15315x 15196x 15315x 101x 101x 15315x 15315x 15315x 14637x 10x 10x 14637x 14637x 678x 678x 15315x 37x 37x 641x 15315x 15315x 75x 2x 2x 75x 75x 566x 566x 15315x 14x 14x 552x 15315x 15315x 18x 18x 18x 3x 3x 18x 15x 15x 18x 18x 18x 534x 534x 534x 1x 1x 1x 1x 1x 49782x 3442x 3442x 46340x 49782x 49782x 43781x 43781x 2559x 2559x 49782x 2234x 2234x 325x 325x 325x 1x 1x 1874x 1481x 1481x 393x 393x 1874x 114x 114x 279x 279x 1874x       279x 279x 1874x 278x 278x 1x 1874x 1874x 1874x 1874x 1874x 1x 1x 1253x 1016x 1016x 237x 237x 237x 1253x 133x 133x 104x 104x 1253x 1x 1x 1x 103x 1253x 6x 6x 6x 6x 97x 97x 1253x 91x 91x 6x 1253x 1253x 1253x 1253x 1253x 1x 1x 1x 28135x     28135x 28135x 28135x 23775x 23775x 4360x 4360x 28135x 196x 196x 4164x 4164x 4164x 1x 1x 156x 156x 1x 1x 10163x     10163x 10163x 1x 1x 27x     27x 27x 1x 1x 24135x 87x 87x 24048x 24135x 24135x 22174x 22174x 1874x 1874x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 56366x 56366x 1x 1x 1043x 1043x 1x 1x 32x 32x 1x 1x 1056x 1056x 1x 1x 63723x     63723x 63723x 1x 1x 191266x     191266x 191266x 1x 1x 110254x 110254x 110254x 110254x 110254x 13120x 13120x 110254x 97134x 97134x 97134x 97134x 110254x 1x 1x 657310x 657310x 1266133x 1266133x 1266133x 1266133x 50352x 50352x 1215781x 1215781x 606958x 606958x 1x 1x 2006x 2006x 4072x 4072x 4072x 4072x 1058x 1058x 3014x 3014x 948x 948x 1x 1x 825x 825x 1650x 1650x 823x 1650x 2x 2x 825x 825x     1x 1x 6x 6x 6x 6x 6x 1x 1x 40x 40x 1x 1x 31x 31x 31x 31x 31x 31x         1x 1x 1712x 1712x 5163x 1352x 1352x 3811x 3811x 360x 360x 1x 1x     1x 1x 1975x 1975x 1x 1x 7x 7x 7x 1x 1x     1x 1x 96643x 96643x 96643x 96643x     96643x 96643x 96643x 96643x 96643x 96643x 1x  
import {IdentifierMeta, TypedIdentifier} from "../types/_typed_identifier";
import {ABAPRelease} from "../../version";
import {BuiltIn} from "./_builtin";
import {DDIC} from "../../ddic";
import {Position} from "../../position";
import {SpaghettiScope, SpaghettiScopeNode} from "./spaghetti_scope";
import {AbstractToken} from "../1_lexer/tokens/abstract_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 {IObject} from "../../objects/_iobject";
import {Class} from "../../objects/class";
import {BehaviorDefinition} from "../../objects/behavior_definition";
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";
import {FunctionGroup} from "../../objects";
import {IMSAGReferences} from "../../_imsag_references";
 
export class CurrentScope {
  protected readonly reg: IRegistry;
  protected current: SpaghettiScopeNode | undefined;
  protected allowHeaderUse: string | undefined;
  protected parentObj: IObject;
  private readonly localFriends: Map<string, string[]> = new Map();
 
  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 getRelease(): ABAPRelease {
    return this.reg.getConfig().getRelease();
  }
 
  public getVersion() {
    return this.reg.getConfig().getVersion();
  }
 
  public getLanguageVersion() {
    return this.reg.getConfig().getLanguageVersion();
  }
 
  public getOpenABAP() {
    return this.reg.getConfig().isOpenABAP();
  }
 
  public getRegistry(): IRegistry {
    return this.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`);
    } else if (this.isOO() && this.current.getData().vars[upper] !== undefined) {
      throw new Error(`"${name}" already defined`);
    }
    this.current.setType(upper, type);
  }
 
  public addExtraLikeType(type: TypedIdentifier | undefined) {
    if (type === undefined) {
      return;
    }
    this.addExtraLikeTypeNamed(type.getName(), type);
  }
 
  public addExtraLikeTypeNamed(name: string, type: TypedIdentifier | undefined) {
    if (type === undefined) {
      return;
    }
    if (this.current === undefined) {
      return;
    }
    const upper = name.toUpperCase();
    if (this.current.getData().extraLikeTypes[upper] !== undefined) {
      throw new Error(`Type name "${name}" already defined`);
    }
    this.current.setExtraLikeType(upper, type);
  }
 
  public addClassDefinition(c: IClassDefinition) {
    if (this.current === undefined) {
      return;
    }
    const name = c.getName().toUpperCase();
    if (this.current.getData().cdefs[name] !== undefined) {
      throw new Error(`Class "${name}" already defined`);
    }
    this.current.setClassDefinition(name, c);
  }
 
  public addFormDefinitions(f: readonly IFormDefinition[]) {
    if (this.current === undefined) {
      return;
    }
    this.current.addFormDefinitions(f);
  }
 
  /** The FORM definitions are built up front, at that point the program level types are not known.
   * When the FORM statement is traversed it is replaced with one resolved in the scope of the FORM */
  public updateFormDefinition(f: IFormDefinition) {
    let search = this.current;
    while (search !== undefined) {
      const forms = search.getData().forms;
      for (let i = 0; i < forms.length; i++) {
        if (forms[i].equals(f)) {
          forms[i] = f;
          return;
        }
      }
      search = search.getParent();
    }
  }
 
  public addInterfaceDefinition(i: IInterfaceDefinition) {
    if (this.current === undefined) {
      return;
    }
    const name = i.getName().toUpperCase();
    if (this.current.getData().cdefs[name] !== undefined) {
      throw new Error(`Interface "${name}" already defined`);
    }
    this.current.setInterfaceDefinition(name, i);
  }
 
  public addNamedIdentifier(name: string, identifier: TypedIdentifier) {
    if (this.current === undefined) {
      return;
    }
    const upper = name.toUpperCase();
    if (this.current.getData().vars[upper] !== undefined) {
//      console.dir(new Error().stack);
      throw new Error(`Variable name "${name}" already defined`);
    } else if (this.isOO() && this.current.getData().types[upper] !== undefined) {
      throw new Error(`"${name}" already defined`);
    }
    this.current.setVariable(upper, identifier);
  }
 
  public addNamedIdentifierToParent(name: string, identifier: TypedIdentifier) {
    if (this.current === undefined) {
      return;
    }
    const parent = this.current.getParent();
    if (parent === undefined) {
      return;
    }
 
    const upper = name.toUpperCase();
    if (parent.getData().vars[upper] !== undefined) {
//      console.dir(new Error().stack);
      throw new Error(`Variable name "${name}" already defined`);
    } else if (this.isOO() && parent.getData().types[upper] !== undefined) {
      throw new Error(`"${name}" already defined`);
    }
    parent.setVariable(upper, identifier);
  }
 
  public addIdentifier(identifier: TypedIdentifier | undefined) {
    if (identifier === undefined) {
      return;
    }
    this.addNamedIdentifier(identifier.getName(), identifier);
  }
 
  public addDeferred(token: AbstractToken | undefined, type: "CLAS" | "INTF") {
    if (token === undefined) {
      return;
    }
    this.current!.setDeferred(token.getStr().toUpperCase(), {token, ooType: type});
  }
 
  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: AbstractToken | undefined,
    referencing: Identifier | undefined,
    type: ReferenceType | ReferenceType[] | undefined,
    filename: string,
    extra?: IReferenceExtras) {
 
    if (usage === undefined || type === undefined) {
      return;
    }
 
    const position = new Identifier(usage, filename);
    if (Array.isArray(type)) {
      for (const t of type) {
        this.current?.addReference({position, resolved: referencing, referenceType: t, extra});
      }
    } else {
      this.current?.addReference({position, resolved: referencing, referenceType: type, extra});
    }
  }
 
  public addSQLConversion(fieldName: string, message: string, token: AbstractToken) {
    this.current?.addSQLConversion({fieldName, message, token});
  }
 
///////////////////////////
 
  public findFunctionModule(name: string | undefined) {
    if (name === undefined) {
      return undefined;
    }
    for (const fugr of this.reg.getObjectsByType("FUGR")) {
      const func = (fugr as FunctionGroup).getModule(name);
      if (func !== undefined) {
        return func;
      }
    }
    return undefined;
  }
 
  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;
  }
 
  /** the BDEF from "FOR BEHAVIOR OF" of the enclosing global class, undefined if not a behavior pool */
  public findBehaviorDefinition(): BehaviorDefinition | undefined {
    if (this.parentObj.getType() !== "CLAS") {
      return undefined;
    }
    const name = (this.parentObj as Class).getClassDefinition()?.behaviorDefinitionName;
    return this.findBehaviorDefinitionByName(name);
  }
 
  public findBehaviorDefinitionByName(name: string | undefined): BehaviorDefinition | undefined {
    if (name === undefined) {
      return undefined;
    }
    return this.reg.getObject("BDEF", name) as BehaviorDefinition | 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;
  }
 
  public existsObject(name: string | undefined): {id: Identifier | undefined, ooType?: IReferenceExtras["ooType"], RTTIName?: string} | undefined {
    if (name === undefined) {
      return undefined;
    }
 
    let prefixRTTI = "";
    if (this.parentObj.getType() === "PROG") {
      prefixRTTI = "\\PROGRAM=" + this.parentObj.getName();
    } else if (this.parentObj.getType() === "CLAS") {
      prefixRTTI = "\\CLASS-POOL=" + this.parentObj.getName();
    }
 
    const findLocalClass = this.current?.findClassDefinition(name);
    if (findLocalClass) {
      if (findLocalClass.isGlobal() === true) {
        prefixRTTI = "";
      }
      return {id: findLocalClass, ooType: "CLAS", RTTIName: prefixRTTI + "\\CLASS=" + findLocalClass.getName()};
    }
 
    const globalClas = this.reg.getObject("CLAS", name);
    if (globalClas) {
      return {id: globalClas.getIdentifier(), ooType: "CLAS", RTTIName: "\\CLASS=" + globalClas.getName()};
    }
 
    const findLocalInterface = this.current?.findInterfaceDefinition(name);
    if (findLocalInterface) {
      if (findLocalInterface.isGlobal() === true) {
        prefixRTTI = "";
      }
      return {id: findLocalInterface, ooType: "INTF", RTTIName: prefixRTTI + "\\INTERFACE=" + findLocalInterface.getName()};
    }
 
    const globalIntf = this.reg.getObject("INTF", name);
    if (globalIntf) {
      return {id: globalIntf.getIdentifier(), ooType: "INTF", RTTIName: "\\INTERFACE=" + globalIntf.getName()};
    }
 
    const def = this.current?.findDeferred(name);
    if (def !== undefined) {
      let rttiName = prefixRTTI;
      switch (def.ooType) {
        case "INTF":
          rttiName = rttiName + "\\INTERFACE=" + name;
          break;
        default:
          rttiName = rttiName + "\\CLASS=" + name;
          break;
      }
      return {id: def.id, ooType: def.ooType, RTTIName: rttiName};
    }
 
    return undefined;
  }
 
///////////////////////////
 
  /** 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("_") === false) {
      return undefined;
    }
 
    const typePoolName = name.split("_")[0];
    if (typePoolName.length <= 1 || typePoolName.length > 5) {
      return undefined;
    }
 
    if (this.parentObj.getType() === "TYPE"
        && this.parentObj.getName().toUpperCase() === typePoolName.toUpperCase()) {
// dont recurse into itself
      return undefined;
    }
 
    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()?.getFirstChild();
 
    const found = spag?.findVariable(name);
    return found;
  }
 
  public findTypePoolType(name: string): TypedIdentifier | undefined {
    if (name.includes("_") === false) {
      return undefined;
    }
 
    const typePoolName = name.split("_")[0];
 
    if (typePoolName.length <= 2 || typePoolName.length > 5) {
      return undefined;
    }
 
    if (this.parentObj.getType() === "TYPE"
        && this.parentObj.getName().toUpperCase() === typePoolName.toUpperCase()) {
// dont recurse into itself
      return undefined;
    }
 
    if (new DDIC(this.reg).lookupNoVoid(name) !== undefined) {
      // this is tricky, it should not do recursion when parsing the type pool itself,
      // think about DTEL ABAP_ENCOD vs TYPE ABAP
      return undefined;
    }
 
    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()?.getFirstChild();
 
    const found = spag?.findType(name);
    return found;
  }
 
  /** Lookup interface in local and global scope */
  public findInterfaceDefinition(name: string | undefined): IInterfaceDefinition | undefined {
    if (name === undefined) {
      return 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 findExtraLikeType(name: string | undefined): TypedIdentifier | undefined {
    if (name === undefined) {
      return undefined;
    }
    return this.current?.findExtraLikeType(name);
  }
 
  public findVariable(name: string | undefined): TypedIdentifier | undefined {
    if (name === undefined) {
      return undefined;
    }
    name = name.replace(/^!/, "");
    const found = this.current?.findVariable(name);
    if (found) {
      return found;
    }
    return this.findTypePoolConstant(name);
  }
 
  public findReturningParameter(): TypedIdentifier | undefined {
    for (const v in this.current?.getData().vars || {}) {
      const variable = this.current!.getData().vars[v];
      if (variable.getMeta().includes(IdentifierMeta.MethodReturning)) {
        return variable;
      }
    }

    return undefined;
  }
 
///////////////////////////
 
  public getDDIC(): DDIC {
    return new DDIC(this.reg);
  }
 
  public getDDICReferences(): IDDICReferences {
    return this.reg.getDDICReferences();
  }
 
  public getMSAGReferences(): IMSAGReferences {
    return this.reg.getMSAGReferences();
  }
 
  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};
 
//    console.dir("push scope, " + stype);
 
    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 isAnyOO(): 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 isInStaticMethod(): boolean {
    let curr = this.current;
    while (curr !== undefined) {
      const stype = curr.getIdentifier().stype;
      if (stype === ScopeType.MethodInstance) {
        return false;
      } else if (stype === ScopeType.ClassImplementation) {
        return true;
      }
      curr = curr.getParent();
    }
    return false;
  }
 
  public addLocalFriend(className: string, friendName: string): void {
    const key = className.toUpperCase();
    const list = this.localFriends.get(key) ?? [];
    list.push(friendName.toUpperCase());
    this.localFriends.set(key, list);
  }
 
  public isLocalFriend(className: string, friendName: string): boolean {
    return this.localFriends.get(className.toUpperCase())?.includes(friendName.toUpperCase()) === true;
  }
 
  public getEnclosingMethodName(): string | undefined {
    let curr = this.current;
    while (curr !== undefined) {
      const stype = curr.getIdentifier().stype;
      if (stype === ScopeType.Method || stype === ScopeType.MethodInstance) {
        return curr.getIdentifier().sname;
      }
      curr = curr.getParent();
    }
    return undefined;
  }
 
  public getEnclosingClassName(): string | undefined {
    let curr = this.current;
    while (curr !== undefined) {
      if (curr.getIdentifier().stype === ScopeType.ClassImplementation) {
        return curr.getIdentifier().sname;
      }
      curr = curr.getParent();
    }
    return undefined;
  }
 
  public isGlobalOO(): boolean {
    return this.parentObj.getType() === "INTF" || this.parentObj.getType() === "CLAS";
  }
 
  public isTypePool(): boolean {
    return this.current?.getIdentifier().filename.endsWith(".type.abap") === true || 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 {
//    console.dir("pop scope, " + this.current?.getIdentifier().stype);
 
    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);
  }
}