All files / src/abap/5_syntax/expressions cast.ts

84.38% Statements 27/32
63.64% Branches 7/11
100% Functions 1/1
84.38% Lines 27/32

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 321x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x   4x 1x 4x     3x 3x 4x 1x 4x     2x 2x 2x 1x
import {ExpressionNode} from "../../nodes";
import {CurrentScope} from "../_current_scope";
import {ObjectReferenceType, VoidType} from "../../types/basic";
import * as Expressions from "../../2_statements/expressions";
import {AbstractType} from "../../types/basic/_abstract_type";
import {Source} from "./source";
 
export class Cast {
  public runSyntax(node: ExpressionNode, scope: CurrentScope, targetType: AbstractType | undefined, filename: string): AbstractType {
    for (const s of node.findAllExpressions(Expressions.Source)) {
      new Source().runSyntax(s, scope, filename);
    }
 
    const typeName = node.findDirectExpression(Expressions.TypeNameOrInfer)?.getFirstToken().getStr();
    if (typeName === undefined) {
      throw new Error("Cast, child TypeNameOrInfer not found");
    } else if (typeName === "#" && targetType) {
      return targetType;
    } else if (typeName === "#") {
      throw new Error("Cast, todo, infer type");
    }
 
    const found = scope.findObjectDefinition(typeName);
    if (found === undefined && scope.getDDIC().inErrorNamespace(typeName) === false) {
      return new VoidType(typeName);
    } else if (found === undefined) {
      throw new Error("Type \"" + typeName + "\" not found in scope, Cast");
    }
 
    return new ObjectReferenceType(found);
  }
}