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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2309x 2309x 1x 1x 64x 64x 6x 10x 2x 2x 10x 4x 64x 2x 58x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 51x 51x 5x 5x 1x 1x 481x 481x 2x 481x 18x 28x 1x 1x 28x 17x 479x 461x 461x 461x 461x 461x 461x 461x 461x 461x 461x 461x 461x 461x 461x 461x 461x 461x 461x 461x 461x 461x 461x 450x 450x 11x 11x 1x 1x 22x 22x 22x 1x 22x 21x 21x 21x 21x 21x 21x 14x 14x 7x 7x 1x 1x 51x 51x 51x 51x 51x 51x 51x 45x 45x 6x 6x 6x 6x 51x 1x 1x 1x 1x 1x 6x 51x 1x 1x 1x 1x 1x 6x 51x 3x 3x 3x 2x 2x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 25x 25x 1x 1x 1x 24x 24x 24x 1x 1x 16x 16x 16x 1x 1x 186x 186x 186x 186x 186x 186x 186x 186x 1x 1x 186x 1x 1x 184x 184x 184x 1x 1x 381x 541x 87x 87x 541x 294x 294x 1x 1x 1x 2x 2x 1x 1x 1x 1x 242x 242x 242x 242x 242x 242x 242x 242x 242x 242x 242x 242x 1x 1x 929x 929x 929x 929x 929x 1x 1x 928x 928x 928x 1x 1x 482x 482x 482x 482x 482x 482x 482x 482x 482x 2x 2x 480x 482x 52x 52x 428x 482x 147x 122x 3x 3x 119x 147x 3x 1x 1x 2x 25x 2x 1x 1x 1x 22x 7x 6x 6x 1x 1x 482x 10x 6x 6x 10x 3x 4x 1x 1x 1x 281x 37x 37x 2x 1x 1x 1x 37x 35x 1x 35x 3x 1x 1x 2x 2x 31x 271x 22x 11x 11x 11x 1x 1x 11x 188x 1x 1x 188x 9x 11x 1x 11x 1x 10x 9x 9x 7x 7x 2x 234x 2x 2x 2x 212x 101x 1x 101x 4x 2x 2x 2x 2x 210x 1x 1x 1x 109x 2x 2x 2x 2x 2x 2x 2x 108x 11x 11x 11x 11x 11x 2x 2x 106x 7x 7x 7x 7x 7x 5x 5x 95x 38x 33x 33x 33x 33x 33x 4x 4x 33x 33x 1x 1x 28x 38x 5x 5x 38x 175x 175x 1x 1x 1446x 3x 3x 1443x 1443x 1443x 1443x 1443x 1446x 142x 3x 3x 139x 142x 142x 142x 7x 142x 130x 130x 130x 6x 130x 124x 130x 23x 1x 23x 22x 1x 1x 130x 101x 122x 122x 2x 1446x 51x 1301x 1250x 13x 13x 13x 13x 13x 13x 10x 10x 3x 1250x 19x 19x 19x 19x 19x 18x 18x 1x 1237x 141x 141x 141x 141x 141x 1x 141x 126x 126x 126x 1x 1x 125x 125x 125x 126x 5x 5x 125x 126x 185x 3x 3x 182x 185x 185x 1x 1x 185x 124x 124x 140x 2x 14x 1x 12x 3x 11x 6x 6x 2x 1218x 1077x 1077x 781x 2x 781x 779x 779x 6x 1x 1x 5x 779x 4x 773x 769x 769x 3x 3x 766x 1077x 2x 2x 2x 2x 2x 294x 294x 294x 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, SimpleType, StringType, StructureType, TableType, TimeType, UnknownType, VoidType, XGenericType, XSequenceType, XStringType} from "../types/basic";
import {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} from "../nodes";
// 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 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 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;
}
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;
}
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;
}
*/
const calculated = node.findFirstExpression(Expressions.MethodCallChain) !== undefined
|| node.findFirstExpression(Expressions.StringTemplate) !== undefined
|| node.findFirstExpression(Expressions.ArithOperator) !== undefined;
return calculated;
}
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): 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) {
return false;
}
if (calculated) {
return this.isAssignable(source, target);
}
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 true;
}
return false;
} 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 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 true;
}
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) {
return true;
}
return false;
} else if (source instanceof Integer8Type) {
if (target instanceof IntegerType || target instanceof StringType) {
return false;
}
} else if (source instanceof IntegerType) {
if (target instanceof StringType) {
return false;
} else if (target instanceof Integer8Type || target instanceof PackedType) {
if (source.getAbstractTypeData()?.derivedFromConstant === true) {
return true;
}
return false;
}
} 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 CLikeType
|| target instanceof IntegerType
|| target instanceof StringType
|| target instanceof ObjectReferenceType
|| target instanceof HexType) {
return false;
}
} else if (source instanceof TableType) {
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);
}
public isAssignable(source: AbstractType | undefined, target: AbstractType | undefined): boolean {
if (source === undefined || target === undefined) {
return true;
}
/*
console.dir(source);
console.dir(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;
}
} |