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

86.21% Statements 375/435
82.8% Branches 390/471
100% Functions 14/14
86.21% Lines 375/435

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 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743  1x   1x 1x     1x 1x 1x 1x 1x 1x 1x 1x   1x         1236x 1236x       10x 4x     6x 6x 4x     2x                           2x       61x       61x 61x 1x   61x 1x   61x 61x   61x   61x       61x 61x 14x   47x 47x 47x   47x 36x     47x 2x 45x 3x 42x 11x       47x 6x 3x 3x 3x     3x 1x 2x       2x                     2x 2x 2x             55x 6x 5x 5x   1x     49x       1053x 44x     1009x 1009x 70x     939x 939x   187x   4x   7x   2x           14x   6x       1x   1x   1x         306x   2x   3x 1x 2x 2x         118x 117x   1x     8x 7x   1x     2x 2x           277x 277x 148x   148x 4x 4x 4x 4x       148x 148x     129x 129x 1x 1x     128x 128x 128x   128x 100x   128x             827x 827x 625x   827x     827x 827x 3x     827x 825x 825x             151x 151x 151x       151x 151x 117x 34x 5x 29x 3x   151x         151x 151x       8x 8x 8x   143x       124x 124x 124x   19x       3x 3x 3x   16x         1x 15x 1x 1x 1x     1x           1x 14x 1x 1x 1x     1x           1x       13x       1060x   1060x 1060x 236x   1060x 114x 114x 1x     1060x 96x   1060x 76x     1060x 1060x 17x 17x 17x 2x   17x   17x   17x 11x 6x 5x   1x   1043x 1x 1x 1x     1x 1042x         7x 1035x 34x 34x 7x   34x 3x   34x 2x   34x   34x 2x   1001x 2x 2x 2x     2x 2x               999x 142x 857x 857x   855x 855x 6x 849x 4x 1x 3x 1x   2x       854x 44x   44x 44x 3x 3x 3x       44x 44x 1x           1030x             1009x   1009x 939x       70x 70x 53x 53x 53x   70x 70x     70x 53x 53x     53x     5x 5x 5x     5x       48x 48x 3x 45x 11x   34x   34x     34x 34x 34x 1x   33x       17x 17x 17x 12x 12x     12x     5x   17x     17x 11x 11x 6x         44x 44x 9x   9x     9x 9x     44x       14x       14x 14x 7x 7x 7x 7x 7x 6x 6x   1x 7x 7x 7x 7x 4x 1x   3x     3x   3x 3x 3x 3x 3x 3x 3x 2x 1x 1x 1x 1x                         150x       150x 150x 149x 15x   134x 134x 79x   79x       56x 56x 9x 47x       47x 1x     46x   34x     12x       77x 77x 4x     73x 1x     72x 72x 62x     10x 10x 10x             856x 856x 1x   855x       857x 857x   857x 1x     856x 4x 4x 4x                   852x 774x     78x 78x 72x     6x 6x 2x     4x 4x 4x 4x             82x 1x     81x 1x 80x 1x     81x      
/* eslint-disable default-case */
import {TypedIdentifier} from "../types/_typed_identifier";
import {StatementNode, ExpressionNode} from "../nodes";
import * as Expressions from "../2_statements/expressions";
import * as Types from "../types/basic";
import {CurrentScope} from "./_current_scope";
import {AbstractType} from "../types/basic/_abstract_type";
import {ScopeType} from "./_scope_type";
import {ObjectOriented} from "./_object_oriented";
import {ClassConstant} from "../types/class_constant";
import {Identifier} from "../1_lexer/tokens/identifier";
import {ReferenceType} from "./_reference";
import {TableAccessType, TableType, VoidType} from "../types/basic";
import {FieldChain} from "./expressions/field_chain";
import {ClassDefinition} from "../types";
 
export class BasicTypes {
  private readonly filename: string;
  private readonly scope: CurrentScope;
 
  public constructor(filename: string, scope: CurrentScope) {
    this.filename = filename;
    this.scope = scope;
  }
 
  public lookupQualifiedName(name: string | undefined): TypedIdentifier | undefined {
    if (name === undefined) {
      return undefined;
    }
 
    const found = this.scope.findType(name);
    if (found) {
      return found;
    }
 
    Iif (name.includes("=>")) {
      const split = name.split("=>");
      const ooName = split[0];
      const typeName = split[1];
      const oo = this.scope.findObjectDefinition(ooName);
      if (oo) {
        const f = oo.getTypeDefinitions().getByName(typeName);
        if (f) {
          return f;
        }
      }
    }
 
// todo: DDIC types
    return undefined;
  }
 
  public resolveLikeName(node: ExpressionNode | StatementNode | undefined, headerLogic = true): AbstractType | undefined {
    Iif (node === undefined) {
      return undefined;
    }
 
    let chain = node.findFirstExpression(Expressions.FieldChain);
    if (chain === undefined) {
      chain = node.findFirstExpression(Expressions.TypeName);
    }
    if (chain === undefined) {
      chain = node.findFirstExpression(Expressions.FieldSub);
    }
    const fullName = chain?.concatTokens();
    const children = chain?.getChildren();
 
    Iif (children === undefined) {
      return new Types.UnknownType("Type error, could not resolve \"" + fullName + "\", resolveLikeName1");
    } else Iif (chain === undefined) {
      throw new Error("resolveLikeName, chain undefined");
    }
 
    let type: AbstractType | undefined = undefined;
    if (children[1] && ( children[1].getFirstToken().getStr() === "=>" || children[1].getFirstToken().getStr() === "->")) {
      type = new FieldChain().runSyntax(chain, this.scope, this.filename, ReferenceType.TypeReference);
    } else {
      const name = children[0].getFirstToken().getStr();
      const found = this.scope.findVariable(name);
      type = found?.getType();
 
      if (found) {
        this.scope.addReference(chain?.getFirstToken(), found, ReferenceType.TypeReference, this.filename);
      }
 
      if (type instanceof TableType && chain.getLastChild()?.get() instanceof Expressions.TableBody) {
        type = new TableType(type.getRowType(), {withHeader: false});
      } else if (type instanceof TableType && type.isWithHeader() && headerLogic === true) {
        type = type.getRowType();
      } else if (type === undefined) {
        type = this.scope.getDDIC().lookupNoVoid(name);
      }
 
      // todo, this only looks up one level, reuse field_chain.ts?
      if (children[1] && children[2] && children[1].getFirstToken().getStr() === "-") {
        if (type instanceof Types.StructureType) {
          const sub = type.getComponentByName(children[2].getFirstToken().getStr());
          Eif (sub) {
            return sub;
          }
          return new Types.UnknownType("Type error, field not part of structure " + fullName);
        } else if (type instanceof Types.VoidType) {
          return type;
        } else Iif (type instanceof Types.TableType
            && type.isWithHeader() === true
            && type.getRowType() instanceof Types.VoidType) {
          return type.getRowType();
        } else Iif (type instanceof Types.TableType
            && type.isWithHeader() === true) {
          const rowType = type.getRowType();
          if (rowType instanceof Types.StructureType) {
            const sub = rowType.getComponentByName(children[2].getFirstToken().getStr());
            if (sub) {
              return sub;
            }
          }
          return new Types.UnknownType("Type error, field not part of structure " + fullName);
        } else {
          Eif (this.scope.isOO() === false && this.scope.getDDIC().inErrorNamespace(name) === false) {
            this.scope.addReference(children[0].getFirstToken(), undefined, ReferenceType.VoidType, this.filename);
            return new Types.VoidType(name);
          }
          return new Types.UnknownType("Type error, not a structure type " + name);
        }
      }
    }
 
    if (!type) {
      if (this.scope.isOO() === false && this.scope.getDDIC().inErrorNamespace(fullName) === false) {
        this.scope.addReference(children[0].getFirstToken(), undefined, ReferenceType.VoidType, this.filename);
        return new Types.VoidType(fullName);
      }
      return new Types.UnknownType("Type error, could not resolve \"" + fullName + "\", resolveLikeName2");
    }
 
    return type;
  }
 
  public resolveTypeName(typeName: ExpressionNode | undefined, length?: number, decimals?: number): AbstractType | undefined {
    if (typeName === undefined) {
      return undefined;
    }
 
    const chain = this.resolveTypeChain(typeName);
    if (chain) {
      return chain;
    }
 
    const chainText = typeName.concatTokens().toUpperCase();
    switch (chainText) {
      case "STRING":
        return new Types.StringType();
      case "XSTRING":
        return new Types.XStringType();
      case "D":
        return new Types.DateType();
      case "T":
        return new Types.TimeType();
      case "XSEQUENCE":
        return new Types.XSequenceType();
      case "CLIKE":
        return new Types.CLikeType();
      case "ANY":
        return new Types.AnyType();
      case "DATA":
        return new Types.AnyType();
      case "NUMERIC":
        return new Types.NumericGenericType();
      case "UTCLONG": // todo, take version into account
        return new Types.UTCLongType();
      case "DECFLOAT16":
        return new Types.DecFloat16Type();
      case "DECFLOAT34":
        return new Types.DecFloat34Type();
      case "CSEQUENCE":
        return new Types.CSequenceType();
      case "I":
      case "INT8": // todo, take version into account
        return new Types.IntegerType();
      case "F":
        return new Types.FloatType();
      case "P":
        if (length && decimals) {
          return new Types.PackedType(length, decimals);
        } else Eif (length) {
          return new Types.PackedType(length, 0);
        } else {
          return new Types.PackedType(1, 0);
        }
      case "C":
        if (length) {
          return new Types.CharacterType(length);
        } else {
          return new Types.CharacterType(1);
        }
      case "X":
        if (length) {
          return new Types.HexType(length);
        } else {
          return new Types.HexType(1);
        }
      case "N":
        Eif (length) {
          return new Types.NumericType(length);
        } else {
          return new Types.NumericType(1);
        }
    }
 
    const typ = this.scope.findType(chainText);
    if (typ) {
      const token = typeName.getFirstToken();
 
      if (chainText.includes("~")) {
        const name = chainText.split("~")[0];
        const idef = this.scope.findInterfaceDefinition(name);
        Eif (idef) {
          this.scope.addReference(token, idef, ReferenceType.ObjectOrientedReference, this.filename, {ooType: "INTF", ooName: name});
        }
      }
 
      this.scope.addReference(token, typ, ReferenceType.TypeReference, this.filename);
      return typ.getType();
    }
 
    const type = this.scope.findTypePoolType(chainText);
    if (type) {
      this.scope.addReference(typeName.getFirstToken(), typ, ReferenceType.TypeReference, this.filename);
      return type;
    }
 
    const ddic = this.scope.getDDIC().lookup(chainText);
    Eif (ddic) {
      Iif (ddic instanceof TypedIdentifier) {
        this.scope.addReference(typeName.getFirstToken(), ddic, ReferenceType.TypeReference, this.filename);
      } else if (ddic instanceof VoidType) {
        this.scope.addReference(typeName.getFirstToken(), undefined, ReferenceType.VoidType, this.filename);
      }
      return ddic;
    }
 
    return undefined;
  }
 
  public simpleType(node: StatementNode | ExpressionNode): TypedIdentifier | undefined {
    let nameExpr = node.findFirstExpression(Expressions.NamespaceSimpleName);
    if (nameExpr === undefined) {
      nameExpr = node.findFirstExpression(Expressions.DefinitionName);
    }
    Iif (nameExpr === undefined) {
      return undefined;
    }
    let name = nameExpr.getFirstToken();
    if (nameExpr.countTokens() > 1) { // workaround for names with dashes
      name = new Identifier(name.getStart(), nameExpr.concatTokens());
    }
 
    const found = this.parseType(node);
    Eif (found) {
      return new TypedIdentifier(name, this.filename, found);
    }
 
    return undefined;
  }
 
  public parseTable(node: ExpressionNode | StatementNode, name?: string): AbstractType | undefined {
    const typename = node.findFirstExpression(Expressions.TypeName);
    const text = node.findFirstExpression(Expressions.TypeTable)?.concatTokens().toUpperCase();
    Iif (text === undefined) {
      return undefined;
    }
 
    let type: Types.TableAccessType | undefined = undefined;
    if (text.includes(" STANDARD TABLE ")) {
      type = TableAccessType.standard;
    } else if (text.includes(" SORTED TABLE ")) {
      type = TableAccessType.sorted;
    } else if (text.includes(" HASHED TABLE ")) {
      type = TableAccessType.hashed;
    }
    const options: Types.ITableOptions = {
      withHeader: text.includes("WITH HEADER LINE"),
      type: type,
    };
 
    let found: AbstractType | undefined = undefined;
    if (text.startsWith("TYPE TABLE OF REF TO ")
        || text.startsWith("TYPE STANDARD TABLE OF REF TO ")
        || text.startsWith("TYPE SORTED TABLE OF REF TO ")
        || text.startsWith("TYPE HASHED TABLE OF REF TO ")) {
      found = this.resolveTypeRef(typename);
      Eif (found) {
        return new Types.TableType(found, options, name);
      }
    } else if (text.startsWith("TYPE TABLE OF ")
        || text.startsWith("TYPE STANDARD TABLE OF ")
        || text.startsWith("TYPE SORTED TABLE OF ")
        || text.startsWith("TYPE HASHED TABLE OF ")) {
      found = this.resolveTypeName(typename);
      Eif (found) {
        return new Types.TableType(found, options, name);
      }
    } else if (text.startsWith("LIKE TABLE OF ")
        || text.startsWith("LIKE STANDARD TABLE OF ")
        || text.startsWith("LIKE SORTED TABLE OF ")
        || text.startsWith("LIKE HASHED TABLE OF ")) {
      found = this.resolveLikeName(node);
      Eif (found) {
        return new Types.TableType(found, options, name);
      }
    } else if (text === "TYPE STANDARD TABLE"
        || text === "TYPE SORTED TABLE"
        || text === "TYPE HASHED TABLE"
        || text === "TYPE INDEX TABLE"
        || text === "TYPE ANY TABLE") {
      return new Types.TableType(new Types.AnyType(), options);
    } else if (text.startsWith("TYPE RANGE OF ")) {
      const sub = node.findFirstExpression(Expressions.TypeName);
      found = this.resolveTypeName(sub);
      Iif (found === undefined) {
        return new Types.UnknownType("TYPE RANGE OF, could not resolve type");
      }
      const structure = new Types.StructureType([
        {name: "sign", type: new Types.CharacterType(1)},
        {name: "option", type: new Types.CharacterType(2)},
        {name: "low", type: found},
        {name: "high", type: found},
      ], name);
      return new Types.TableType(structure, options);
    } else if (text.startsWith("LIKE RANGE OF ")) {
      const sub = node.findFirstExpression(Expressions.FieldSub);
      found = this.resolveLikeName(sub);
      Iif (found === undefined) {
        return new Types.UnknownType("LIKE RANGE OF, could not resolve type");
      }
      const structure = new Types.StructureType([
        {name: "sign", type: new Types.CharacterType(1)},
        {name: "option", type: new Types.CharacterType(2)},
        {name: "low", type: found},
        {name: "high", type: found},
      ], name);
      return new Types.TableType(structure, options);
    }
 
    // fallback to old style syntax, OCCURS etc
    return this.parseType(node, name);
  }
 
  public parseType(node: ExpressionNode | StatementNode, name?: string): AbstractType | undefined {
    const typename = node.findFirstExpression(Expressions.TypeName);
 
    let text = node.findFirstExpression(Expressions.Type)?.concatTokens().toUpperCase();
    if (text === undefined) {
      text = node.findFirstExpression(Expressions.TypeParam)?.concatTokens().toUpperCase();
    }
    if (text === undefined) {
      text = node.findFirstExpression(Expressions.TypeTable)?.concatTokens().toUpperCase();
      if (text?.startsWith("TYPE") === false && text?.startsWith("LIKE") === false) {
        text = "TYPE";
      }
    }
    if (text === undefined) {
      text = node.findFirstExpression(Expressions.FormParamType)?.concatTokens().toUpperCase();
    }
    if (text === undefined) {
      text = "TYPE";
    }
 
    let found: AbstractType | undefined = undefined;
    if (text.startsWith("LIKE LINE OF ")) {
      const name = node.findFirstExpression(Expressions.FieldChain)?.concatTokens();
      let e = node.findFirstExpression(Expressions.Type);
      if (e === undefined) {
        e = node.findFirstExpression(Expressions.FormParamType);
      }
      const type = this.resolveLikeName(e, false);
 
      Iif (type === undefined) {
        return new Types.UnknownType("Type error, could not resolve \"" + name + "\", parseType");
      } else if (type instanceof Types.TableType) {
        return type.getRowType();
      } else if (type instanceof Types.VoidType) {
        return type;
      } else {
        return new Types.UnknownType("Type error, not a table type " + name);
      }
    } else if (text.startsWith("LIKE REF TO ")) {
      const name = node.findFirstExpression(Expressions.FieldChain)?.concatTokens();
      const type = this.resolveLikeName(node.findFirstExpression(Expressions.Type), false);
      Iif (type === undefined) {
        return new Types.UnknownType("Type error, could not resolve \"" + name + "\", parseType");
      }
      return new Types.DataReference(type);
    } else if (text === "TYPE STANDARD TABLE"
        || text === "TYPE SORTED TABLE"
        || text === "TYPE HASHED TABLE"
        || text === "TYPE INDEX TABLE"
        || text === "TYPE ANY TABLE") {
      return new Types.TableType(new Types.AnyType(), {withHeader: node.concatTokens().toUpperCase().includes("WITH HEADER LINE")});
    } else if (text.startsWith("LIKE ")) {
      let sub = node.findFirstExpression(Expressions.Type);
      if (sub === undefined) {
        sub = node.findFirstExpression(Expressions.FormParamType);
      }
      if (sub === undefined) {
        sub = node.findFirstExpression(Expressions.TypeParam);
      }
      if (sub === undefined) {
        sub = node.findFirstExpression(Expressions.FieldChain);
      }
      found = this.resolveLikeName(sub);
 
      if (found && text.includes(" OCCURS ")) {
        found = new Types.TableType(found, {withHeader: text.includes("WITH HEADER LINE")}, name);
      }
    } else if (text.startsWith("TYPE LINE OF ")) {
      const sub = node.findFirstExpression(Expressions.TypeName);
      found = this.resolveTypeName(sub);
      Iif (found instanceof TypedIdentifier) {
        found = found.getType();
      }
      Eif (found instanceof Types.TableType) {
        return found.getRowType();
      } else if (found instanceof Types.VoidType) {
        return found;
      } else if (found instanceof Types.UnknownType) {
        return new Types.UnknownType("TYPE LINE OF, unknown type, " + found.getError());
      } else {
        return new Types.UnknownType("TYPE LINE OF, unexpected type, " + found?.constructor.name);
      }
    } else if (text.startsWith("TYPE REF TO ")) {
      found = this.resolveTypeRef(typename);
    } else Eif (text.startsWith("TYPE")) {
      found = this.resolveTypeName(typename, this.findLength(node), this.findDecimals(node));
 
      const concat = node.concatTokens().toUpperCase();
      if (found && concat.includes(" OCCURS ")) {
        found = new Types.TableType(found, {withHeader: concat.includes("WITH HEADER LINE")}, name);
      } else if (found && concat.includes("WITH HEADER LINE")) {
        if (found instanceof Types.VoidType) {
          found = new Types.TableType(found, {withHeader: true});
        } else if (!(found instanceof Types.TableType)) {
          throw new Error("WITH HEADER LINE can only be used with internal table");
        } else {
          found = new Types.TableType(found.getRowType(), {withHeader: true});
        }
      }
 
      if (found === undefined && typename === undefined) {
        let length = 1;
 
        const len = node.findDirectExpression(Expressions.ConstantFieldLength);
        if (len) {
          const int = len.findDirectExpression(Expressions.Integer);
          Eif (int) {
            length = parseInt(int.concatTokens(), 10);
          }
        }
 
        found = new Types.CharacterType(length, name); // fallback
        if (concat.includes(" OCCURS ")) {
          found = new Types.TableType(found, {withHeader: concat.includes("WITH HEADER LINE")}, name);
        }
      }
 
    }
 
    return found;
  }
 
/////////////////////
 
  // todo, rewrite this method
  private resolveTypeChain(expr: ExpressionNode): AbstractType | undefined {
    const chainText = expr.concatTokens().toUpperCase();
 
    if (chainText.includes("=>") === false && chainText.includes("-") === false) {
      return undefined;
    }
 
    let className: string | undefined;
    let rest = chainText;
    if (chainText.includes("=>")) {
      const split = chainText.split("=>");
      className = split[0];
      rest = split[1];
    }
    const subs = rest.split("-");
    let foundType: AbstractType | undefined = undefined;
 
 
    if (className) {
      const split = chainText.split("=>");
      const className = split[0];
 
    // the prefix might be itself
      if ((this.scope.getType() === ScopeType.Interface
          || this.scope.getType() === ScopeType.ClassDefinition)
          && this.scope.getName().toUpperCase() === className.toUpperCase()) {
        const foundId = this.scope.findType(subs[0]);
        foundType = foundId?.getType();
        Iif (foundType === undefined) {
          return new Types.UnknownType("Could not resolve type " + chainText);
        }
        this.scope.addReference(expr.getTokens()[2], foundId, ReferenceType.TypeReference, this.filename);
 
      } else {
    // lookup in local and global scope
        const obj = this.scope.findObjectDefinition(className);
        if (obj === undefined && this.scope.getDDIC().inErrorNamespace(className) === false) {
          return new Types.VoidType(className);
        } else if (obj === undefined) {
          return new Types.UnknownType("Could not resolve top " + className + ", resolveTypeChain");
        }
        const type = obj instanceof ClassDefinition ? "CLAS" : "INTF";
 
        this.scope.addReference(expr.getFirstToken(), obj, ReferenceType.ObjectOrientedReference, this.filename,
                                {ooType: type, ooName: className});
 
        const byName = new ObjectOriented(this.scope).searchTypeName(obj, subs[0]);
        foundType = byName?.getType();
        if (byName === undefined || foundType === undefined) {
          return new Types.UnknownType(subs[0] + " not found in class or interface");
        }
        this.scope.addReference(expr.getTokens()[2], byName, ReferenceType.TypeReference, this.filename);
 
      }
    } else {
      const found = this.scope.findType(subs[0]);
      foundType = found?.getType();
      if (foundType === undefined) {
        const f = this.scope.getDDIC().lookupTableOrView(subs[0]);
        Iif (f instanceof TypedIdentifier) {
          foundType = f.getType();
        } else {
          foundType = f;
        }
      } else {
        this.scope.addReference(expr.getFirstToken(), found, ReferenceType.TypeReference, this.filename);
      }
      Iif (foundType === undefined && this.scope.getDDIC().inErrorNamespace(subs[0]) === false) {
        this.scope.addReference(expr.getFirstToken(), undefined, ReferenceType.VoidType, this.filename);
        return new Types.VoidType(subs[0]);
      } else if (foundType instanceof Types.VoidType) {
        this.scope.addReference(expr.getFirstToken(), undefined, ReferenceType.VoidType, this.filename);
        return foundType;
      } else Iif (foundType === undefined) {
        return new Types.UnknownType("Unknown type " + subs[0]);
      }
    }
 
    subs.shift();
    while (subs.length > 0) {
      Iif (foundType instanceof Types.UnknownType) {
        return foundType;
      } else Iif (!(foundType instanceof Types.StructureType)) {
        return new Types.UnknownType("Not a structured type");
      }
      foundType = foundType.getComponentByName(subs[0]);
      subs.shift();
    }
 
    return foundType;
  }
 
  private resolveConstantValue(expr: ExpressionNode): string | undefined {
    Iif (!(expr.get() instanceof Expressions.SimpleFieldChain)) {
      throw new Error("resolveConstantValue");
    }
 
    const first = expr.getFirstChild()!;
    if (first.get() instanceof Expressions.Field) {
      const token = first.getFirstToken();
      const name = token.getStr();
      const found = this.scope.findVariable(name);
      const val = found?.getValue();
      if (typeof val === "string") {
        this.scope.addReference(token, found, ReferenceType.DataReadReference, this.filename);
        return val;
      }
      return undefined;
    } else Eif (first.get() instanceof Expressions.ClassName) {
      const name = first.getFirstToken().getStr();
      const obj = this.scope.findObjectDefinition(name);
      if (obj === undefined) {
        if (this.scope.getDDIC().inErrorNamespace(name) === true) {
          throw new Error("resolveConstantValue, not found: " + name);
        } else {
          return undefined;
        }
      }
      const children = expr.getChildren();
 
      const token = children[2]?.getFirstToken();
      const attr = token.getStr();
      const c = new ObjectOriented(this.scope).searchConstantName(obj, attr);
      Eif (c instanceof ClassConstant) {
        this.scope.addReference(token, c, ReferenceType.DataReadReference, this.filename);
        const val = c.getValue();
        if (typeof val === "string") {
          return val;
        } else Eif (typeof val === "object" && children[4]) {
          const name = children[4].getFirstToken().getStr();
          Eif (val[name] !== undefined) {
            return val[name];
          }
        }
        return undefined;
      }
      throw new Error("resolveConstantValue, constant not found " + attr);
 
    } else {
      throw new Error("resolveConstantValue, unexpected structure");
    }
  }
 
  private resolveTypeRef(chain: ExpressionNode | undefined): AbstractType | undefined {
    Iif (chain === undefined) {
      return undefined;
    }
 
    const name = chain.getFirstToken().getStr();
    if (chain.getAllTokens().length === 1) {
      if (name.toUpperCase() === "OBJECT") {
        return new Types.GenericObjectReferenceType();
      }
      const search = this.scope.existsObject(name);
      if (search.found === true && search.id) {
        this.scope.addReference(chain.getFirstToken(), search.id, ReferenceType.ObjectOrientedReference, this.filename,
                                {ooType: search.ooType, ooName: name});
        return new Types.ObjectReferenceType(search.id);
      }
    }
 
    const found = this.resolveTypeName(chain);
    if (found && !(found instanceof Types.UnknownType) && !(found instanceof Types.VoidType)) {
      return new Types.DataReference(found);
    } else Iif (chain.concatTokens().toUpperCase() === "DATA") {
      return new Types.DataReference(new Types.AnyType());
    }
 
    if (this.scope.isBadiDef(name) === true) {
      return new Types.VoidType(name);
    }
 
    if (this.scope.getDDIC()?.inErrorNamespace(name) === false) {
//      this.scope.addReference(chain.getFirstToken(), undefined, ReferenceType.VoidType, this.filename);
      return new Types.VoidType(name);
    }
 
    return new Types.UnknownType("REF, unable to resolve " + name);
  }
 
  public findValue(node: StatementNode): string | undefined {
    const val = node.findFirstExpression(Expressions.Value);
    if (val === undefined) {
      throw new Error("VALUE missing in expression");
    }
 
    if (val.concatTokens().toUpperCase() === "VALUE IS INITIAL") {
      return "";
    }
 
    const constant = val.findFirstExpression(Expressions.Constant);
    if (constant) {
      return constant.concatTokens();
    }
 
    const chain = val.findFirstExpression(Expressions.SimpleFieldChain);
    Eif (chain) {
      return this.resolveConstantValue(chain);
    }
 
    throw new Error("findValue, unexpected");
  }
 
  private findDecimals(node: StatementNode | ExpressionNode): number | undefined {
    const dec = node.findDirectExpression(Expressions.Decimals)?.findDirectExpression(Expressions.Integer)?.concatTokens();
    if (dec) {
      return parseInt(dec, 10);
    }
    return undefined;
  }
 
  private findLength(node: StatementNode | ExpressionNode): number | undefined {
    const val = node.findFirstExpression(Expressions.Length);
    const flen = node.findFirstExpression(Expressions.ConstantFieldLength);
 
    if (val && flen) {
      throw new Error("Only specify length once");
    }
 
    if (flen) {
      const cintExpr = flen.findFirstExpression(Expressions.Integer);
      Eif (cintExpr) {
        return this.parseInt(cintExpr.concatTokens());
      }
 
      const cchain = flen.findFirstExpression(Expressions.SimpleFieldChain);
      if (cchain) {
        const val = this.resolveConstantValue(cchain);
        return this.parseInt(val);
      }
    }
 
    if (val === undefined) {
      return 1;
    }
 
    const intExpr = val.findFirstExpression(Expressions.Integer);
    if (intExpr) {
      return this.parseInt(intExpr.concatTokens());
    }
 
    const strExpr = val.findFirstExpression(Expressions.ConstantString);
    if (strExpr) {
      return this.parseInt(strExpr.concatTokens());
    }
 
    const chain = val.findFirstExpression(Expressions.SimpleFieldChain);
    Eif (chain) {
      const val = this.resolveConstantValue(chain);
      return this.parseInt(val);
    }
 
    throw new Error("Unexpected, findLength");
  }
 
  private parseInt(text: string | undefined): number | undefined {
    if (text === undefined) {
      return undefined;
    }
 
    if (text.startsWith("'")) {
      text = text.split("'")[1];
    } else if (text.startsWith("`")) {
      text = text.split("`")[1];
    }
 
    return parseInt(text, 10);
  }
 
}