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 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 | 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 90445x 90445x 1x 1x 1068x 1068x 172x 172x 896x 896x 1068x 237x 237x 659x 1068x 31x 31x 31x 31x 31x 31x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x 4x 4x 4x 31x 27x 27x 27x 27x 27x 31x 1068x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x 1x 10x 10x 10x 10x 10x 10x 618x 618x 1068x 1068x 2x 2x 616x 616x 1068x 294x 294x 322x 322x 1068x 322x 322x 322x 1x 1x 355x 355x 355x 355x 10x 10x 355x 6x 6x 355x 6x 6x 355x 6x 6x 355x 355x 355x 355x 355x 355x 355x 355x 355x 16x 355x 339x 339x 339x 339x 339x 275x 275x 275x 339x 339x 339x 339x 25x 25x 25x 339x 339x 315x 315x 339x 339x 24x 24x 8x 8x 8x 24x 24x 339x 339x 12x 12x 12x 327x 339x 79x 79x 79x 40x 4x 4x 79x 2x 2x 2x 39x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 37x 36x 3x 3x 36x 35x 35x 36x 36x 79x 323x 339x 10x 339x 313x 313x 2x 313x 311x 311x 339x 329x 355x 4x 4x 325x 325x 325x 1x 1x 16756x 16756x 16756x 16483x 16483x 273x 273x 16756x 11x 16756x 38x 262x 23x 224x 20x 201x 20x 181x 20x 161x 20x 141x 20x 121x 20x 101x 20x 81x 7x 61x 5x 54x 5x 49x 5x 44x 4x 39x 26x 26x 273x 273x 1x 1x 71148x 71148x 71148x 66x 66x 71082x 71082x 71148x 690x 690x 70392x 70392x 70392x 70392x 71148x 71148x 2x 2x 2x 71148x 54321x 54321x 16068x 16068x 71148x 14730x 14730x 14730x 7x 7x 7x 7x 7x 7x 14730x 14730x 14730x 14730x 1338x 71148x 71148x 2x 2x 2x 1336x 1336x 1336x 1336x 1336x 1336x 1336x 699x 699x 1336x 1336x 1336x 1x 1x 27517x 27517x 21579x 21579x 27517x 27517x 27517x 3x 3x 27517x 27517x 27517x 5938x 8x 5938x 5930x 5930x 5930x 3821x 3821x 5930x 27517x 27517x 27517x 27517x 27512x 27512x 1x 1x 4359x 4359x 4359x 4359x 4359x 4359x 4359x 4359x 4359x 4359x 3704x 4359x 655x 406x 655x 249x 162x 162x 4359x 4359x 4359x 4359x 4359x 4359x 4359x 4359x 4359x 4359x 4359x 4359x 4359x 4359x 4357x 641x 641x 4359x 2x 2x 4359x 4359x 4359x 191x 191x 191x 191x 191x 191x 191x 191x 191x 191x 191x 191x 221x 221x 191x 191x 191x 4359x 4359x 4359x 433x 4359x 3131x 3131x 4359x 4359x 4359x 4359x 4359x 4359x 4359x 4359x 4359x 4359x 4359x 4359x 4359x 88x 88x 88x 88x 4359x 4271x 4271x 4271x 4140x 4140x 4140x 4140x 4271x 131x 131x 131x 5x 5x 5x 5x 131x 126x 126x 126x 126x 126x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 126x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 78x 39x 39x 35x 35x 35x 35x 1x 1x 1x 49x 49x 7x 6x 6x 6x 6x 7x 49x 38x 42x 1x 1x 3x 3x 1x 1x 84472x 84472x 84472x 84472x 56455x 56455x 84472x 9732x 9732x 1x 1x 9732x 84472x 9661x 9661x 84472x 9609x 9609x 84472x 84472x 84472x 3x 3x 84472x 870x 870x 84472x 84472x 84472x 36x 36x 36x 84436x 84436x 84472x 52x 84472x 71x 71x 71x 71x 4x 4x 71x 2x 2x 71x 1x 1x 1x 1x 71x 71x 71x 71x 71x 71x 59x 71x 9x 12x 3x 3x 68x 71x 1x 1x 1x 1x 67x 84384x 51x 51x 51x 51x 51x 3x 3x 48x 84313x 84262x 84262x 84262x 84262x 436x 436x 436x 436x 436x 436x 436x 84262x 207x 207x 57x 57x 207x 51x 51x 207x 11x 11x 207x 8x 8x 207x 207x 207x 7x 207x 1x 1x 83826x 2x 2x 2x 2x 2x 2x 83619x 17571x 83617x 66046x 66046x 66046x 66046x 20x 66046x 5x 2x 5x 1x 3x 2x 2x 5x 66043x 66046x 66x 66x 66x 66x 13x 13x 13x 13x 66x 6x 6x 66x 66x 66x 1x 1x 66x 66046x 83821x 84472x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 83821x 83821x 83821x 1x 1x 1x 1x 66319x 66319x 29x 29x 66290x 66290x 1x 1x 49x 49x 49x 49x 10x 10x 39x 39x 39x 39x 39x 1x 1x 1x 49x 49x 1x 1x 42x 42x 1x 1x 65x 65x 1x 1x 84436x 84436x 84436x 84436x 1x 1x 1x 71082x 71082x 71082x 138x 138x 138x 3x 3x 3x 71082x 70392x 70392x 687x 687x 687x 71082x 557x 557x 557x 71082x 12x 12x 12x 12x 687x 687x 687x 71082x 557x 557x 557x 557x 557x 557x 557x 8x 8x 8x 8x 8x 8x 8x 557x 3x 3x 549x 546x 546x 546x 134x 134x 134x 134x 546x 16x 16x 546x 546x 546x 546x 546x 546x 546x 546x 546x 1x 1x 395x 546x 546x 393x 393x 546x 71082x 12x 12x 12x 12x 12x 12x 12x 2x 2x 2x 2x 2x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 130x 118x 118x 118x 69x 69x 1x 1x 1x 69x 69x 68x 68x 68x 68x 68x 68x 68x 68x 68x 69x 118x 49x 49x 118x 118x 53x 53x 118x 118x 480x 480x 71082x 73x 73x 5x 73x 68x 73x 1x 1x 67x 67x 474x 474x 474x 1x 1x 37x 37x 37x 37x 37x 37x 37x 19x 19x 3x 3x 3x 3x 16x 19x 2x 2x 2x 2x 14x 37x 18x 18x 18x 1x 1x 1x 1x 1x 1x 1x 18x 17x 17x 9x 2x 9x 3x 7x 4x 4x 4x 4x 4x 9x 8x 17x 17x 17x 17x 8x 8x 8x 8x 37x 1x 1x 17x 17x 17x 1x 1x 17x 10x 10x 6x 6x 17x 17x 1x 1x 23x 20x 23x 1x 1x 4x 1x 1x 1x 4x 1x 1x 2x 2x 1x 1x 17659x 17659x 17659x 17659x 17575x 798x 798x 16777x 17575x 15984x 15984x 15984x 15984x 15984x 17575x 877x 877x 17659x 74x 17659x 768x 803x 35x 1x 1x 1x 34x 17659x 1x 1x 33x 17659x 33x 33x 33x 1x 1x 2992x 2992x 4x 4x 2988x 2992x 3x 3x 2985x 2985x 2992x 2949x 2949x 2x 2x 2x 6x 2x 6x 4x 4x 6x 2x 2949x 2947x 2947x 2949x 36x 36x 36x 36x 36x 36x 2x 2x 2x 36x 2x 2x 2x 32x 32x 1x 1x 1x 1x 1x 84472x 84472x 84472x 84472x 84472x 80751x 84472x 11x 5x 5x 6x 11x 11x 84472x 172x 172x 16x 16x 172x 84472x 84472x 8x 84472x 6x 6x 3680x 3680x 1x 1x 1x 1x 1x 172x 172x 172x 172x 172x 149x 149x 23x 23x 23x 172x 10x 10x 5x 5x 5x 10x 172x 7x 7x 11x 11x 1x 1x 66045x 66045x 522x 522x 65523x 66045x 47x 47x 47x 8x 8x 47x 65515x 65515x 65515x 1x 1x 1x 45x 45x 19x 19x 26x 45x 45x 1x 1x 1x 92x 92x 526x 526x 526x 526x 526x 492x 492x 526x 526x 58x 58x 1x 1x 66056x 66056x 66056x 66056x 1x 1x 66055x 66056x 173x 173x 172x 172x 1x 1x 1x 1x 1x 1x 173x 65882x 66056x 62361x 39x 39x 62322x 62322x 3521x 3521x 66056x 3515x 3515x 6x 6x 66056x 2x 2x 4x 4x 4x 4x 4x 4x 1x 1x 3720x 1x 1x 3719x 3720x 2x 3720x 1x 1x 3719x 3719x 3719x 1x 1x | import {TypedIdentifier, IdentifierMeta} from "../types/_typed_identifier";
import {StatementNode, ExpressionNode} from "../nodes";
import * as Expressions from "../2_statements/expressions";
import * as Statements from "../2_statements/statements";
import * as Types from "../types/basic";
import {AbstractType} from "../types/basic/_abstract_type";
import {ScopeType} from "./_scope_type";
import {ObjectOriented} from "./_object_oriented";
import {ClassConstant} from "../types/class_constant";
import {ClassAttribute} from "../types/class_attribute";
import {Identifier as TokenIdentifier} from "../1_lexer/tokens/identifier";
import {ReferenceType} from "./_reference";
import {CharacterType, ITableKey, ObjectReferenceType, StructureType, TableAccessType, TableType, VoidType} from "../types/basic";
import {FieldChain} from "./expressions/field_chain";
import {ClassDefinition, InterfaceDefinition} from "../types";
import {Field, FieldSub, TypeTableKey} from "../2_statements/expressions";
import {BuiltIn} from "./_builtin";
import {Position} from "../../position";
import {CheckSyntaxKey, SyntaxInput, syntaxIssue} from "./_syntax_input";
import {IObjectAndToken} from "../../_iddic_references";
import {Release, releaseAtLeast} from "../../version";
export class BasicTypes {
private readonly input: SyntaxInput;
public constructor(input: SyntaxInput) {
this.input = input;
}
public lookupQualifiedName(name: string | undefined): TypedIdentifier | undefined {
// argh, todo, rewrite this entire method, more argh, again argh
if (name === undefined) {
return undefined;
}
const found = this.input.scope.findType(name);
if (found) {
return found;
}
if (name.includes("=>")) {
const split = name.split("=>");
const ooName = split[0];
const typeName = split[1];
const oo = this.input.scope.findObjectDefinition(ooName);
if (oo) {
if (typeName.includes("-")) {
const split = typeName.split("-");
const subTypeName = split[0];
const fieldName = split[1];
const stru = oo.getTypeDefinitions().getByName(subTypeName);
const struType = stru?.getType();
if (stru && struType instanceof StructureType) {
let f = struType.getComponentByName(fieldName);
if (split[2] && f instanceof StructureType) {
f = f.getComponentByName(split[2]);
}
if (f) {
return new TypedIdentifier(stru.getToken(), stru.getFilename(), f);
}
}
} else {
const f = oo.getTypeDefinitions().getByName(typeName);
if (f) {
return f;
}
}
}
} else if (name.includes("-")) {
const split = name.split("-");
const typeName = split[0];
const fieldName = split[1];
const type = this.input.scope.findType(typeName);
if (type) {
const stru = type.getType();
if (stru instanceof StructureType) {
let f = stru.getComponentByName(fieldName);
if (split[2] && f instanceof StructureType) {
f = f.getComponentByName(split[2]);
}
if (f) {
return new TypedIdentifier(type.getToken(), type.getFilename(), f);
}
}
}
}
const lookup = this.input.scope.getDDIC().lookupNoVoid(name);
const id = lookup?.object?.getIdentifier();
if (id && lookup?.type) {
return new TypedIdentifier(id.getToken(), id.getFilename(), lookup.type);
}
const builtin = this.input.scope.getDDIC().lookupBuiltinType(name);
if (builtin) {
return new TypedIdentifier(new TokenIdentifier(new Position(1, 1), name), BuiltIn.filename, builtin);
}
const type = this.input.scope.findTypePoolType(name);
if (type) {
return type;
}
return undefined;
}
public resolveLikeName(node: ExpressionNode | StatementNode | undefined, headerLogic = true): AbstractType | undefined {
if (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);
}
if (chain === undefined) {
chain = node.findFirstExpression(Expressions.SimpleFieldChain);
}
if (chain === undefined) {
chain = node.findFirstExpression(Expressions.SimpleFieldChain2);
}
if (chain === undefined) {
throw new Error("resolveLikeName, chain undefined");
}
const fullName = chain.concatTokens();
let children = [...chain.getChildren()];
if (children.length === 0) {
return new Types.UnknownType("Type error, could not resolve \"" + fullName + "\", resolveLikeName1");
}
let type: AbstractType | undefined;
if (children[1] && (children[1].getFirstToken().getStr() === "=>" || children[1].getFirstToken().getStr() === "->")) {
type = FieldChain.runSyntax(chain, this.input, ReferenceType.TypeReference);
} else {
const name = children.shift()!.getFirstToken().getStr();
let found = this.input.scope.findVariable(name);
const full = this.input.scope.findVariable(fullName); // workaround for eg "sy-repid"
if (full) {
children = [];
found = full;
}
type = found?.getType();
if (found === undefined) {
found = this.input.scope.findExtraLikeType(name);
type = found?.getType();
}
if (found) {
this.input.scope.addReference(chain?.getFirstToken(), found, ReferenceType.TypeReference, this.input.filename);
}
if (type === undefined) {
const found = this.input.scope.getDDIC().lookupNoVoid(name);
if (found !== undefined) {
const using: IObjectAndToken = {filename: this.input.filename, token: chain.getFirstToken(), object: found.object};
this.input.scope.getDDICReferences().addUsing(this.input.scope.getParentObj(), using);
}
type = found?.type;
}
if (type === undefined && this.input.scope.isAnyOO() === false && this.input.scope.getDDIC().inErrorNamespace(name) === false) {
this.input.scope.addReference(chain.getChildren()[0].getFirstToken(), undefined, ReferenceType.VoidType, this.input.filename);
return Types.VoidType.get(name);
}
while (children.length > 0) {
const child = children.shift()!;
if (child.getFirstToken().getStr() === "-") {
if (type instanceof Types.VoidType) {
return type;
}
} else if (child.concatTokens() === "[]") {
if (type instanceof Types.TableType) {
type = new TableType(type.getRowType(), {withHeader: false, keyType: Types.TableKeyType.default});
}
} else if (child instanceof ExpressionNode && child.get() instanceof Expressions.FieldLength) {
const len = parseInt(child.concatTokens().replace("(", "").replace(")", ""), 10);
if (isNaN(len)) {
type = new Types.UnknownType("Type error, invalid field length");
} else {
if (type instanceof Types.DateType) {
if (len > 8) {
type = new Types.UnknownType("Type error, offset too long");
} else {
type = new Types.CharacterType(len);
}
} else if (type instanceof Types.CharacterType) {
if (len <= type.getLength()) {
type = new Types.CharacterType(len);
} else {
type = new Types.UnknownType("Type error, offset too long");
}
}
}
} else { // field name
if (type instanceof Types.TableType) {
type = type.getRowType();
}
if (type instanceof Types.StructureType) {
type = type.getComponentByName(child.getFirstToken().getStr());
if (type === undefined) {
return new Types.UnknownType("Type error, field not part of structure " + fullName);
}
} else if (!(type instanceof Types.VoidType)) {
return new Types.UnknownType("Type error, field not part of structure " + fullName);
}
}
}
if (type instanceof Types.VoidType) {
return type;
} else if (type instanceof TableType
&& type.isWithHeader()
&& headerLogic === true) {
type = type.getRowType();
} else if (type instanceof Types.TableType
&& type.isWithHeader() === true
&& type.getRowType() instanceof Types.VoidType) {
return type.getRowType();
}
}
if (!type) {
return new Types.UnknownType("Type error, could not resolve \"" + fullName + "\", resolveLikeName2");
}
return type;
}
private cloneType(type: AbstractType, qualifiedName?: string): AbstractType {
// nested types(containing "-") will inherit the qualified names if possible
// todo, this needs to be extended to all AbstractTypes
if (!qualifiedName || qualifiedName.includes("-") === true) {
return type;
}
// TYPES creates a new type, it is no longer the DDIC type
const data = {...type.getAbstractTypeData(), qualifiedName, ddicName: undefined};
if (type instanceof CharacterType) {
type = new CharacterType(type.getLength(), data);
} else if (type instanceof Types.IntegerType) {
type = Types.IntegerType.get(data);
} else if (type instanceof Types.StringType) {
type = Types.StringType.get(data);
} else if (type instanceof Types.XStringType) {
type = Types.XStringType.get(data);
} else if (type instanceof Types.Integer8Type) {
type = new Types.Integer8Type(data);
} else if (type instanceof Types.NumericType) {
type = new Types.NumericType(type.getLength(), data);
} else if (type instanceof Types.HexType) {
type = new Types.HexType(type.getLength(), data);
} else if (type instanceof Types.FloatingPointType) {
type = new Types.FloatingPointType(type.getLength(), data);
} else if (type instanceof Types.DateType) {
type = new Types.DateType(data);
} else if (type instanceof Types.TimeType) {
type = new Types.TimeType(data);
} else if (type instanceof Types.PackedType) {
type = new Types.PackedType(type.getLength(), type.getDecimals(), data);
} else if (type instanceof Types.DecFloat16Type) {
type = new Types.DecFloat16Type(data);
} else if (type instanceof Types.DecFloat34Type) {
type = new Types.DecFloat34Type(data);
} else if (type instanceof Types.UTCLongType) {
type = new Types.UTCLongType(data);
} else if (type instanceof StructureType) {
type = new StructureType(type.getComponents().slice(), qualifiedName, undefined, type.getDescription());
} else if (type instanceof TableType) {
type = new TableType(type.getRowType(), type.getOptions(), qualifiedName, type.getDescription());
}
return type;
}
public resolveTypeName(typeName: ExpressionNode | undefined,
length?: number, decimals?: number, qualifiedName?: string): AbstractType | undefined {
if (typeName === undefined) {
return undefined;
}
const chain = this.resolveTypeChain(typeName);
if (chain) {
return this.cloneType(chain, qualifiedName);
}
const chainText = typeName.concatTokens().toUpperCase();
const f = this.input.scope.getDDIC().lookupBuiltinType(chainText, length, decimals, qualifiedName);
if (f instanceof Types.Integer8Type
&& !releaseAtLeast(this.input.scope.getRelease(), Release.v750)
&& !this.input.scope.getOpenABAP()) {
const message = "Type INT8 requires 7.50";
this.input.issues.push(syntaxIssue(this.input, typeName.getFirstToken(), message));
return VoidType.get(CheckSyntaxKey);
} else if (f !== undefined) {
return f;
}
const typ = this.input.scope.findType(chainText);
if (typ) {
const token = typeName.getFirstToken();
if (chainText.includes("~")) {
const name = chainText.split("~")[0];
const idef = this.input.scope.findInterfaceDefinition(name);
if (idef) {
this.input.scope.addReference(token, idef, ReferenceType.ObjectOrientedReference, this.input.filename, {ooType: "INTF", ooName: name});
}
}
this.input.scope.addReference(token, typ, ReferenceType.TypeReference, this.input.filename);
return this.cloneType(typ.getType(), qualifiedName);
}
const type = this.input.scope.findTypePoolType(chainText)?.getType();
if (type) {
// this.scope.addReference(typeName.getFirstToken(), type, ReferenceType.TypeReference, this.filename);
return type;
}
const ddic = this.input.scope.getDDIC().lookup(chainText);
if (ddic) {
this.input.scope.getDDICReferences().addUsing(this.input.scope.getParentObj(),
{object: ddic.object, token: typeName.getFirstToken(), filename: this.input.filename});
if (ddic.type instanceof TypedIdentifier) {
this.input.scope.addReference(typeName.getFirstToken(), ddic.type, ReferenceType.TypeReference, this.input.filename);
} else if (ddic.type instanceof VoidType) {
this.input.scope.addReference(typeName.getFirstToken(), undefined, ReferenceType.VoidType, this.input.filename);
}
return this.cloneType(ddic.type, qualifiedName);
}
return undefined;
}
public simpleType(node: StatementNode | ExpressionNode, qualifiedNamePrefix?: string): TypedIdentifier | undefined {
let nameExpr = node.findFirstExpression(Expressions.NamespaceSimpleName);
if (nameExpr === undefined) {
nameExpr = node.findFirstExpression(Expressions.DefinitionName);
}
if (nameExpr === undefined) {
return undefined;
}
let name = nameExpr.getFirstToken();
if (nameExpr.countTokens() > 1) { // workaround for names with dashes
name = new TokenIdentifier(name.getStart(), nameExpr.concatTokens());
}
let qualifiedName: string | undefined = undefined;
if (node.get() instanceof Statements.Type) {
if (this.input.scope.isTypePool() === true) {
qualifiedName = name.getStr();
} else {
qualifiedName = (qualifiedNamePrefix || "") + name.getStr();
if (this.input.scope.getType() === ScopeType.ClassDefinition
|| this.input.scope.getType() === ScopeType.Interface) {
qualifiedName = this.input.scope.getName() + "=>" + qualifiedName;
}
}
} else if (qualifiedNamePrefix) {
qualifiedName = qualifiedNamePrefix + qualifiedName;
}
const found = this.parseType(node, qualifiedName);
if (found) {
return new TypedIdentifier(name, this.input.filename, found);
}
return undefined;
}
public parseTable(node: ExpressionNode | StatementNode, name?: string): AbstractType | undefined {
const typename = node.findFirstExpression(Expressions.TypeName) || node.findFirstExpression(Expressions.EntityAssociation);
const text = node.findFirstExpression(Expressions.TypeTable)?.concatTokens().toUpperCase();
if (text === undefined) {
return undefined;
}
let type: Types.TableAccessType | undefined = undefined;
if (text.startsWith("TYPE STANDARD TABLE ")
|| text.startsWith("TYPE TABLE ")
|| text.startsWith("LIKE TABLE ")
|| text.startsWith("LIKE STANDARD TABLE ")) {
type = TableAccessType.standard;
} else if (text.startsWith("TYPE SORTED TABLE ")
|| text.startsWith("LIKE SORTED TABLE ")) {
type = TableAccessType.sorted;
} else if (text.startsWith("TYPE HASHED TABLE ")
|| text.startsWith("LIKE HASHED TABLE ")) {
type = TableAccessType.hashed;
}
const typeTableKeys = node.findAllExpressions(TypeTableKey);
const firstKey = typeTableKeys[0];
const isNamed = firstKey?.findDirectExpression(Field) !== undefined
&& firstKey?.findDirectExpression(Field)?.concatTokens().toUpperCase() !== "PRIMARY_KEY";
const primaryKey: ITableKey = {
name: "primary_key",
type: type || TableAccessType.standard,
isUnique: isNamed ? false : firstKey?.concatTokens().toUpperCase().includes("WITH UNIQUE ") === true,
keyFields: [],
};
let start = 1;
if (isNamed === false) {
for (const k of firstKey?.findDirectExpressions(FieldSub) || []) {
primaryKey.keyFields.push(k.concatTokens().toUpperCase());
}
} else {
start = 0;
}
const secondaryKeys: ITableKey[] = [];
for (let i = start; i < typeTableKeys.length; i++) {
const row = typeTableKeys[i];
const name = row.findDirectExpression(Field)?.concatTokens();
if (name === undefined) {
continue;
}
const secondary: ITableKey = {
name: name,
type: row.findDirectTokenByText("SORTED") ? TableAccessType.sorted : TableAccessType.hashed,
isUnique: row?.concatTokens().toUpperCase().includes("WITH UNIQUE ") === true,
keyFields: [],
};
for (const k of row?.findDirectExpressions(FieldSub) || []) {
secondary.keyFields.push(k.concatTokens().toUpperCase());
}
secondaryKeys.push(secondary);
}
let keyType = Types.TableKeyType.user;
if (text.includes(" EMPTY KEY")) {
keyType = Types.TableKeyType.empty;
} else if (text.includes(" DEFAULT KEY")) {
keyType = Types.TableKeyType.default;
}
const options: Types.ITableOptions = {
withHeader: text.includes(" WITH HEADER LINE"),
keyType: keyType,
primaryKey: primaryKey,
secondary: secondaryKeys,
};
let found: AbstractType | 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);
if (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);
if (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);
if (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(Types.AnyType.get(), options);
} else if (text.startsWith("TYPE RANGE OF ")) {
const sub = node.findFirstExpression(Expressions.TypeName);
found = this.resolveTypeName(sub);
if (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},
]);
options.primaryKey!.type = TableAccessType.standard;
return new Types.TableType(structure, options, name);
} else if (text.startsWith("LIKE RANGE OF ")) {
const sub = node.findFirstExpression(Expressions.FieldChain);
found = this.resolveLikeName(sub);
if (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);
options.primaryKey!.type = TableAccessType.standard;
return new Types.TableType(structure, options);
} else if (typename && this.isRAPTableFor(text)) {
return this.rapTableFor(typename.concatTokens(), options);
}
// fallback to old style syntax, OCCURS etc
return this.parseType(node, name);
}
/** type of a RAP derived table type, ie the "TABLE FOR LOCK zentity" part of a type */
public rapTableFor(entityName: string, options?: Types.ITableOptions): AbstractType {
const ddlsName = this.resolveRAPAlias(this.getRAPBaseEntityName(entityName));
if (this.input.scope.getDDIC().lookupDDLS(ddlsName)?.type) {
return new Types.TableType(VoidType.get("RAP-TODO"), options ?? {
withHeader: false,
keyType: Types.TableKeyType.user,
primaryKey: {name: "primary_key", type: TableAccessType.standard, isUnique: false, keyFields: []},
secondary: [],
});
} else if (this.isRAPDerivedEntityName(entityName)) {
return Types.VoidType.get(entityName);
} else if (this.input.scope.getDDIC().inErrorNamespace(ddlsName)) {
return new Types.UnknownType(`DDLS ${ddlsName} not found`);
}
return Types.VoidType.get(entityName);
}
public parseType(node: ExpressionNode | StatementNode, qualifiedName?: 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.TypeStructure)?.concatTokens().toUpperCase();
}
if (text === undefined) {
text = node.findFirstExpression(Expressions.FormParamType)?.concatTokens().toUpperCase();
}
if (text === undefined
&& node.get() instanceof Statements.Parameter
&& node.findDirectTokenByText("LIKE")) {
text = "LIKE " + typeName?.concatTokens();
}
if (text === undefined) {
text = "TYPE";
}
const additionIssue = this.checkLengthDecimals(node, text);
if (additionIssue !== undefined) {
this.input.issues.push(additionIssue);
return VoidType.get(CheckSyntaxKey);
}
let found: AbstractType | undefined = undefined;
if (this.isRAPTypeStructure(text)) {
return Types.VoidType.get(node.findFirstExpression(Expressions.TypeStructure)?.concatTokens());
} else if (text.startsWith("LIKE LINE OF ")) {
let name = node.findFirstExpression(Expressions.FieldChain)?.concatTokens();
let e = node.findFirstExpression(Expressions.Type);
if (e === undefined) {
e = node.findFirstExpression(Expressions.FormParamType);
}
if (e === undefined) {
e = node.findFirstExpression(Expressions.FieldChain);
}
if (e === undefined) {
// old style, eg "LIKE LINE OF tab OCCURS 0 WITH HEADER LINE", it is parsed as TypeName
e = typeName;
name = typeName?.concatTokens();
}
const type = this.resolveLikeName(e, false);
let row: AbstractType | undefined;
if (type === undefined) {
return new Types.UnknownType("Type error, could not resolve \"" + name + "\", parseType");
} else if (type instanceof Types.TableType) {
row = type.getRowType();
} else if (type instanceof Types.VoidType) {
row = type;
} else {
return new Types.UnknownType("Type error, not a table type " + name);
}
if (this.isOccurs(node)) {
return new Types.TableType(row, {
withHeader: text.includes(" WITH HEADER LINE"),
keyType: Types.TableKeyType.default}, qualifiedName);
}
return row;
} else if (text.startsWith("LIKE REF TO ")) {
const name = node.findFirstExpression(Expressions.FieldChain)?.concatTokens();
const type = this.resolveLikeName(node.findFirstExpression(Expressions.Type), false);
if (type === undefined) {
return new Types.UnknownType("Type error, could not resolve \"" + name + "\", parseType");
}
// generic tables are reported via DataReference.isGeneric()
if (type.isGeneric() === true && !(type instanceof TableType)) {
throw new Error("LIKE REF TO not possible, \"" + name + "\" is generic");
}
return new Types.DataReference(type, name);
} else if (text === "TYPE STANDARD TABLE"
|| text === "TYPE SORTED TABLE"
|| text === "TYPE HASHED TABLE"
|| text === "TYPE INDEX TABLE"
|| text === "TYPE ANY TABLE") {
// the table kind is kept, the key is left open
const access = text.split(" ")[1] as TableAccessType;
return new Types.TableType(Types.AnyType.get(), {
withHeader: node.concatTokens().toUpperCase().includes("WITH HEADER LINE"),
keyType: Types.TableKeyType.user,
primaryKey: {name: "primary_key", type: access, isUnique: false, keyFields: []},
});
} 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.TypeName);
}
if (sub === undefined) {
sub = node.findFirstExpression(Expressions.FieldChain);
}
found = this.resolveLikeName(sub);
if (found && this.isOccurs(node)) {
found = new Types.TableType(found, {withHeader: text.includes("WITH HEADER LINE"), keyType: Types.TableKeyType.default}, qualifiedName);
} else if (text.endsWith(" WITH HEADER LINE") && found instanceof Types.TableType) {
found = new Types.TableType(found.getRowType(), {withHeader: true, keyType: Types.TableKeyType.default}, qualifiedName);
}
} else if (text.startsWith("TYPE LINE OF ")) {
const sub = node.findFirstExpression(Expressions.TypeName);
found = this.resolveTypeName(sub);
if (found instanceof TypedIdentifier) {
found = found.getType();
}
if (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 if (text.startsWith("TYPE")) {
found = this.resolveTypeName(typeName, this.findLength(node), this.findDecimals(node), qualifiedName);
const concat = node.concatTokens().toUpperCase();
if (found && this.isOccurs(node)) {
found = new Types.TableType(found, {withHeader: concat.includes(" WITH HEADER LINE"), keyType: Types.TableKeyType.default}, qualifiedName);
} else if (found && concat.includes(" WITH HEADER LINE")) {
if (found instanceof Types.VoidType) {
found = new Types.TableType(found, {withHeader: true, keyType: Types.TableKeyType.default});
} 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, keyType: Types.TableKeyType.default});
}
}
if (found === undefined && typeName === undefined) {
let length = 1;
const len = node.findDirectExpression(Expressions.ConstantFieldLength);
if (len) {
const int = len.findDirectExpression(Expressions.Integer);
if (int) {
length = parseInt(int.concatTokens(), 10);
}
} else if (node.get() instanceof Statements.Parameter) {
length = this.findParameterLength(node) ?? length;
}
found = new Types.CharacterType(length, {qualifiedName: qualifiedName}); // fallback
if (this.isOccurs(node)) {
found = new Types.TableType(found, {withHeader: concat.includes(" WITH HEADER LINE"), keyType: Types.TableKeyType.default}, qualifiedName);
}
}
}
if (text.includes(" WITH INDICATORS ")) {
const componentName = node.findFirstExpression(Expressions.Type)
?.findDirectExpression(Expressions.ComponentName)?.concatTokens().toUpperCase();
if (componentName === undefined) {
throw new Error("parseType, componentName expected");
}
if (found instanceof Types.StructureType) {
const newComponents = [...found.getComponents()];
newComponents.push({name: componentName, type: Types.VoidType.get("INDICATORStodo")});
found = new Types.StructureType(newComponents, qualifiedName);
}
}
return found;
}
/////////////////////
private isOccurs(node: ExpressionNode | StatementNode): boolean {
if (node.findDirectTokenByText("OCCURS")) {
return true;
} else if (node.findFirstExpression(Expressions.TypeTable)?.findDirectTokenByText("OCCURS")) {
return true;
}
return false;
}
private getRAPBaseEntityName(name: string): string {
const association = name.indexOf("\\_");
const path = name.indexOf("\\\\");
const action = name.indexOf("~");
if (association === -1 && path === -1 && action === -1) {
return name;
}
const candidates = [association, path, action].filter(i => i !== -1);
const splitAt = Math.min(...candidates);
return name.substring(0, splitAt);
}
/** inside a behavior pool the entities are referenced via the aliases defined in the BDEF */
private resolveRAPAlias(name: string): string {
return this.input.scope.findBehaviorDefinition()?.findEntityNameByAlias(name) ?? name;
}
private isRAPDerivedEntityName(name: string): boolean {
return name.includes("\\") || name.includes("~");
}
private isRAPTableFor(text: string): boolean {
return text.startsWith("TYPE TABLE FOR ");
}
private isRAPTypeStructure(text: string): boolean {
return text.startsWith("TYPE STRUCTURE FOR ")
|| text.startsWith("TYPE RESPONSE FOR ")
|| text.startsWith("TYPE REQUEST FOR ");
}
// todo, rewrite this method
private resolveTypeChain(expr: ExpressionNode): AbstractType | undefined {
const chainText = expr.concatTokens().toUpperCase();
if (chainText.includes("-")) {
// workaround for stuff like "sy-repid"
const built = this.input.scope.findType(chainText);
if (built) {
this.input.scope.addReference(expr.getFirstToken(), built, ReferenceType.TypeReference, this.input.filename);
return built.getType();
}
} else 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];
} else if (chainText.includes("->")) {
const split = chainText.split("->");
className = split[0];
rest = split[1];
}
const subs = rest.split("-");
let foundType: AbstractType | undefined;
if (className && chainText.includes("=>")) {
const split = chainText.split("=>");
const className = split[0];
// the prefix might be itself
if ((this.input.scope.getType() === ScopeType.Interface
|| this.input.scope.getType() === ScopeType.ClassDefinition)
&& this.input.scope.getName().toUpperCase() === className.toUpperCase()) {
const foundId = this.input.scope.findType(subs[0]);
foundType = foundId?.getType();
if (foundType === undefined) {
return new Types.UnknownType("Could not resolve type " + chainText);
}
const token = expr.getChildren()[2]?.getFirstToken();
if (token) {
this.input.scope.addReference(token, foundId, ReferenceType.TypeReference, this.input.filename);
}
} else if (className === "ME") {
// me is the reference to the instance, not a class or interface; "me->ty" names the type through it
return new Types.UnknownType("\"me\" is not a class or interface, \"" + chainText + "\" cannot be resolved");
} else {
// lookup in local and global scope
const obj = this.input.scope.findObjectDefinition(className);
if (obj === undefined && this.input.scope.getDDIC().inErrorNamespace(className) === false) {
this.input.scope.addReference(
expr.getFirstToken(), undefined,
ReferenceType.ObjectOrientedVoidReference, this.input.filename, {ooName: className.toUpperCase()});
return Types.VoidType.get(className);
} else if (obj === undefined) {
return new Types.UnknownType("Could not resolve top " + className + ", resolveTypeChain");
}
const type = obj instanceof ClassDefinition ? "CLAS" : "INTF";
this.input.scope.addReference(
expr.getFirstToken(), obj, ReferenceType.ObjectOrientedReference, this.input.filename,
{ooType: type, ooName: className});
const byName = new ObjectOriented(this.input.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");
}
const token = expr.getChildren()[2]?.getFirstToken();
if (token) {
this.input.scope.addReference(token, byName, ReferenceType.TypeReference, this.input.filename);
}
}
} else if (className && chainText.includes("->")) {
const varVar = this.input.scope.findVariable(className);
const foo = varVar?.getType();
if (foo instanceof ObjectReferenceType) {
const typeName = subs[0];
let id = foo.getIdentifier();
if (!(id instanceof ClassDefinition || id instanceof InterfaceDefinition)) {
const found = this.input.scope.findObjectDefinition(foo.getIdentifierName());
if (found) {
id = found;
} else {
return new Types.UnknownType(foo.getIdentifierName() + " not found in scope");
}
}
if (id instanceof ClassDefinition || id instanceof InterfaceDefinition) {
const type = id instanceof ClassDefinition ? "CLAS" : "INTF";
this.input.scope.addReference(
expr.getFirstToken(), id, ReferenceType.ObjectOrientedReference, this.input.filename,
{ooType: type, ooName: id.getName()});
const byName = new ObjectOriented(this.input.scope).searchTypeName(id, typeName);
foundType = byName?.getType();
if (byName === undefined || foundType === undefined) {
return new Types.UnknownType(typeName + " not found in class or interface");
}
const token = expr.getChildren()[2]?.getFirstToken();
if (token) {
this.input.scope.addReference(token, byName, ReferenceType.TypeReference, this.input.filename);
}
} else {
return new Types.UnknownType("Not an object reference, " + className + ", " + id.constructor.name);
}
} else if (foo === undefined) {
return new Types.UnknownType(className + " not found in scope");
} else {
return new Types.UnknownType("Not an object reference, " + className + ", " + foo.constructor.name);
}
} else {
const found = this.input.scope.findType(subs[0]);
foundType = found?.getType();
if (foundType === undefined) {
const typePoolType = this.input.scope.findTypePoolType(subs[0])?.getType();
if (typePoolType) {
// this.scope.addReference(typeName.getFirstToken(), typePoolType, ReferenceType.TypeReference, this.filename);
foundType = typePoolType;
}
if (foundType === undefined) {
const f = this.input.scope.getDDIC().lookupTableOrView(subs[0]);
this.input.scope.getDDICReferences().addUsing(
this.input.scope.getParentObj(),
{object: f.object, filename: this.input.filename, token: expr.getFirstToken()});
if (f.type instanceof TypedIdentifier) {
foundType = f.type.getType();
} else {
foundType = f.type;
}
}
} else {
this.input.scope.addReference(expr.getFirstToken(), found, ReferenceType.TypeReference, this.input.filename);
}
if (foundType === undefined && this.input.scope.getDDIC().inErrorNamespace(subs[0]) === false) {
this.input.scope.addReference(expr.getFirstToken(), undefined, ReferenceType.VoidType, this.input.filename);
return Types.VoidType.get(subs[0]);
} else if (foundType instanceof Types.VoidType) {
this.input.scope.addReference(expr.getFirstToken(), undefined, ReferenceType.VoidType, this.input.filename);
return foundType;
} else if (foundType === undefined) {
return new Types.UnknownType("Unknown type " + subs[0]);
}
}
subs.shift();
while (subs.length > 0) {
if (foundType instanceof Types.UnknownType
|| foundType instanceof Types.VoidType) {
return foundType;
} else if (!(foundType instanceof Types.StructureType)) {
return new Types.UnknownType("Not a structured type");
}
foundType = foundType.getComponentByName(subs[0]);
if (foundType === undefined) {
return new Types.UnknownType(`Field "${subs[0]}" not found in structure`);
}
subs.shift();
}
return foundType;
}
private resolveConstantValue(expr: ExpressionNode): string | undefined {
// todo: rewrite this method
if (!(expr.get() instanceof Expressions.SimpleFieldChain)) {
throw new Error("resolveConstantValue");
}
const firstNode = expr.getFirstChild()!;
const firstToken = firstNode.getFirstToken();
const firstName = firstToken.getStr();
if (firstNode.get() instanceof Expressions.Field) {
const found = this.input.scope.findVariable(firstName);
if (found === undefined) {
const message = `"${firstName}" not found, findTop`;
this.input.issues.push(syntaxIssue(this.input, firstToken, message));
return undefined;
}
this.input.scope.addReference(firstToken, found, ReferenceType.DataReadReference, this.input.filename);
if (this.isConstant(found) === false) {
const message = `VALUE, ${expr.concatTokens().toLowerCase()} is not a constant`;
this.input.issues.push(syntaxIssue(this.input, firstToken, message));
return undefined;
}
return this.constantValue(expr, found.getValue());
} else if (firstNode.get() instanceof Expressions.ClassName
&& firstName.toLowerCase() === this.input.scope.getName().toLowerCase()
&& (this.input.scope.getType() === ScopeType.Interface
|| this.input.scope.getType() === ScopeType.ClassDefinition)) {
const children = expr.getChildren();
const token = children[2]?.getFirstToken();
const found = this.input.scope.findVariable(token.getStr());
if (found === undefined) {
const message = `"${token.getStr()}" not found, findTop`;
this.input.issues.push(syntaxIssue(this.input, token, message));
return undefined;
}
this.input.scope.addReference(firstToken, found, ReferenceType.DataReadReference, this.input.filename);
if (this.isConstant(found) === false) {
const message = `VALUE, ${expr.concatTokens().toLowerCase()} is not a constant`;
this.input.issues.push(syntaxIssue(this.input, token, message));
return undefined;
}
return this.constantValue(expr, found.getValue());
} else if (firstNode.get() instanceof Expressions.ClassName) {
const obj = this.input.scope.findObjectDefinition(firstName);
if (obj === undefined) {
if (this.input.scope.existsObject(firstName) !== undefined) {
return undefined;
} else if (this.input.scope.getDDIC().inErrorNamespace(firstName) === true) {
throw new Error("resolveConstantValue, not found: " + firstName);
} else {
this.input.scope.addReference(
firstNode.getFirstToken(), undefined,
ReferenceType.ObjectOrientedVoidReference, this.input.filename, {ooName: firstName.toUpperCase()});
return undefined;
}
}
const children = expr.getChildren();
const token = children[2]?.getFirstToken();
const attr = token.getStr();
const c = new ObjectOriented(this.input.scope).searchConstantName(obj, attr);
if (c instanceof ClassConstant) {
this.input.scope.addReference(firstToken, obj, ReferenceType.ObjectOrientedReference, this.input.filename, {ooName: obj.getName()});
this.input.scope.addReference(token, c, ReferenceType.DataReadReference, this.input.filename);
return this.constantValue(expr, c.getValue());
}
const helper = new ObjectOriented(this.input.scope);
const attribute = helper.searchAttributeName(obj, attr);
if (attribute) {
this.input.scope.addReference(firstToken, obj, ReferenceType.ObjectOrientedReference, this.input.filename, {ooName: obj.getName()});
this.input.scope.addReference(token, attribute, ReferenceType.DataReadReference, this.input.filename);
const message = `VALUE, ${expr.concatTokens().toLowerCase()} is not a constant`;
this.input.issues.push(syntaxIssue(this.input, token, message));
return undefined;
}
const message = `Attribute or constant "${attr}" not found in "${obj.getName()}"`;
this.input.issues.push(syntaxIssue(this.input, token, message));
return undefined;
} else {
throw new Error("resolveConstantValue, unexpected structure");
}
}
private isConstant(identifier: TypedIdentifier): boolean {
if (identifier instanceof ClassAttribute) {
return false;
}
const meta = identifier.getMeta();
if (meta.includes(IdentifierMeta.ReadOnly) === false) {
return false;
}
if (meta.includes(IdentifierMeta.Static)) {
return true;
}
// Built-in constants and built-in system fields share ReadOnly/BuiltIn metadata.
// Only the constants have a value; system fields such as SY-INDEX do not.
return meta.includes(IdentifierMeta.BuiltIn) && identifier.getValue() !== undefined;
}
private constantValue(expr: ExpressionNode, value: string | {[index: string]: string} | undefined): string | undefined {
if (typeof value === "string") {
return value;
} else if (typeof value === "object" && value !== null) {
let current: string | {[index: string]: string} | undefined = value;
for (const child of expr.getChildren().slice(1)) {
if (child instanceof ExpressionNode && child.get() instanceof Expressions.ComponentName) {
if (typeof current !== "object" || current === null) {
return undefined;
}
current = current[child.getFirstToken().getStr()];
}
}
return typeof current === "string" ? current : undefined;
}
return undefined;
}
private resolveTypeRef(chain: ExpressionNode | undefined): AbstractType | undefined {
if (chain === undefined) {
return undefined;
}
const name = chain.getFirstToken().getStr();
if (chain.getChildren().length === 1) {
if (name.toUpperCase() === "OBJECT") {
return new Types.GenericObjectReferenceType();
}
const search = this.input.scope.existsObject(name);
if (search?.id) {
this.input.scope.addReference(
chain.getFirstToken(), search.id, ReferenceType.ObjectOrientedReference, this.input.filename,
{ooType: search.ooType, ooName: name});
return new Types.ObjectReferenceType(search.id, {qualifiedName: name, RTTIName: search.RTTIName});
}
}
const found = this.resolveTypeName(chain);
if (found && found instanceof Types.VoidType) {
return found;
} else if (found && !(found instanceof Types.UnknownType)) {
return new Types.DataReference(found);
} else if (chain.concatTokens().toUpperCase() === "DATA") {
return new Types.DataReference(new Types.DataType());
} else if (found instanceof Types.UnknownType && name.toUpperCase() === "ME") {
// "REF TO me=>ty", me is not a class or interface whatever the error namespace
return found;
}
if (this.input.scope.isBadiDef(name) === true) {
return Types.VoidType.get(name);
}
if (this.input.scope.getDDIC()?.inErrorNamespace(name) === false) {
// this.scope.addReference(chain.getFirstToken(), undefined, ReferenceType.VoidType, this.filename);
return Types.VoidType.get(name);
}
return new Types.UnknownType("REF, unable to resolve " + name);
}
public findValue(node: StatementNode | ExpressionNode): 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 undefined;
}
const constant = val.findFirstExpression(Expressions.Constant);
if (constant) {
const conc = val.findFirstExpression(Expressions.ConcatenatedConstant);
if (conc) {
const first = conc.getFirstToken().getStr().substring(0, 1);
let result = "";
for (const token of conc.getAllTokens()) {
if (token.getStr() === "&") {
continue;
} else {
result += token.getStr().substring(1, token.getStr().length - 1);
}
}
return first + result + first;
} else {
return constant.concatTokens();
}
}
const chain = val.findFirstExpression(Expressions.SimpleFieldChain);
if (chain) {
// todo, hardcoded for now, should be a generic check that the chain is a constant
const concat = chain.concatTokens().toUpperCase();
if (concat === "SY-DATUM" || concat === "SY-UZEIT") {
const message = "VALUE, " + concat.toLowerCase() + " is not a constant";
this.input.issues.push(syntaxIssue(this.input, chain.getFirstToken(), message));
return undefined;
} else if (concat === "SY-REPID" || concat === "SYST-REPID") {
// predefined constant in every program, not a SYST component
return undefined;
}
return this.resolveConstantValue(chain);
}
throw new Error("findValue, unexpected");
}
// the LENGTH and DECIMALS additions of DATA, TYPES, CONSTANTS and STATICS, as a system checks them:
// LENGTH only with TYPE c, n, x or p, DECIMALS only with TYPE p, also not without a TYPE addition,
// a named type cannot be given a length either. With LIKE, a system ignores LENGTH and rejects DECIMALS
private checkLengthDecimals(node: StatementNode | ExpressionNode, text: string) {
const length = node.findDirectExpression(Expressions.Length);
const decimals = node.findDirectExpression(Expressions.Decimals);
const fieldLength = node.get() instanceof Statements.Parameter
? undefined : node.findDirectExpression(Expressions.ConstantFieldLength);
if (length === undefined && decimals === undefined && fieldLength === undefined) {
return undefined;
} else if (text.startsWith("LIKE")) {
if (decimals === undefined && fieldLength === undefined) {
return undefined;
}
const message = "When referencing another field (with LIKE), type and length cannot be changed";
return syntaxIssue(this.input, (decimals ?? node).getFirstToken(), message);
}
if (fieldLength !== undefined) {
const fieldLengthIssue = this.checkFieldLength(node, text);
if (fieldLengthIssue !== undefined) {
return fieldLengthIssue;
}
}
const builtin = text.startsWith("TYPE") ? text.substring(4).trim() : "";
if (length !== undefined && ["C", "N", "X", "P"].includes(builtin) === false) {
return syntaxIssue(this.input, length.getFirstToken(), "\"LENGTH\" can only be specified for types C, N, X, or P");
} else if (decimals !== undefined && builtin !== "P") {
return syntaxIssue(this.input, decimals.getFirstToken(), "\"DECIMALS\" can be specified only for type P");
}
return undefined;
}
// the length in brackets, "DATA foo(8) TYPE d", as a system checks it: c, n, x and p take any length,
// d, t and f only their own length, i any length. Their allowed lengths are ignored with a warning,
// which is an error in classes and interfaces. Any other type is an error
private checkFieldLength(node: StatementNode | ExpressionNode, text: string) {
const typeNameExpression = node.findFirstExpression(Expressions.TypeName);
const typeName = typeNameExpression?.concatTokens().toUpperCase() ?? "";
if (typeNameExpression === undefined
|| text.includes(" REF TO ")
|| ["C", "N", "X", "P"].includes(typeName)) {
return undefined;
}
const token = typeNameExpression.getFirstToken();
const ownLength: {[type: string]: number} = {"D": 8, "T": 6, "F": 8};
if (ownLength[typeName] !== undefined) {
const length = this.findLength(node);
if (length !== undefined && length !== ownLength[typeName]) {
const message = "Type \"" + typeName + "\" can only have the specified length \"" + ownLength[typeName] + "\" or no length specified at all";
return syntaxIssue(this.input, token, message);
}
}
if ((ownLength[typeName] !== undefined || typeName === "I") && this.input.scope.isAnyOO() === false) {
return undefined;
}
return syntaxIssue(this.input, token, "Lengths specified in combination with type \"" + typeName + "\" are ignored");
}
private findDecimals(node: StatementNode | ExpressionNode): number | undefined {
const dec = node.findDirectExpression(Expressions.Decimals)?.findDirectExpression(Expressions.Integer)?.concatTokens();
if (dec) {
return parseInt(dec, 10);
}
if (node.get() instanceof Statements.Parameter) {
// PARAMETERS spells it seq("DECIMALS", Source) instead of the Decimals expression
const source = this.findParameterAddition(node, "DECIMALS")?.concatTokens();
if (source && /^\d+$/.test(source)) {
return parseInt(source, 10);
}
}
return undefined;
}
// PARAMETERS spells it seq("LENGTH", Constant) instead of the Length expression
private findParameterLength(node: StatementNode | ExpressionNode): number | undefined {
const constant = this.findParameterAddition(node, "LENGTH");
if (constant === undefined) {
return undefined;
}
const length = this.parseInt(constant.concatTokens());
return length === undefined || isNaN(length) ? undefined : length;
}
// finds the expression following a direct keyword token, "VISIBLE LENGTH" is not "LENGTH"
private findParameterAddition(node: StatementNode | ExpressionNode, keyword: string): ExpressionNode | undefined {
const children = node.getChildren();
for (let i = 0; i < children.length - 1; i++) {
const child = children[i];
const next = children[i + 1];
if (child instanceof ExpressionNode
|| child.getFirstToken().getStr().toUpperCase() !== keyword
|| children[i - 1]?.getFirstToken().getStr().toUpperCase() === "VISIBLE") {
continue;
}
return next instanceof ExpressionNode ? next : undefined;
}
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);
if (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) {
if (node.get() instanceof Statements.Parameter) {
return this.findParameterLength(node);
}
return undefined;
}
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);
if (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);
}
}
|