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

89.38% Statements 160/179
81.35% Branches 48/59
100% Functions 3/3
89.38% Lines 160/179

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 1791x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 344x 344x 344x 344x 344x     344x 344x 344x   344x 149x 149x 148x 148x 148x 148x 148x 149x 1x 1x 1x 1x 1x 1x 1x 148x 148x 149x 1x 1x 1x 1x 147x 147x 147x       147x 344x 15x 195x     342x 344x 180x 180x 152x 152x 152x 152x 152x 152x 152x 152x 152x 1x 1x 1x 1x 151x 151x 151x       151x 151x 151x 180x 341x 344x 28x 28x 28x 6x 28x 10x 22x 12x 12x         28x 341x 344x 298x 344x 43x 17x 17x 43x 341x 344x 6x 6x 6x 6x 335x 335x 335x 1x 1x 298x 298x 298x 298x 298x 298x 298x 298x 298x 298x 8x 8x 8x 1x 1x 1x 1x 7x 8x 1x 1x 1x 1x 298x 18x 18x         18x 290x 1x 1x 1x 1x 298x 1x 1x 8x 8x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x
import {ExpressionNode} from "../../nodes";
import {ObjectReferenceType, VoidType, DataReference, UnknownType} from "../../types/basic";
import * as Expressions from "../../2_statements/expressions";
import {AbstractType} from "../../types/basic/_abstract_type";
import {ReferenceType} from "../_reference";
import {Source} from "./source";
import {ObjectOriented} from "../_object_oriented";
import {IMethodDefinition} from "../../types/_method_definition";
import {MethodParameters} from "./method_parameters";
import {BasicTypes} from "../basic_types";
import {TypeUtils} from "../_type_utils";
import {CheckSyntaxKey, SyntaxInput, syntaxIssue} from "../_syntax_input";
import {AssertError} from "../assert_error";
import {TypedIdentifier} from "../../types/_typed_identifier";
import {CreateObject} from "../statements/create_object";
 
export class NewObject {
  public static runSyntax(node: ExpressionNode, input: SyntaxInput, targetType: AbstractType | undefined): AbstractType {
    let ret: AbstractType | undefined = undefined;
 
    const typeExpr = node.findDirectExpression(Expressions.TypeNameOrInfer);
    const typeToken = typeExpr?.getFirstToken();
    if (typeToken === undefined) {
      throw new Error("NewObject, child TypeNameOrInfer not found");
    }
    const typeName = typeExpr?.concatTokens();
 
    if (typeName === undefined) {
      throw new AssertError("NewObject, child TypeNameOrInfer not found");
    } else if (typeName === "#" && targetType && targetType instanceof ObjectReferenceType) {
      const clas = input.scope.findClassDefinition(targetType.getIdentifierName());
      if (clas) {
        const tid = new TypedIdentifier(typeToken, input.filename, targetType);
        input.scope.addReference(typeToken, tid, ReferenceType.InferredType, input.filename);
 
        input.scope.addReference(typeToken, clas, ReferenceType.ConstructorReference, input.filename,
                                 {ooName: clas.getName()});
      } else {
        const intf = input.scope.findInterfaceDefinition(targetType.getIdentifierName());
        if (intf) {
          const message = intf.getName() + " is an interface, cannot be instantiated";
          input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
          return VoidType.get(CheckSyntaxKey);
        }
      }
      ret = targetType;
 
      if (clas?.isAbstract() === true) {
        const message = clas.getName() + " is abstract, cannot be instantiated";
        input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
        return VoidType.get(CheckSyntaxKey);
      }
      if (clas) {
        const err = CreateObject.checkInstantiationAllowed(clas, input);
        if (err) {
          input.issues.push(syntaxIssue(input, node.getFirstToken(), err));
          return VoidType.get(CheckSyntaxKey);
        }
      }
    } else if (typeName === "#" && targetType) {
      ret = targetType;
    } else if (typeName === "#") {
      throw new AssertError("NewObject, todo, infer type");
    }
 
    if (ret === undefined) {
      const objDefinition = input.scope.findObjectDefinition(typeName);
      if (objDefinition) {
        const objref = new ObjectReferenceType(objDefinition);
        const tid = new TypedIdentifier(typeToken, input.filename, objref);
        input.scope.addReference(typeToken, tid, ReferenceType.InferredType, input.filename);
 
        input.scope.addReference(typeToken, objDefinition, ReferenceType.ObjectOrientedReference, input.filename);
        input.scope.addReference(typeToken, objDefinition, ReferenceType.ConstructorReference, input.filename,
                                 {ooName: objDefinition.getName()});
        const clas = input.scope.findClassDefinition(objref.getIdentifierName());
        if (clas?.isAbstract() === true) {
          const message = clas.getName() + " is abstract, cannot be instantiated";
          input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
          return VoidType.get(CheckSyntaxKey);
        }
        if (clas) {
          const err = CreateObject.checkInstantiationAllowed(clas, input);
          if (err) {
            input.issues.push(syntaxIssue(input, node.getFirstToken(), err));
            return VoidType.get(CheckSyntaxKey);
          }
        }
        ret = objref;
      }
    }
 
    if (ret === undefined) {
      const basic = new BasicTypes(input);
      const type = basic.resolveTypeName(typeExpr);
      if (type instanceof UnknownType) {
        ret = type;
      } else if (type && !(type instanceof VoidType)) {
        ret = new DataReference(type);
      } else if (type instanceof VoidType) {
        ret = type;
      } else {
        const message = "Type \"" + typeName + "\" not found in scope, NewObject";
        input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
        return VoidType.get(CheckSyntaxKey);
      }
    }
 
    if (ret instanceof ObjectReferenceType) {
      this.parameters(node, ret, input);
    } else {
      for (const s of node.findAllExpressions(Expressions.Source)) {
        Source.runSyntax(s, input, ret);
      }
    }
 
    if (ret instanceof UnknownType && input.scope.getDDIC().inErrorNamespace(typeName) === true) {
      const message = "Class or type \"" + typeName + "\" not found";
      input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
      return VoidType.get(CheckSyntaxKey);
    }
 
    return ret;
  }
 
  private static parameters(node: ExpressionNode, obj: ObjectReferenceType, input: SyntaxInput) {
    const name = obj.getIdentifier().getName();
    const def = input.scope.findObjectDefinition(name);
    const helper = new ObjectOriented(input.scope);
    // eslint-disable-next-line prefer-const
    let {method} = helper.searchMethodName(def, "CONSTRUCTOR");
    const requiredParameters = method?.getParameters().getRequiredParameters() || [];
 
    const source = node.findDirectExpression(Expressions.Source);
    const parameters = node.findDirectExpression(Expressions.ParameterListS);
    if (source) {
      // single unnamed parameter
      const type = this.defaultImportingType(method);
      if (type === undefined) {
        const message = "NewObject, no default importing parameter found for constructor, " + name;
        input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
        return;
      }
      const sourceType = Source.runSyntax(source, input, type);
      if (new TypeUtils(input.scope).isAssignableStrict(sourceType, type) === false) {
        const message = `NEW parameter type not compatible`;
        input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
        return;
      }
    } else if (parameters) {
      // parameters with names
      if (method === undefined) {
        const message = "NewObject, no parameters for constructor found, " + name;
        input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
        return;
      }
      new MethodParameters().checkExporting(parameters, input, method);
    } else if (requiredParameters.length > 0) {
      const message = `constructor parameter "${requiredParameters[0].getName()}" must be supplied, ` + name;
      input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
      return;
    }
  }
 
  private static defaultImportingType(method: IMethodDefinition | undefined) {
    let targetType: AbstractType | undefined = undefined;
    if (method === undefined) {
      return undefined;
    }
    const name = method.getParameters().getDefaultImporting();
    for (const i of method.getParameters().getImporting()) {
      if (i.getName().toUpperCase() === name) {
        targetType = i.getType();
      }
    }
    return targetType;
  }
 
}