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

77.18% Statements 203/263
90.22% Branches 120/133
88.88% Functions 8/9
77.18% Lines 203/263

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 2631x 1x 1x 1x 1x 1x 1x 1x 1x 1x 800x 800x 1x 1x 3x   3x             3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 1x 1x 1x 1x 234x   234x 1x 234x 8x 13x 1x 1x 13x 7x 233x 225x 225x 225x 225x 225x 225x 225x 225x 225x 225x 225x 225x 225x 225x 225x 225x 225x 225x 219x 219x 6x 6x 1x 1x 6x   6x             6x 6x 6x 6x 6x 3x 3x 3x 3x 1x 1x 31x 31x 31x 31x 31x 31x 31x 26x 26x 5x 31x 1x 1x 1x 1x     1x 5x 31x 1x 1x 1x 1x     1x 5x 31x 3x     3x 3x 2x 2x 31x 2x 2x 2x         2x                       1x 1x 1x 1x                                               1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 7x 7x 7x 1x 1x 582x 582x 582x 582x 582x 99x 3x 3x 96x 99x 99x 99x 94x 94x 2x 582x 31x 483x 452x 4x 4x 4x 4x 4x 2x 2x 2x 452x 10x 10x 10x 10x 9x 9x 1x 448x 37x   37x 37x 37x 37x 26x 37x 2x 11x 1x 9x 6x 6x 2x 438x 401x 296x 1x 296x 295x 295x 1x 1x 294x 294x 105x 105x 105x 1x
import {ClassDefinition, InterfaceDefinition} from "../types";
import {AnyType, CharacterType, CLikeType, CSequenceType, DataReference, DateType, DecFloat16Type, DecFloat34Type, DecFloatType, FloatingPointType, FloatType, GenericObjectReferenceType, HexType, IntegerType, NumericGenericType, NumericType, ObjectReferenceType, PackedType, SimpleType, StringType, StructureType, TableType, TimeType, UnknownType, VoidType, XStringType} from "../types/basic";
import {AbstractType} from "../types/basic/_abstract_type";
import {CurrentScope} from "./_current_scope";
 
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 StringType
        || type instanceof AnyType
        || type instanceof CharacterType
        || type instanceof SimpleType
        || 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 VoidType
        || type instanceof AnyType
        || type instanceof UnknownType
        || type instanceof NumericType
        || type instanceof IntegerType
        || 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 DateType
        || type instanceof CLikeType
        || type instanceof PackedType
        || type instanceof TimeType
        || type instanceof CharacterType) {
      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 XStringType
        || type instanceof HexType
        || type instanceof VoidType
        || 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;
    }
 
    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 isCastable(_source: AbstractType | undefined, _target: AbstractType | undefined): boolean {
// todo
    return true;
  }
 
  public isAssignable(source: AbstractType | undefined, target: AbstractType | undefined): boolean {
/*
    console.dir(source);
    console.dir(target);
*/
    if (target instanceof TableType) {
      if (target.isWithHeader()) {
        return this.isAssignable(source, target.getRowType());
      }
      if (source instanceof TableType
          || source instanceof VoidType
          || source instanceof AnyType
          || source instanceof UnknownType) {
        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 AnyType
          || source instanceof UnknownType) {
        return true;
      }
      return false;
    } else if (target instanceof DataReference) {
      if (source instanceof DataReference
          || source instanceof VoidType
          || 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 StructureType
          || source instanceof VoidType
          || source instanceof AnyType
          || source instanceof UnknownType) {
        return true;
      } else if (target.containsVoid() === true) {
        return true;
      } 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 StringType) {
      if (source instanceof TableType && source.isWithHeader() === false) {
        return false;
      } else if (source instanceof DataReference
          || source instanceof ObjectReferenceType
          || source instanceof GenericObjectReferenceType) {
        return false;
      }
      return true;
    }
 
    return true;
  }
}