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

92.36% Statements 944/1022
90.62% Branches 599/661
100% Functions 32/32
92.36% Lines 944/1022

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 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 10231x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10802x 10802x 1x 1x 302x   302x 9x 15x 2x 2x 15x 7x 302x 5x 293x 288x 288x 288x 288x 288x 288x 288x 288x 288x 288x 288x 288x 288x 270x 270x 18x 18x 1x 1x 17x 17x 1x 1x 16x 17x 17x 17x 17x 17x 17x 17x 17x 17x 1x 1x 5x 5x 5x 5x 5x 5x 5x 1x 1x 680x   680x 2x 680x 29x 47x 1x 1x 47x 28x 678x 649x 649x 649x 649x 649x 649x 649x 649x 649x 649x 649x 649x 649x 649x 649x 649x 649x 649x 649x 649x 649x 649x 649x 633x 633x 16x 16x 1x 1x 56x   56x             56x 1x 56x 55x 55x 55x 55x 55x 55x 41x 41x 14x 14x 1x 1x 1x 1781x 1781x 1x 1x 1303x 1303x 1303x 1303x 1303x 1303x 1303x 1240x 1240x 63x 63x 63x 63x 1303x 1x 1x 1x 1x     1x 63x 1303x 1x 1x 1x 1x     1x 63x 1303x 48x     48x 48x 46x 46x 1303x 15x 11x 11x 4x 4x 4x 4x 15x                       2x 2x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 52x 52x 52x 53x 53x 53x 52x 52x 1x 1x 176x 176x 1x 1x 1x 175x 175x 175x 1x 1x 460x 460x 460x 1x 1x 942x 942x 942x 942x 942x 942x     942x 942x 1x 1x 942x 1x 1x 940x 940x 940x 940x 940x 1x 1x 699x 1336x 201x 201x 1336x 498x 498x 1x 1x 1x 2x     2x 1x 1x 1x 1x 1397x 1397x 1397x 1397x 1397x 1397x 1397x 1397x 1397x 1397x 1397x 1x 1x 1396x 1397x 86x 86x 8x 8x 86x 1388x 1388x 1397x 1397x 1397x 1397x 1x 1x 1x 1014x 111x 111x 903x 1014x 715x 715x 188x 188x 1014x 23x 23x 1014x 38x 38x 6x 6x 38x 159x 159x 1x 1x 1x 1x 1x 159x 301x 301x 301x 301x 159x 159x 159x 159x 50x 159x 15x 109x 10x 94x 7x 84x 32x 77x 7x 45x 9x 38x 4x 29x 8x 25x 17x 17x 142x 159x 54x 159x 12x 88x 3x 76x 3x 73x 4x 70x 24x 66x 2x 42x 4x 40x 1x 36x 5x 35x 4x 30x 3x 26x 7x 23x 4x 16x 9x 9x 3x 3x 1x 1x 1x 1x 17x 17x     17x 17x 17x 17x 17x 17x     1x 1x 5533x 5533x 5533x 5533x 5533x 1x 1x 5532x 5532x 5532x 1x 1x 1830x 1830x 1830x 1830x 1830x 1830x 1830x 1830x 1830x 1830x 1830x 6x 6x 1824x 1830x 2x 2x 1822x 1822x 1830x 1830x 4x 4x 4x 4x 4x 3x 3x 4x 1819x 1830x 1830x 59x 59x 1760x 1760x 1830x 100x 100x 100x 1830x 1830x 191x 191x 1569x 1569x 1830x 20x 3x 20x 17x 17x 17x 17x 17x 17x 17x 7x 7x 20x 1559x 1559x 1559x 1830x 1830x 8x 8x 1551x 1830x 171x 171x 1380x 1830x 3x 3x 3x 3x 3x 3x 3x 1377x 1830x 1x 1830x 240x 202x 4x 4x 198x 240x 3x 1x 1x 2x 38x 7x 6x 6x 1x 35x 6x 6x 6x   28x 8x 5x 5x 3x 3x 1376x 14x 9x     9x 14x 3x 5x 1x     1x 1x 1136x 9x 1x 1x 1122x 142x   142x 1x     1x 142x 2x 1x 1x 1x 141x 139x 3x 139x 6x 5x 5x 1x 136x 130x 130x 3x 3x 127x 1113x 65x 51x 51x 51x 8x 8x 51x 262x 1x 1x 262x 42x 65x 1x 14x   13x 13x 13x 13x 7x 7x 6x 971x 5x 5x 5x 3x 3x 906x 478x 478x 478x 3x 478x 4x 2x 2x 2x 2x 901x 4x 4x 423x 8x 1x 1x 419x 5x 5x 5x 5x 5x 3x 3x 411x 186x 186x 186x 186x 186x 2x 2x 406x 13x 13x 13x 13x 13x 13x 5x 5x 220x 20x 207x 109x 13x 109x 3x 3x 96x 53x 53x 93x 32x 32x 32x 32x 32x 5x 5x 32x 32x 1x 1x 26x 40x 8x 8x 2x 2x 109x 781x 781x 1x 1x 1x 1x 101x 101x 101x 101x 101x 101x 101x 101x 56x 101x 2x 45x 3x 3x 40x 40x 1x 1x 1x 20x 20x 20x 8x 20x 5x 5x 7x 7x 1x 1x 1x 1x 9x 9x 9x 9x   9x 3x 3x 6x 9x 9x 9x 1x 9x   5x 2x 2x 2x     2x 3x 3x 1x 1x 3x 5x     3x     4x 4x 1x 1x 1x 371x 371x 371x 371x 371x 2x 2x 369x 369x 1x 1x 1x 1x 1x 1x 52x 52x 52x 26x 26x 26x 4x 4x 26x 42x 3x 3x 42x 19x 26x 14x 26x 4x 4x 4x 4x 8x 8x 8x 1x 1x 1x 4x 4x 6x 6x 6x 6x                         6x 4x 4x 1x 1x 7345x 8x 8x 7337x 7337x 7337x 7337x 7337x 7345x 9x 9x 7328x 7345x 294x 6x 6x 288x 294x 294x 294x 10x 294x 274x 274x 274x 9x 274x     265x 274x 41x 1x 41x 40x 1x 1x 274x 224x               263x 263x 4x 7345x 1303x 7034x 5731x 176x 176x 176x 176x 176x 176x 168x 168x 8x 5731x 90x 90x 90x 90x 90x 86x 86x 4x 5555x 265x 1x 265x 264x 264x 264x 3x 264x 240x 240x 240x 1x 1x 239x 239x 239x 240x 6x     6x 239x 240x 522x 3x 3x 519x 522x   522x 1x 1x 522x 238x 238x 261x 2x 21x 1x 19x 5x 18x 9x 9x 4x 5465x 5200x 5200x 4402x 2x 4402x 4400x 4400x 6x 1x 1x 5x 4400x 4x 4394x 4390x 4390x 3x 3x 4387x 5200x 14x 14x 2x 2x 14x 796x 796x 796x 1x 1x 1x 9x   9x 7x 9x 2x 2x     1x 1x 18x 18x 18x 18x 18x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x  
import {ClassDefinition, InterfaceDefinition} from "../types";
import {AnyType, CharacterType, CLikeType, CSequenceType, DataReference, DataType, DateType, DecFloat16Type, DecFloat34Type, DecFloatType, FloatingPointType, FloatType, GenericObjectReferenceType, HexType, Integer8Type, IntegerType, NumericGenericType, NumericType, ObjectReferenceType, PackedType, PGenericType, SimpleType, StringType, StructureType, TableType, TimeType, UnknownType, UTCLongType, VoidType, XGenericType, XSequenceType, XStringType} from "../types/basic";
import {TableAccessType, TableKeyType} from "../types/basic/table_type";
import {EnumType} from "../types/basic/enum_type";
import {AbstractType} from "../types/basic/_abstract_type";
import {CGenericType} from "../types/basic/cgeneric_type";
import {CurrentScope} from "./_current_scope";
import * as Expressions from "../2_statements/expressions";
import {ExpressionNode, TokenNodeRegex} from "../nodes";
import {NGenericType} from "../types/basic/ngeneric_type";
import {BuiltIn} from "./_builtin";
 
// todo: refactor to static? for performance
export class TypeUtils {
  // scope is needed to determine class hieraracy for typing
  private readonly scope: CurrentScope;
 
  public constructor(scope: CurrentScope) {
    this.scope = scope;
  }
 
  public isCharLikeStrict(type: AbstractType | undefined): boolean {
    if (type === undefined) {
      return false;
    } else if (type instanceof StructureType) {
      for (const c of type.getComponents()) {
        if (this.isCharLikeStrict(c.type) === false) {
          return false;
        }
      }
      return true;
    } else if (type instanceof TableType && type.isWithHeader()) {
      return this.isCharLikeStrict(type.getRowType());
    } else if (type instanceof StringType
        || type instanceof AnyType
        || type instanceof DataType
        || type instanceof CharacterType
        || type instanceof SimpleType
        || type instanceof CGenericType
        || type instanceof CLikeType
        || type instanceof DateType
        || type instanceof CSequenceType
        || type instanceof NumericGenericType
        || type instanceof NumericType
        || type instanceof TimeType
        || type instanceof UnknownType
        || type instanceof VoidType) {
      return true;
    }
    return false;
  }
 
  public isCharLikeField(type: AbstractType | undefined): boolean {
    if (type instanceof StructureType
        || (type instanceof TableType && type.isWithHeader())) {
      return this.isCharLikeStrict(type);
    }
    return type instanceof CharacterType
      || type instanceof NumericType
      || type instanceof DateType
      || type instanceof TimeType
      || type instanceof CGenericType
      || type instanceof CLikeType
      || type instanceof AnyType
      || type instanceof UnknownType
      || type instanceof VoidType;
  }
 
  public isCharLikeForCompare(type: AbstractType | undefined): boolean {
    return type === undefined
      || type instanceof StringType
      || type instanceof CLikeType
      || type instanceof CSequenceType
      || type instanceof DataType
      || this.isCharLikeField(type);
  }
 
  public isCharLike(type: AbstractType | undefined): boolean {
    if (type === undefined) {
      return false;
    } else if (type instanceof TableType && type.isWithHeader()) {
      return this.isCharLike(type.getRowType());
    } else if (type instanceof StructureType) {
      for (const c of type.getComponents()) {
        if (this.isCharLike(c.type) === false) {
          return false;
        }
      }
      return true;
    } else if (type instanceof StringType
        || type instanceof CharacterType
        || type instanceof VoidType
        || type instanceof AnyType
        || type instanceof UnknownType
        || type instanceof NumericType
        || type instanceof IntegerType
        || type instanceof Integer8Type
        || type instanceof SimpleType
        || type instanceof FloatType
        || type instanceof FloatingPointType
        || type instanceof DecFloatType
        || type instanceof DecFloat16Type
        || type instanceof DecFloat34Type
        || type instanceof NumericGenericType
        || type instanceof CSequenceType
        || type instanceof CGenericType
        || type instanceof DateType
        || type instanceof DataType
        || type instanceof CLikeType
        || type instanceof PackedType
        || type instanceof PGenericType
        || type instanceof TimeType
        || type instanceof EnumType) {
      return true;
    }
    return false;
  }
 
  public isHexLike(type: AbstractType | undefined): boolean {
    if (type === undefined) {
      return false;
    } else if (type instanceof StructureType) {
      for (const c of type.getComponents()) {
        if (this.isHexLike(c.type) === false) {
          return false;
        }
      }
      return true;
    } else if (type instanceof TableType && type.isWithHeader()) {
      return this.isHexLike(type.getRowType());
    } else if (type instanceof XStringType
        || type instanceof HexType
        || type instanceof VoidType
        || type instanceof XGenericType
        || type instanceof XSequenceType
        || type instanceof AnyType
        || type instanceof UnknownType) {
      return true;
    }
    return false;
  }
 
  /** a number literal beyond the range of i */
  public isPackedConstant(type: AbstractType | undefined): boolean {
    return type instanceof PackedType && type.getAbstractTypeData()?.derivedFromConstant === true;
  }
 
  public isOOAssignable(source: ObjectReferenceType, target: ObjectReferenceType): boolean {
    let sid = source.getIdentifier();
    let tid = target.getIdentifier();
 
    const tname = tid.getName().toUpperCase();
    const sname = sid.getName().toUpperCase();
 
    if (tname === sname) {
      return true;
    }
/*
    console.dir(sid);
    console.dir(tid);
*/
    if (!(sid instanceof ClassDefinition || sid instanceof InterfaceDefinition)) {
      const found = this.scope.findObjectDefinition(sid.getName());
      if (found) {
        sid = found;
      } else {
        return false;
      }
    }
 
    if (!(tid instanceof ClassDefinition || tid instanceof InterfaceDefinition)) {
      const found = this.scope.findObjectDefinition(tid.getName());
      if (found) {
        tid = found;
      } else {
        return false;
      }
    }
 
    if (sid instanceof ClassDefinition && tid instanceof ClassDefinition) {
      if (sname === tname) {
        return true;
      }
      const slist = this.listAllSupers(sid);
      if (slist.indexOf(tname) >= 0) {
        return true;
      }
    } else if (sid instanceof ClassDefinition && tid instanceof InterfaceDefinition) {
      if (sid.getImplementing().some(i => i.name === tname)) {
        return true;
      }
      const slist = this.listAllInterfaces(sid);
      if (slist.indexOf(tname) >= 0) {
        return true;
      }
    } else if (sid instanceof InterfaceDefinition && tid instanceof InterfaceDefinition) {
      if (sname === tname) {
        return true;
      }
      if (sid.getImplementing().some(i => i.name === tname)) {
        return true;
      }
      const slist = this.listAllInterfaces(sid);
      if (slist.indexOf(tname) >= 0) {
        return true;
      }
    }
    return false;
  }
 
  private listAllInterfaces(cdef: ClassDefinition | InterfaceDefinition): string[] {
    const ret = new Set<string>();
    const stack: string[] = [];
 
    // initialize
    cdef.getImplementing().forEach(i => stack.push(i.name));
    if (cdef instanceof ClassDefinition) {
      const supers = this.listAllSupers(cdef);
      for (const s of supers) {
        this.scope.findClassDefinition(s)?.getImplementing().forEach(i => stack.push(i.name));
      }
    }
 
    // main loop
    while (stack.length > 0) {
      const intf = stack.pop()!.toUpperCase();
      ret.add(intf);
 
      const idef = this.scope.findInterfaceDefinition(intf);
      idef?.getImplementing().forEach(i => stack.push(i.name));
    }
 
    return Array.from(ret.values());
  }
 
  private listAllSupers(cdef: ClassDefinition): string[] {
    const ret: string[] = [];
    let sup = cdef.getSuperClass();
    while (sup !== undefined) {
      ret.push(sup?.toUpperCase());
      sup = this.scope.findClassDefinition(sup)?.getSuperClass()?.toUpperCase();
    }
    return ret;
  }
 
  public isConvable(source: AbstractType | undefined, target: AbstractType | undefined): boolean {
 
    if (source instanceof TableType && target instanceof TableType) {
// table key conversion, eg DEFAULT to EMPTY
      return this.isAssignable(source.getRowType(), target.getRowType());
    }
 
    return this.isAssignable(source, target);
  }
 
  public isCastable(_source: AbstractType | undefined, _target: AbstractType | undefined): boolean {
// todo
    return true;
  }
 
  public isCompareable(source1: AbstractType | undefined, source2: AbstractType | undefined,
                       node1: ExpressionNode, node2: ExpressionNode): boolean {
    /*
    console.dir(source1);
    console.dir(source2);
    */
    if (source1 === undefined || source2 === undefined) {
      return true;
    }
 
    if (source1 instanceof HexType && this.isCalculated(node1) && source2 instanceof IntegerType) {
      return false;
    }
    if (source2 instanceof HexType && this.isCalculated(node2) && source1 instanceof IntegerType) {
      return false;
    }
 
    // a date compared with an arithmetic expression is reported by Compare, a date compared with
    // a function or method result of type i is compared like a date compared with a data object of type i
    return true;
  }
 
  private structureContainsString(structure: StructureType): boolean {
    for (const c of structure.getComponents()) {
      if (c.type instanceof StringType) {
        return true;
      }
    }
    return false;
  }
 
  private structureContainsVoid(structure: StructureType): boolean {
    for (const c of structure.getComponents()) {
      if (c.type instanceof VoidType) {
        return true;
      }
    }
    return false;
  }
 
  private isCalculated(node: ExpressionNode): boolean {
    /*
    if (node.getChildren().length === 1
        && node.get() instanceof Expressions.Source
        && node.getFirstChild()?.get() instanceof Expressions.MethodCallChain) {
      return false;
    }
    */
 
    if (node.getChildren().length === 4
        && node.getFirstChild() instanceof TokenNodeRegex
        && node.getFirstChild()?.concatTokens().toUpperCase() === "BOOLC") {
      return true;
    }
 
    if (node.getChildren().length >= 3) {
      const second = node.getChildren()[1];
      if (second.getFirstToken().getStr() === "&&") {
        return true;
      }
    }
 
    const calculated = node.findFirstExpression(Expressions.MethodCallChain) !== undefined
      || node.findFirstExpression(Expressions.StringTemplate) !== undefined
      || node.findFirstExpression(Expressions.ArithOperator) !== undefined;
    return calculated;
  }
 
  /** the source is only a call of a functional method, not a built-in function */
  private isFunctionalMethodResult(node: ExpressionNode): boolean {
    if (!(node.get() instanceof Expressions.Source) || node.getChildren().length !== 1) {
      return false;
    }
    const chain = node.getFirstChild();
    if (!(chain instanceof ExpressionNode) || !(chain.get() instanceof Expressions.MethodCallChain)) {
      return false;
    }
    const children = chain.getChildren();
    const last = children[children.length - 1];
    if (!(last instanceof ExpressionNode) || !(last.get() instanceof Expressions.MethodCall)) {
      return false;
    }
    if (children.length === 1) {
      const name = last.findDirectExpression(Expressions.MethodName)?.concatTokens().toUpperCase();
      if (name === undefined || BuiltIn.searchBuiltin(name) !== undefined) {
        return false;
      }
    }
    return true;
  }
 
  /** the result of a functional method passed to a formal parameter: the result is expected to be of
   * the same kind as the parameter, numeric types mix, c, n and x only into the same or a longer length.
   * Returns false for a known mismatch of elementary types, true otherwise */
  private isFunctionalResultAssignable(source: AbstractType | undefined, target: AbstractType | undefined): boolean {
    const isNumber = (t: AbstractType | undefined) => t instanceof IntegerType
      || t instanceof Integer8Type
      || t instanceof PackedType
      || t instanceof FloatType
      || t instanceof DecFloat16Type
      || t instanceof DecFloat34Type;
 
    let kind: string;
    if (isNumber(source)) {
      kind = "number";
    } else if (source instanceof CharacterType) {
      kind = "c";
    } else if (source instanceof NumericType && !(source instanceof NGenericType)) {
      kind = "n";
    } else if (source instanceof HexType) {
      kind = "x";
    } else if (source instanceof StringType) {
      kind = "string";
    } else if (source instanceof XStringType) {
      kind = "xstring";
    } else if (source instanceof DateType) {
      kind = "d";
    } else if (source instanceof TimeType) {
      kind = "t";
    } else if (source instanceof UTCLongType) {
      kind = "utclong";
    } else {
      return true;
    }
 
    if (isNumber(target) || target instanceof NumericGenericType || target instanceof PGenericType) {
      return kind === "number";
    } else if (target instanceof CharacterType) {
      return source instanceof CharacterType && source.getLength() <= target.getLength();
    } else if (target instanceof NGenericType) {
      return kind === "n";
    } else if (target instanceof NumericType) {
      return source instanceof NumericType && source.getLength() <= target.getLength();
    } else if (target instanceof HexType) {
      return source instanceof HexType && source.getLength() <= target.getLength();
    } else if (target instanceof StringType) {
      return kind === "c" || kind === "string";
    } else if (target instanceof XStringType) {
      return kind === "x" || kind === "xstring";
    } else if (target instanceof DateType) {
      return kind === "d";
    } else if (target instanceof TimeType) {
      return kind === "t";
    } else if (target instanceof UTCLongType) {
      return kind === "utclong";
    } else if (target instanceof CGenericType) {
      return kind === "c";
    } else if (target instanceof XGenericType) {
      return kind === "x";
    } else if (target instanceof CSequenceType) {
      return kind === "c" || kind === "string";
    } else if (target instanceof XSequenceType) {
      return kind === "x" || kind === "xstring";
    } else if (target instanceof CLikeType) {
      return kind !== "number" && kind !== "x" && kind !== "xstring" && kind !== "utclong";
    }
    return true;
  }
 
  // a text or string literal is only assignable to an xstring or xsequence parameter if its
  // content is hexadecimal: one or more of 0-9 and A-F, upper case, nothing else, or ``
  private isHexConstant(node: ExpressionNode | undefined): boolean {
    const constant = node?.concatTokens();
    if (constant === undefined) {
      return true;
    }
    if ((constant.startsWith("'") && constant.endsWith("'"))
        || (constant.startsWith("`") && constant.endsWith("`"))) {
      const value = constant.substring(1, constant.length - 1);
      // an empty string literal is an empty byte string, while '' is a text field of one blank
      return /^[0-9A-F]+$/.test(value) || constant === "``";
    }
    return true;
  }
 
  public isAssignableNew(source: AbstractType | undefined,
                         target: AbstractType | undefined,
                         node: ExpressionNode | undefined): boolean {
    const calculated = node?.findFirstExpression(Expressions.ArithOperator) !== undefined;
 
    if (calculated && source instanceof HexType && target instanceof IntegerType) {
      return false;
    }
 
    return this.isAssignable(source, target);
  }
 
  public isAssignableStrict(source: AbstractType | undefined,
                            target: AbstractType | undefined,
                            node?: ExpressionNode,
                            builtin = false): boolean {
    const calculated = node ? this.isCalculated(node) : false;
/*
    console.dir(source);
    console.dir(target);
    console.dir(calculated);
*/
    if (calculated && source instanceof StringType
        && (target instanceof XStringType || target instanceof XSequenceType)) {
      return false;
    }
 
    if (calculated && source instanceof IntegerType && target instanceof DateType) {
      return false;
    }
 
    if (target instanceof NumericType
        && (source instanceof CharacterType || source instanceof StringType)
        && source.getAbstractTypeData()?.derivedFromConstant === true) {
      const constant = node?.concatTokens();
      if (constant !== undefined
          && ((constant.startsWith("'") && constant.endsWith("'"))
            || (constant.startsWith("`") && constant.endsWith("`")))
          && /^\d*$/.test(constant.substring(1, constant.length - 1)) === false) {
        return false;
      }
    }
 
    const functionalResult = node !== undefined && builtin === false && this.isFunctionalMethodResult(node);
    if (functionalResult && this.isFunctionalResultAssignable(source, target) === false) {
      return false;
    }
 
    // a structure, table or reference returned by a functional method follows the same rule as a variable
    const strictResult = functionalResult && (source instanceof StructureType
      || source instanceof TableType
      || source instanceof DataReference
      || source instanceof ObjectReferenceType
      || source instanceof GenericObjectReferenceType);
    if (calculated && strictResult === false) {
      return this.isAssignable(source, target);
    }
 
    // a number literal of type p fits a packed parameter of any length without decimals
    if (this.isPackedConstant(source)) {
      if (target instanceof PackedType) {
        return target.getDecimals() === 0;
      } else if (target instanceof StringType
          || target instanceof DateType
          || target instanceof CharacterType
          || target instanceof CGenericType
          || target instanceof CSequenceType
          || target instanceof XStringType
          || target instanceof XGenericType
          || (target instanceof StructureType && target.containsVoid() === false)) {
        return false;
      }
    }
 
    // an elementary text field is not compatible with a structure, also not with a flat character-like one
    if (target instanceof StructureType
        && target.containsVoid() === false
        && (source instanceof CharacterType || source instanceof StringType || source instanceof NumericType)) {
      return false;
    }
 
    if (target instanceof CLikeType) {
      return this.isCharLikeStrict(source);
    }
 
    if (target instanceof PGenericType) {
      return source instanceof PackedType
        || source instanceof PGenericType
        || source instanceof VoidType
        || source instanceof AnyType
        || source instanceof DataType
        || source instanceof UnknownType;
    }
 
    if (source instanceof CGenericType && target instanceof StringType) {
      return false;
    } else if (source instanceof CharacterType) {
      if (target instanceof CharacterType) {
        if (source.getAbstractTypeData()?.derivedFromConstant === true) {
          return source.getLength() <= target.getLength();
        }
        return source.getLength() === target.getLength();
      } else if (target instanceof IntegerType) {
        if (source.getAbstractTypeData()?.derivedFromConstant === true) {
          return true;
        }
        return false;
      } else if (target instanceof XStringType) {
        if (source.getAbstractTypeData()?.derivedFromConstant === true) {
          return this.isHexConstant(node);
        }
        return false;
      } else if (target instanceof XSequenceType) {
        if (source.getAbstractTypeData()?.derivedFromConstant === true) {
          return this.isHexConstant(node);
        }
        return true;
      } else if (target instanceof StringType) {
        if (source.getAbstractTypeData()?.derivedFromConstant === true) {
          return true;
        }
        return false;
      }
    } else if (source instanceof HexType) {
      if (target instanceof HexType) {
        if (source.getAbstractTypeData()?.derivedFromConstant === true) {
          return source.getLength() <= target.getLength();
        }
        return source.getLength() === target.getLength();
      } else if (target instanceof XStringType) {
        return false;
      } else if (target instanceof IntegerType || target instanceof Integer8Type) {
        if (source.getAbstractTypeData()?.derivedFromConstant === true) {
          return true;
        }
        return false;
      }
    } else if (source instanceof DateType || source instanceof TimeType) {
      if (target instanceof CharacterType || target instanceof StringType) {
        return false;
      }
    } else if (source instanceof StringType) {
      if (target instanceof StructureType && this.structureContainsString(target)) {
        return false;
      } else if (target instanceof CharacterType) {
        if (source.getAbstractTypeData()?.derivedFromConstant === true) {
          return true;
        }
        return false;
      } else if (target instanceof IntegerType) {
        if (source.getAbstractTypeData()?.derivedFromConstant === true) {
          return true;
        }
        return false;
      } else if (target instanceof CGenericType
          || target instanceof GenericObjectReferenceType) {
        return false;
      } else if (target instanceof XSequenceType || target instanceof XStringType) {
        if (source.getAbstractTypeData()?.derivedFromConstant === true) {
          return this.isHexConstant(node);
        }
        return false;
      } else if (target instanceof TableType
          || target instanceof DataReference
          || target instanceof ObjectReferenceType) {
        return false;
      }
      return true;
    } else if (source instanceof StructureType) {
      if (target instanceof StructureType) {
        const sourceComponents = source.getComponents();
        const targetComponents = target.getComponents();
        if (sourceComponents.length !== targetComponents.length) {
          return false;
        }
        for (let i = 0; i < sourceComponents.length; i++) {
          if (this.isAssignableStrict(sourceComponents[i].type, targetComponents[i].type) === false) {
            return false;
          }
        }
        return true;
      } else if (target instanceof SimpleType) {
        return this.isCharLikeStrict(source);
      } else if (target instanceof CLikeType) {
        return this.isCharLikeStrict(source);
      } else if (target instanceof VoidType
          || target instanceof AnyType
          || target instanceof DataType
          || target instanceof UnknownType) {
        return true;
      }
      return false;
    } else if (source instanceof Integer8Type) {
      if (target instanceof IntegerType
          || target instanceof StringType
          || target instanceof FloatType) {
        return false;
      }
    } else if (source instanceof IntegerType) {
      if (target instanceof StringType
          || target instanceof DateType
          || target instanceof CharacterType) {
        return false;
      } else if (target instanceof Integer8Type || target instanceof PackedType) {
        if (source.getAbstractTypeData()?.derivedFromConstant === true) {
          return true;
        }
        return false;
      }
    } else if (source instanceof PackedType && target instanceof PackedType) {
      return source.getLength() === target.getLength()
        && source.getDecimals() === target.getDecimals();
    } else if (source instanceof FloatType) {
      if (target instanceof IntegerType) {
        return false;
      }
    } else if (source instanceof GenericObjectReferenceType) {
      if (target instanceof ObjectReferenceType
          || target instanceof StringType
          || target instanceof CharacterType
          || target instanceof TableType
          || target instanceof CLikeType) {
        return false;
      }
    } else if (source instanceof ObjectReferenceType) {
      if (target instanceof XSequenceType
          || target instanceof IntegerType
          || target instanceof StructureType
          || target instanceof TableType
          || target instanceof XStringType) {
        return false;
      }
    } else if (source instanceof XStringType) {
      if (target instanceof CharacterType
          || target instanceof CLikeType
          || target instanceof IntegerType
          || target instanceof StringType
          || target instanceof ObjectReferenceType
          || target instanceof HexType) {
        return false;
      }
    } else if (source instanceof DataReference && target instanceof DataReference) {
      return this.isDataReferenceCompatible(source, target);
    } else if (source instanceof TableType) {
      if (target instanceof TableType && this.isTableKindCompatible(source, target) === false) {
        return false;
      } else if (source.getRowType() instanceof AnyType) {
        // then its a generic table, todo: add top level generic table type?
        return true;
      } else if (target instanceof TableType && target.getRowType() instanceof AnyType) {
        // then its a generic table, todo: add top level generic table type?
        return true;
      } else if (target instanceof TableType) {
        const sourceKeyType = source.getOptions().keyType;
        const targetKeyType = target.getOptions().keyType;
        if (sourceKeyType !== targetKeyType
            && sourceKeyType !== TableKeyType.user
            && targetKeyType !== TableKeyType.user) {
          return false;
        }
        if (sourceKeyType === TableKeyType.empty && targetKeyType !== TableKeyType.empty
            && !(target.getRowType() instanceof AnyType)) {
          return false;
        }
        return this.isAssignableStrict(source.getRowType(), target.getRowType());
      } else if (target instanceof UnknownType
          || target instanceof AnyType
          || target instanceof VoidType) {
        return true;
      }
    }
    return this.isAssignable(source, target);
  }
 
  // a standard, sorted or hashed table only fits a table type of the same kind, generic or not,
  // INDEX TABLE takes standard and sorted tables, ANY TABLE takes all
  private isTableKindCompatible(source: TableType, target: TableType): boolean {
    const sourceAccess = source.getAccessType();
    const targetAccess = target.getAccessType();
    if (sourceAccess === undefined
        || targetAccess === undefined
        || sourceAccess === TableAccessType.index
        || sourceAccess === TableAccessType.any
        || source.isWithHeader()
        || target.isWithHeader()) {
      return true;
    } else if (targetAccess === TableAccessType.any) {
      return true;
    } else if (targetAccess === TableAccessType.index) {
      return sourceAccess !== TableAccessType.hashed;
    }
    return sourceAccess === targetAccess;
  }
 
  // a data reference fits REF TO data, or a reference to the same type, no down cast
  private isDataReferenceCompatible(source: DataReference, target: DataReference): boolean {
    const targetType = target.getType();
    const sourceType = source.getType();
    if (targetType instanceof DataType || targetType instanceof AnyType || target.isGeneric()) {
      return true;
    } else if (sourceType instanceof DataType || sourceType instanceof AnyType) {
      return false;
    }
    return this.isSameType(sourceType, targetType);
  }
 
  // types of the same kind and technical attributes, structures also with the same component names;
  // returns true whenever this cannot be decided
  private isSameType(source: AbstractType, target: AbstractType): boolean {
    if (source instanceof VoidType || target instanceof VoidType
        || source instanceof UnknownType || target instanceof UnknownType
        || source instanceof AnyType || target instanceof AnyType
        || source.containsVoid() || target.containsVoid()) {
      return true;
    } else if (source.constructor !== target.constructor) {
      return false;
    }
 
    if ((source instanceof CharacterType && target instanceof CharacterType)
        || (source instanceof HexType && target instanceof HexType)
        || (source instanceof NumericType && target instanceof NumericType)) {
      return source.getLength() === target.getLength();
    } else if (source instanceof PackedType && target instanceof PackedType) {
      return source.getLength() === target.getLength() && source.getDecimals() === target.getDecimals();
    } else if (source instanceof StructureType && target instanceof StructureType) {
      const sourceComponents = source.getComponents();
      const targetComponents = target.getComponents();
      if (sourceComponents.length !== targetComponents.length) {
        return false;
      }
      for (let i = 0; i < sourceComponents.length; i++) {
        if (sourceComponents[i].name.toUpperCase() !== targetComponents[i].name.toUpperCase()
            || this.isSameType(sourceComponents[i].type, targetComponents[i].type) === false) {
          return false;
        }
      }
    } else if (source instanceof TableType && target instanceof TableType) {
      return source.getAccessType() === target.getAccessType()
        && this.isSameType(source.getRowType(), target.getRowType());
    } else if (source instanceof DataReference && target instanceof DataReference) {
      return this.isSameType(source.getType(), target.getType());
    }
    return true;
  }
 
  // character-like arguments of built-in functions must be elementary, flat character-like structures are not allowed
  public isAssignableBuiltin(source: AbstractType | undefined,
                             target: AbstractType | undefined,
                             node?: ExpressionNode): boolean {
    if (target instanceof CLikeType
        && (source instanceof StructureType
          || (source instanceof TableType && source.isWithHeader() && source.getRowType() instanceof StructureType))) {
      return false;
    }
    return this.isAssignableStrict(source, target, node, true);
  }
 
  // Typing with the classic STRUCTURE addition, ie. "FORM foo USING bar STRUCTURE zkey",
  // is not a compatibility check: ABAP only checks the fragment view up to the end of the
  // formal structure, so the actual parameter is allowed to be longer. For TABLES parameters
  // the table key is not part of the check at all.
  public isAssignableStructureTyping(source: AbstractType | undefined,
                                     target: AbstractType | undefined,
                                     node?: ExpressionNode): boolean {
    if (source instanceof StructureType && target instanceof StructureType) {
      const sourceComponents = source.getComponents();
      const targetComponents = target.getComponents();
      if (sourceComponents.length < targetComponents.length) {
        return false;
      }
      for (let i = 0; i < targetComponents.length; i++) {
        if (this.isAssignableStrict(sourceComponents[i].type, targetComponents[i].type) === false) {
          return false;
        }
      }
      return true;
    } else if (source instanceof TableType && target instanceof TableType) {
      return this.isAssignableStructureTyping(source.getRowType(), target.getRowType());
    } else if (source instanceof CharacterType && target instanceof StructureType) {
      // an elementary actual must be character-like, the formal structure purely character-like
      const length = this.characterLikeStructureLength(target);
      return length !== undefined && source.getLength() >= length;
    }
 
    return this.isAssignableStrict(source, target, node);
  }
 
  // returns the total length if the structure is flat and purely character-like, otherwise undefined
  private characterLikeStructureLength(type: StructureType): number | undefined {
    let length = 0;
    for (const component of type.getComponents()) {
      const t = component.type;
      if (t instanceof CharacterType || t instanceof NumericType) {
        length += t.getLength();
      } else if (t instanceof DateType) {
        length += 8;
      } else if (t instanceof TimeType) {
        length += 6;
      } else if (t instanceof StructureType) {
        const sub = this.characterLikeStructureLength(t);
        if (sub === undefined) {
          return undefined;
        }
        length += sub;
      } else {
        return undefined;
      }
    }
    return length;
  }
 
  public isAssignable(source: AbstractType | undefined, target: AbstractType | undefined): boolean {
    if (source === undefined || target === undefined) {
      return true;
    }
 
/*
    console.dir(source);
    console.dir(target);
*/
    if (source instanceof UTCLongType || target instanceof UTCLongType) {
      return this.isUTCLongAssignable(source, target);
    }
 
    if (target instanceof TableType) {
      if (target.isWithHeader()) {
        return this.isAssignable(source, target.getRowType());
      }
      if (source instanceof VoidType
          || source instanceof AnyType
          || source instanceof DataType
          || source instanceof UnknownType) {
        return true;
      } else if (source instanceof TableType) {
        const targetRowType = target.getRowType();
        const sourceRowType = source.getRowType();
        if (targetRowType instanceof VoidType || targetRowType instanceof AnyType || targetRowType instanceof UnknownType) {
          return true;
        } else if (sourceRowType instanceof VoidType || sourceRowType instanceof AnyType || sourceRowType instanceof UnknownType) {
          return true;
        }
        if (targetRowType instanceof StructureType
            && this.structureContainsString(targetRowType)) {
          if (!(sourceRowType instanceof StructureType)) {
            return false;
          } else if (!this.structureContainsString(sourceRowType)
              && this.structureContainsVoid(sourceRowType) === false) {
            return false;
          }
        } else if (sourceRowType instanceof StructureType
            && this.structureContainsString(sourceRowType)) {
          if (!(targetRowType instanceof StructureType)) {
            return false;
          } else if (!this.structureContainsString(targetRowType)
          && this.structureContainsVoid(targetRowType) === false) {
            return false;
          }
        }
        return true;
      }
      return false;
    } else if (target instanceof ObjectReferenceType && source instanceof ObjectReferenceType) {
      return this.isOOAssignable(source, target);
    } else if (target instanceof ObjectReferenceType
        || target instanceof GenericObjectReferenceType) {
      if (source instanceof ObjectReferenceType
          || source instanceof GenericObjectReferenceType
          || source instanceof VoidType
          || source instanceof DataType
          || source instanceof AnyType
          || source instanceof UnknownType) {
        return true;
      }
      return false;
    } else if (target instanceof DataReference) {
      if (source instanceof DataReference
          || source instanceof VoidType
          || source instanceof DataType
          || source instanceof AnyType
          || source instanceof UnknownType) {
        return true;
      }
      return false;
    } else if (target instanceof StructureType) {
      if (source instanceof TableType && source.isWithHeader()) {
        return this.isAssignable(source.getRowType(), target);
      } else if (source instanceof VoidType
          || source instanceof AnyType
          || source instanceof DataType
          || source instanceof UnknownType) {
        return true;
      } else if (source instanceof StructureType) {
        const targetDeep = this.structureContainsString(target);
        const sourceDeep = this.structureContainsString(source);
        if (targetDeep && !sourceDeep) {
          return false;
        }
 
        const targetComponents = target.getComponents();
        const sourceComponents = source.getComponents();
        if (targetComponents.length !== sourceComponents.length) {
          if (targetDeep === true || sourceDeep === true) {
            return false;
          }
        }
 
        for (let i = 0; i < targetComponents.length; i++) {
          if (sourceComponents[i] === undefined) {
            continue;
          }
          // hmm
          if (sourceComponents[i].type instanceof StringType && !(targetComponents[i].type instanceof StringType)) {
            return false;
          } else if (!(sourceComponents[i].type instanceof StringType) && targetComponents[i].type instanceof StringType) {
            return false;
          }
        }
 
        return true;
      } else if (target.containsVoid() === true) {
        return true;
      } else if (source instanceof StringType && this.structureContainsString(target)) {
        return false;
      } else if (source instanceof IntegerType) {
        return false;
      } else if (this.isCharLike(target) && this.isCharLike(source)) {
        return true;
      }
      return false;
    } else if (target instanceof IntegerType
        || target instanceof CharacterType
        || target instanceof StringType) {
      if (source instanceof TableType && source.isWithHeader() === false) {
        return false;
      } else if ((target instanceof StringType || target instanceof CharacterType)
          && source instanceof StructureType
          && this.isCharLike(source)) {
        if (this.structureContainsString(source) === true) {
          return false;
        }
        return true;
      } else if (source instanceof StructureType) {
        return this.isCharLikeStrict(source);
      } else if (source instanceof DataReference
          || source instanceof ObjectReferenceType
          || source instanceof GenericObjectReferenceType) {
        return false;
      }
      return true;
    } else if (target instanceof SimpleType) {
      if (source instanceof ObjectReferenceType
          || source instanceof TableType) {
        return false;
      }
    }
 
    return true;
  }
 
  // utclong can only be converted to and from character like types
  private isUTCLongAssignable(source: AbstractType, target: AbstractType): boolean {
    if (this.isGenericOrVoid(source) || this.isGenericOrVoid(target)) {
      return true;
    } else if (source instanceof UTCLongType && target instanceof UTCLongType) {
      return true;
    } else if (source instanceof UTCLongType) {
      return this.isTextLike(target);
    }
    return this.isTextLike(source);
  }
 
  private isGenericOrVoid(type: AbstractType): boolean {
    return type instanceof VoidType
      || type instanceof AnyType
      || type instanceof DataType
      || type instanceof UnknownType;
  }
 
  private isTextLike(type: AbstractType): boolean {
    return type instanceof CharacterType
      || type instanceof StringType
      || type instanceof CLikeType
      || type instanceof CSequenceType
      || type instanceof CGenericType
      || type instanceof SimpleType;
  }
}