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

90.86% Statements 189/208
88.77% Branches 87/98
100% Functions 2/2
90.86% Lines 189/208

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 2091x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5953x 5953x 5953x 455x 455x 455x 2x 2x 2x 455x 5951x 5951x 5951x 5953x     5951x 5951x 5953x 71x 71x 71x 71x 5880x 5880x 5880x 5953x 6946x 6946x 5859x 5859x 1087x 1087x 6946x 314x   314x 1x 314x 1x 313x 312x 312x         6946x 125x 2x 2x 123x 125x 125x 13x 13x 13x 13x 773x 20x         20x 20x 19x 19x 20x 20x 20x 20x 3x 3x 648x 312x 628x 8x 8x 8x 8x 1x 1x 1x 1x 8x 5x 5x 316x 308x 17x         17x 17x 17x 17x 17x 308x 115x 115x 115x 6946x 5859x 5859x 5859x 5859x 5953x 90x 3x 3x 3x 3x 87x 90x 84x 84x 90x 5856x 5856x 5953x 81x 2x 2x 2x 2x 79x 81x 76x 76x 81x 5953x 26x 26x 5828x 5828x 5828x 1x 1x 1x 1x 5951x     5951x 5951x 5951x 5951x 5951x 5951x 5739x 5739x 5668x 5668x 5739x 4x 4x 4x 4x 4x 5739x 5951x 5x 5x 4x 4x 5x 1x 1x 1x     212x 2x 2x 1x 1x 1x 1x 1x 1x 205x 205x 205x 1x  
import * as Expressions from "../../2_statements/expressions";
import {ExpressionNode} from "../../nodes";
import {AbstractType} from "../../types/basic/_abstract_type";
import {UnknownType} from "../../types/basic/unknown_type";
import {INode} from "../../nodes/_inode";
import {Dash, InstanceArrow} from "../../1_lexer/tokens";
import {StructureType, ObjectReferenceType, VoidType, DataReference, TableType, XStringType, StringType} from "../../types/basic";
import {ComponentName} from "./component_name";
import {AttributeName} from "./attribute_name";
import {FieldOffset} from "./field_offset";
import {ReferenceType} from "../_reference";
import {TableExpression} from "./table_expression";
import {Dereference} from "../../2_statements/expressions";
import {FieldLength} from "./field_length";
import {Cast} from "./cast";
import {isFieldSymbolItself, substringType} from "./_substring";
import {CheckSyntaxKey, SyntaxInput, syntaxIssue} from "../_syntax_input";
import {LanguageVersion, Release, releaseAtLeast} from "../../../version";
 
export class Target {
  public static runSyntax(node: ExpressionNode, input: SyntaxInput): AbstractType | undefined {
 
    const concat = node.concatTokens();
    if (concat.includes("-")) {
      // workaround for names with dashes
      const found = input.scope.findVariable(concat);
      if (found) {
        input.scope.addReference(node.getFirstToken(), found, ReferenceType.DataWriteReference, input.filename);
        return found.getType();
      }
    }
 
    const children = node.getChildren();
    const first = children[0];
    if (first === undefined || !(first instanceof ExpressionNode)) {
      return undefined;
    }
 
    let context = this.findTop(first, input);
    if (context === undefined) {
      const message = `"${first.getFirstToken().getStr()}" not found, Target`;
      input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
      return VoidType.get(CheckSyntaxKey);
    }
 
    let currentIndex = 1;
    let afterTableExpression = false;
    while (currentIndex <= children.length) {
      const current = children[currentIndex];
      if (current === undefined) {
        break;
      }
      currentIndex++;
 
      if (current.get() instanceof Dash) {
        if (context instanceof UnknownType) {
          return VoidType.get(CheckSyntaxKey);
        } else if (context instanceof TableType && context.isWithHeader() && context.getRowType() instanceof UnknownType) {
          return VoidType.get(CheckSyntaxKey);
        } else if (context instanceof TableType && context.isWithHeader() && context.getRowType() instanceof VoidType) {
          return context.getRowType();
        } else if (!(context instanceof StructureType)
            && !(context instanceof TableType && context.isWithHeader() && context.getRowType() instanceof StructureType)
            && !(context instanceof VoidType)) {
          const message = "Not a structure, target, " + context?.constructor.name + ", " + current.concatTokens();
          input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
          return VoidType.get(CheckSyntaxKey);
        }
      } else if (current.get() instanceof InstanceArrow) {
        if (afterTableExpression && TableExpression.checkArrowAfter(input, current.getFirstToken())) {
          return VoidType.get(CheckSyntaxKey);
        }
        if (!(context instanceof ObjectReferenceType)
            && !(context instanceof DataReference)
            && !(context instanceof VoidType)) {
          const message = "Not an object reference, target";
          input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
          return VoidType.get(CheckSyntaxKey);
        }
      } else if (current.get() instanceof Dereference) {
        if (!(context instanceof DataReference) && !(context instanceof VoidType)) {
          const message = "Not an object reference, target";
          input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
          return VoidType.get(CheckSyntaxKey);
        }
 
        if (!(context instanceof VoidType)) {
          context = context.getType();
        }
        if (context?.isGeneric() === true
            && !releaseAtLeast(input.scope.getRelease(), Release.v756)
            && input.scope.getLanguageVersion() !== LanguageVersion.Cloud
            && !input.scope.getOpenABAP()) {
          throw new Error("A generic reference cannot be dereferenced");
        }
      } else if (current.get() instanceof Expressions.ComponentName) {
        context = ComponentName.runSyntax(context, current, input);
      } else if (current.get() instanceof Expressions.TableBody) {
        if (!(context instanceof TableType)
            && !(context instanceof VoidType)
            && !(context instanceof UnknownType)
            && !(context instanceof UnknownType)) {
          const message = `Not an internal table, "${node.concatTokens()}"`;
          input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
          return VoidType.get(CheckSyntaxKey);
        }
        if (context instanceof TableType && context.isWithHeader()) {
          context = new TableType(context.getRowType(), {...context.getOptions(), withHeader: false});
        }
      } else if (current instanceof ExpressionNode
          && current.get() instanceof Expressions.TableExpression) {
        if (!(context instanceof TableType) && !(context instanceof VoidType) && !(context instanceof UnknownType)) {
          const message = "Table expression, expected table";
          input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
          return VoidType.get(CheckSyntaxKey);
        }
        TableExpression.runSyntax(current, input, context);
        afterTableExpression = true;
        if (!(context instanceof VoidType) && !(context instanceof UnknownType)) {
          context = context.getRowType();
        }
      } else if (current.get() instanceof Expressions.AttributeName) {
        const type = children.length === 0 ? ReferenceType.DataWriteReference : ReferenceType.DataReadReference;
        context = AttributeName.runSyntax(context, current, input, type);
      }
    }
 
    // the substring is checked, but the type of the whole field is returned
    let substring = context;
    const offset = node.findDirectExpression(Expressions.FieldOffset);
    if (offset) {
      if (context instanceof XStringType || context instanceof StringType) {
        const message = "xstring/string offset/length in writer position not possible";
        input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
        return VoidType.get(CheckSyntaxKey);
      }
      const value = FieldOffset.runSyntax(offset, input);
      if (!isFieldSymbolItself(children, children.indexOf(offset))) {
        substring = substringType(substring, value, undefined, offset, input);
      }
    }
 
    const length = node.findDirectExpression(Expressions.FieldLength);
    if (length) {
      if (context instanceof XStringType || context instanceof StringType) {
        const message = "xstring/string offset/length in writer position not possible";
        input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
        return VoidType.get(CheckSyntaxKey);
      }
      const value = FieldLength.runSyntax(length, input);
      if (!isFieldSymbolItself(children, children.indexOf(length))) {
        substring = substringType(substring, undefined, value, length, input);
      }
    }
    if (substring instanceof VoidType && !(context instanceof VoidType)) {
      return substring;
    }
 
    return context;
  }
 
/////////////////////////////////
 
  private static findTop(node: INode | undefined, input: SyntaxInput): AbstractType | undefined {
    if (node === undefined) {
      return undefined;
    }
 
    const token = node.getFirstToken();
    const name = token.getStr();
 
    if (node.get() instanceof Expressions.TargetField
        || node.get() instanceof Expressions.TargetFieldSymbol) {
      const found = input.scope.findVariable(name);
      if (found) {
        input.scope.addReference(token, found, ReferenceType.DataWriteReference, input.filename);
      }
      if (name.includes("~")) {
        const idef = input.scope.findInterfaceDefinition(name.split("~")[0]);
        if (idef) {
          input.scope.addReference(token, idef, ReferenceType.ObjectOrientedReference, input.filename);
        }
      }
      return found?.getType();
    } else if (node.get() instanceof Expressions.ClassName) {
      const found = input.scope.findObjectDefinition(name);
      if (found) {
        input.scope.addReference(token, found, ReferenceType.ObjectOrientedReference, input.filename);
        return new ObjectReferenceType(found);
      } else if (input.scope.getDDIC().inErrorNamespace(name) === false) {
        input.scope.addReference(token, undefined, ReferenceType.ObjectOrientedVoidReference, input.filename, {ooName: name, ooType: "CLAS"});
        return VoidType.get(name);
      } else {
        return new UnknownType(name + " unknown, Target");
      }
    } else if (node.get() instanceof Expressions.Cast && node instanceof ExpressionNode) {
      const ret = Cast.runSyntax(node, input, undefined);
      if (ret instanceof UnknownType) {
        const message = "CAST, uknown type";
        input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
        return VoidType.get(CheckSyntaxKey);
      }
      return ret;
    }
 
    return new UnknownType("unknown target type");
  }
}