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

84.21% Statements 272/323
69.47% Branches 66/95
100% Functions 10/10
84.21% Lines 272/323

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 3241x 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 1x 1x 669x 669x 669x 1x 570x 570x 570x 570x 570x 121x     121x 121x 121x 5x 121x 116x 116x 121x 121x 127x 127x 127x 29x 29x 127x 25x 25x 127x 66x 66x 127x 6x 6x 127x 1x 1x 1x 1x 1x 1x 1x 1x 1x 127x   127x 127x 120x 120x 120x 570x 570x 570x 570x 6x 6x 6x       6x         6x 6x 6x 6x 1x 6x 5x 5x         5x 6x 570x 570x 25x 25x 25x 1x 25x 24x 24x         24x 24x 25x 25x 25x 3x 25x       22x 1x 1x 1x 1x 25x 23x 570x 570x 66x 66x 1x 1x 1x 1x 65x 65x 66x 1x 66x 64x 64x         64x 64x 65x 66x 1x 1x 1x 1x 64x 66x 66x 64x 570x 570x 478x 478x 478x 478x 10x 10x 468x 468x 478x 440x 440x 468x 478x 564x 564x 5x 5x 5x 564x 1x 559x 7x 7x 7x 7x 551x 551x 460x 478x 433x 433x 478x 570x 570x 553x 5x 5x 5x 553x 570x 570x 478x 478x 478x 478x 478x   478x     478x 478x 478x 478x 478x 577x     577x 577x 577x 577x     577x 1x 1x 1x 577x 577x 577x 577x     577x 577x 577x 564x 824x 559x 559x 559x 824x 577x 13x 13x 577x 577x 577x 577x 577x 577x                 577x 577x 577x 478x 478x 478x 570x 570x 91x 91x 91x 91x   91x     91x 91x 91x 91x 91x 92x     92x 92x 92x 92x     92x 1x 1x 1x 92x 92x 92x 92x     92x 92x 92x 92x 92x 90x 90x 90x 570x 570x  
import {VoidType} from "../../types/basic";
import * as Expressions from "../../2_statements/expressions";
import {IMethodDefinition} from "../../types/_method_definition";
import {ExpressionNode} from "../../nodes";
import {InlineData} from "./inline_data";
import {Target} from "./target";
import {AbstractType} from "../../types/basic/_abstract_type";
import {INode} from "../../nodes/_inode";
import {Source} from "./source";
import {TypeUtils} from "../_type_utils";
import {CheckSyntaxKey, SyntaxInput, syntaxIssue} from "../_syntax_input";
import {AssertError} from "../assert_error";
import {FieldChain} from "./field_chain";
import {ReferenceType} from "../_reference";
import {checkOffsetLength} from "./_check_offset_length";
import {checkTextSymbol} from "./_check_text_symbol";
 
interface IListItemT {
  name: string;
  target: ExpressionNode;
  targetType: AbstractType | undefined;
}
 
interface IListItemS {
  name: string;
  source: ExpressionNode;
  sourceType: AbstractType | undefined;
}
 
function normalizeParameterName(name: string): string {
  return name.replace(/^!/, "").toUpperCase();
}
 
export class MethodParameters {
 
  private requiredParameters: Set<string> | undefined = undefined;
 
  public runSyntax(node: INode, input: SyntaxInput, method: IMethodDefinition | VoidType): void {
    if (!(node.get() instanceof Expressions.MethodParameters)) {
      throw new AssertError("MethodParameters, unexpected input");
    }
 
    const children = node.getChildren().slice();
    if (method instanceof VoidType) {
      this.requiredParameters = new Set();
    } else {
      this.requiredParameters = new Set(method.getParameters().getRequiredParameters().map(i => i.getName().toUpperCase()));
    }
 
    while (children.length > 0) {
      const name = children.shift()?.getFirstToken().getStr().toUpperCase();
      switch (name) {
        case "EXPORTING":
          this.checkExporting(children.shift()!, input, method, false);
          break;
        case "IMPORTING":
          this.checkImporting(children.shift()!, input, method);
          break;
        case "CHANGING":
          this.checkChanging(children.shift()!, input, method);
          break;
        case "RECEIVING":
          this.checkReceiving(children.shift()!, input, method);
          break;
        case "EXCEPTIONS":
          {
            // todo, old style exceptions
            const node = children.shift()! as ExpressionNode;
            const exceptions = node.findFirstExpression(Expressions.ParameterException);
            for (const s of exceptions?.findAllExpressions(Expressions.SimpleFieldChain) || []) {
              FieldChain.runSyntax(s, input, ReferenceType.DataReadReference);
            }
          }
          break;
        default:
          throw new AssertError("MethodParameters, unexpected token, " + name);
      }
    }
 
    this.reportErrors(node, input);
  }
 
///////////////////////
 
  private checkReceiving(node: INode, input: SyntaxInput, method: IMethodDefinition | VoidType) {
 
    const type = method instanceof VoidType ? method : method.getParameters().getReturning()?.getType();
    if (type === undefined) {
      const message = "Method does not have a returning parameter";
      input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
      return;
    } else if (!(node instanceof ExpressionNode)) {
      const message = "checkReceiving, not an expression node";
      input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
      return;
    }
 
    const target = node.findDirectExpression(Expressions.Target);
    const inline = target?.findDirectExpression(Expressions.InlineData);
    if (inline) {
      InlineData.runSyntax(inline, input, type);
    } else if (target) {
      const targetType = Target.runSyntax(target, input);
      if (targetType && new TypeUtils(input.scope).isAssignable(type, targetType) === false) {
        const message = "Method returning value not type compatible";
        input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
        return;
      }
    }
  }
 
  private checkImporting(node: INode, input: SyntaxInput, method: IMethodDefinition | VoidType) {
    for (const item of this.parameterListT(node, input)) {
      let parameterType: AbstractType | undefined;
      if (method instanceof VoidType) {
        parameterType = method;
      } else {
        const parameter = method.getParameters().getExporting().find(p => p.getName().toUpperCase() === item.name);
        if (parameter === undefined) {
          const message = "Method exporting parameter \"" + item.name + "\" does not exist";
          input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
          return;
        }
        parameterType = parameter.getType();
      }
 
      const inline = item.target.findDirectExpression(Expressions.InlineData);
      if (inline) {
        InlineData.runSyntax(inline, input, parameterType);
      } else if (item.targetType === undefined) {
        const message = "Could not determine target type";
        input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
        return;
      } else if (item.targetType && new TypeUtils(input.scope).isAssignableStrict(parameterType, item.targetType) === false) {
        const message = "Method parameter type not compatible, " + item.name;
        input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
        return;
      }
    }
  }
 
  private checkChanging(node: INode, input: SyntaxInput, method: IMethodDefinition | VoidType) {
    for (const item of this.parameterListT(node, input)) {
      if (item.target.findFirstExpression(Expressions.InlineData) !== undefined) {
        const message = "CHANGING cannot be inlined";
        input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
        return;
      }
 
      let parameterType: AbstractType | undefined;
      if (method instanceof VoidType) {
        parameterType = method;
      } else {
        const parameter = method.getParameters().getChanging().find(p => p.getName().toUpperCase() === item.name);
        if (parameter === undefined) {
          const message = "Method changing parameter \"" + item.name + "\" does not exist";
          input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
          return;
        }
        parameterType = parameter.getType();
      }
 
      if (item.targetType && new TypeUtils(input.scope).isAssignableStrict(parameterType, item.targetType) === false) {
        const message = "Method parameter type not compatible, " + item.name;
        input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
        return;
      }
 
      this.requiredParameters?.delete(item.name);
    }
  }
 
  public checkExporting(node: INode, input: SyntaxInput,
                        method: IMethodDefinition | VoidType, errors = true): void {
 
    const items = this.parameterListS(node, input, method);
    if (method instanceof VoidType) {
      return;
    }
 
    const allImporting = method.getParameters().getImporting();
    if (this.requiredParameters === undefined) {
      this.requiredParameters = new Set(method.getParameters().getRequiredParameters().map(i => i.getName().toUpperCase()));
    }
 
    for (const item of items) {
      const parameter = allImporting.find(p => p.getName().toUpperCase() === item.name);
      if (parameter === undefined) {
        const message = "Method importing parameter \"" + item.name + "\" does not exist";
        input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
        continue;
      } else if (checkTextSymbol(item.source, parameter.getType(), input)) {
        return;
      } else if (new TypeUtils(input.scope).isAssignableStrict(item.sourceType, parameter.getType(), item.source) === false) {
        const message = "Method parameter type not compatible, " + item.name;
        input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
        return;
      }
      this.requiredParameters.delete(item.name);
    }
 
    if (errors === true) {
      this.reportErrors(node, input);
    }
  }
 
  private reportErrors(node: INode, input: SyntaxInput) {
    for (const r of this.requiredParameters?.values() || []) {
      const message = `method parameter "${r}" must be supplied`;
      input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
    }
  }
 
  private parameterListS(
    node: INode | undefined,
    input: SyntaxInput,
    method: IMethodDefinition | VoidType): IListItemS[] {
 
    if (node === undefined) {
      return [];
    } else if (!(node.get() instanceof Expressions.ParameterListS)) {
      throw new AssertError("parameterListS, unexpected node");
    }
 
    const ret: IListItemS[] = [];
    const supplied = new Set<string>();
 
    for (const c of node.getChildren()) {
      if (!(c.get() instanceof Expressions.ParameterS) || !(c instanceof ExpressionNode)) {
        throw new AssertError("parameterListS, unexpected node, child");
      }
 
      const nameNode = c.findDirectExpression(Expressions.ParameterName);
      const name = nameNode === undefined ? undefined : normalizeParameterName(nameNode.getFirstToken().getStr());
      if (name === undefined || nameNode === undefined) {
        throw new AssertError("parameterListS, no name determined");
      }
      if (supplied.has(name)) {
        const message = `Method parameter "${name}" is supplied more than once`;
        input.issues.push(syntaxIssue(input, nameNode.getFirstToken(), message));
      }
      supplied.add(name);
 
      const source = c.findDirectExpression(Expressions.Source);
      if (source === undefined) {
        throw new AssertError("parameterListS, no source found");
      }
 
      let targetType: AbstractType | undefined = undefined;
      if (!(method instanceof VoidType)) {
        for (const i of method.getParameters().getImporting()) {
          if (i.getName().toUpperCase() === name) {
            targetType = i.getType();
            break;
          }
        }
      } else {
        targetType = method;
      }
 
      let sourceType = Source.runSyntax(source, input, targetType);
 
      checkOffsetLength(source, sourceType, method, input);
 
      if (sourceType === undefined) {
        if (method instanceof VoidType) {
          sourceType = method;
        } else {
          const message = "No source type determined for parameter " + name + " input";
          input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
          sourceType = VoidType.get(CheckSyntaxKey);
        }
      }
 
      ret.push({name, source, sourceType});
    }
 
    return ret;
  }
 
  private parameterListT(
    node: INode | undefined,
    input: SyntaxInput): IListItemT[] {
 
    if (node === undefined) {
      return [];
    } else if (!(node.get() instanceof Expressions.ParameterListT)) {
      throw new AssertError("parameterListT, unexpected node");
    }
 
    const ret: IListItemT[] = [];
    const supplied = new Set<string>();
 
    for (const c of node.getChildren()) {
      if (!(c.get() instanceof Expressions.ParameterT) || !(c instanceof ExpressionNode)) {
        throw new AssertError("parameterListT, unexpected node, child");
      }
 
      const nameNode = c.findDirectExpression(Expressions.ParameterName);
      const name = nameNode === undefined ? undefined : normalizeParameterName(nameNode.getFirstToken().getStr());
      if (name === undefined || nameNode === undefined) {
        throw new AssertError("parameterListT, no name determined");
      }
      if (supplied.has(name)) {
        const message = `Method parameter "${name}" is supplied more than once`;
        input.issues.push(syntaxIssue(input, nameNode.getFirstToken(), message));
      }
      supplied.add(name);
 
      const target = c.findDirectExpression(Expressions.Target);
      if (target === undefined) {
        throw new AssertError("parameterListT, no target found");
      }
 
      const targetType = Target.runSyntax(target, input);
 
      ret.push({name, target, targetType});
    }
 
    return ret;
  }
 
}