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

97.58% Statements 323/331
93.28% Branches 125/134
100% Functions 5/5
97.58% Lines 323/331

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 324 325 326 327 328 329 330 3311x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3442x 3442x 3442x 3442x 3442x 3442x 3442x 4x 4x 3438x 3438x 3438x 3438x 3442x 316x 316x 316x 316x 316x 316x 316x 5x 316x 9x 9x 9x 9x 9x 9x 316x 11x 11x 11x 11x 11x 11x 316x 39x 39x 39x 39x 2x 39x 36x 36x 39x 39x 316x 10x 10x 10x 10x 5x 5x 5x 5x 10x 10x 316x 32x 32x 32x 32x 10x 32x 21x 21x 31x 31x 31x 31x 31x 32x 32x 316x 20x 20x 20x 20x 1x 1x 19x 19x 19x 316x 7x 7x 7x 7x 3x 7x 2x 2x 7x 316x 5x 5x 5x 5x 2x 5x 3x 3x 5x 5x 4x 5x 1x 1x 5x 316x 11x 11x 11x 11x 11x 11x 316x 1x 316x 166x 166x 166x 166x 32x 166x 127x 127x 166x 166x 316x   316x 3442x     3127x 3127x 3127x 3127x 3442x 36x 36x 3127x 3127x 3127x 3442x 3390x 169x 169x 3x 3x 3390x 1725x 3221x 164x 1496x 120x 120x 1332x 1064x 1064x 1212x 7x 148x 2x 141x 82x 2x 2x 82x 82x 6x 6x 6x 139x 4x 4x 3251x 3390x 11x 11x 11x 11x 11x 11x 5x 5x 11x 1x 1x 6x 6x 3246x 3246x 3390x 2983x 2983x 3390x 2983x 3442x 35x 35x 2948x 2948x 2948x 1x 1x 1x 1x 31x 31x 2x 2x 31x 1x 1x 1181x 5x 1181x 1176x 1176x 1181x 1x 1x 283x 283x 283x 283x 283x 283x 283x 283x 283x 283x 283x 129x 129x 85x 129x 3x 3x 3x 3x 3x 44x 8x 8x 8x 8x 129x 283x 283x 1x 1x 291x 291x 291x 291x 291x 291x 291x 291x 291x 291x 291x   291x 105x 105x 186x 291x 135x 135x 2x     2x 2x 2x 2x 2x 135x     133x 133x 51x 51x 51x 1x 1x
import {ExpressionNode, TokenNode} from "../../nodes";
import {CurrentScope} from "../_current_scope";
import {AbstractType} from "../../types/basic/_abstract_type";
import * as Expressions from "../../2_statements/expressions";
import {MethodCallChain} from "./method_call_chain";
import {UnknownType} from "../../types/basic/unknown_type";
import {FieldChain} from "./field_chain";
import {VoidType, StringType, CharacterType, DataReference, ObjectReferenceType, FloatType, IntegerType, XSequenceType, XStringType, HexType, XGenericType} from "../../types/basic";
import {Constant} from "./constant";
import {BasicTypes} from "../basic_types";
import {ComponentChain} from "./component_chain";
import {StringTemplate} from "./string_template";
import {ValueBody} from "./value_body";
import {Cond} from "./cond";
import {ReduceBody} from "./reduce_body";
import {ReferenceType} from "../_reference";
import {SwitchBody} from "./switch_body";
import {CondBody} from "./cond_body";
import {ConvBody} from "./conv_body";
import {FilterBody} from "./filter_body";
import {CorrespondingBody} from "./corresponding_body";
import {BuiltIn} from "../_builtin";
import {AttributeChain} from "./attribute_chain";
import {Dereference} from "./dereference";
import {TypedIdentifier} from "../../types/_typed_identifier";
import {TypeUtils} from "../_type_utils";
 
/*
* Type interference, valid scenarios:
* typed = VALUE #( ... ).         right hand side must follow left hand type
* DATA(bar) = VALUE type( ... ).  left gets the type of rigthand
* typed = VALUE type( ... ).      types must match and be compatible???
************* ERRORS *********
* VALUE #( ... ).                 syntax error
* DATA(bar) = VALUE #( ... ).     give error, no type can be derived
*/
 
export class Source {
  public runSyntax(
    node: ExpressionNode | undefined,
    scope: CurrentScope,
    filename: string,
    targetType?: AbstractType,
    writeReference = false): AbstractType | undefined {
 
    if (node === undefined) {
      return undefined;
    }
 
    const children = node.getChildren().slice();
    let first = children.shift();
 
    if (first instanceof TokenNode) {
      const token = first.getFirstToken();
      const tok = token.getStr().toUpperCase();
      switch (tok) {
        case "(":
        case "-":
        case "+":
        case "BIT":
          break;
        case "BOOLC":
        {
          const method = new BuiltIn().searchBuiltin(tok);
          scope.addReference(token, method, ReferenceType.BuiltinMethodReference, filename);
          new Cond().runSyntax(node.findDirectExpression(Expressions.Cond), scope, filename);
          return StringType.get();
        }
        case "XSDBOOL":
        {
          const method = new BuiltIn().searchBuiltin(tok);
          scope.addReference(token, method, ReferenceType.BuiltinMethodReference, filename);
          new Cond().runSyntax(node.findDirectExpression(Expressions.Cond), scope, filename);
          return new CharacterType(1, {qualifiedName: "ABAP_BOOL", ddicName: "ABAP_BOOL"});
        }
        case "REDUCE":
        {
          const foundType = this.determineType(node, scope, filename, targetType);
          const bodyType = new ReduceBody().runSyntax(node.findDirectExpression(Expressions.ReduceBody), scope, filename, foundType);
          if (foundType === undefined || foundType.isGeneric()) {
            this.addIfInferred(node, scope, filename, bodyType);
          } else {
            this.addIfInferred(node, scope, filename, foundType);
          }
          return foundType ? foundType : bodyType;
        }
        case "SWITCH":
        {
          const foundType = this.determineType(node, scope, filename, targetType);
          const bodyType = new SwitchBody().runSyntax(node.findDirectExpression(Expressions.SwitchBody), scope, filename);
          if (foundType === undefined || foundType.isGeneric()) {
            this.addIfInferred(node, scope, filename, bodyType);
          } else {
            this.addIfInferred(node, scope, filename, foundType);
          }
          return foundType ? foundType : bodyType;
        }
        case "COND":
        {
          const foundType = this.determineType(node, scope, filename, targetType);
          const bodyType = new CondBody().runSyntax(node.findDirectExpression(Expressions.CondBody), scope, filename);
          if (foundType === undefined || foundType.isGeneric()) {
            this.addIfInferred(node, scope, filename, bodyType);
          } else {
            this.addIfInferred(node, scope, filename, foundType);
          }
          children.shift();
          children.shift();
          children.shift();
          children.shift();
          this.traverseRemainingChildren(children, scope, filename);
          return foundType ? foundType : bodyType;
        }
        case "CONV":
        {
          const foundType = this.determineType(node, scope, filename, targetType);
          const bodyType = new ConvBody().runSyntax(node.findDirectExpression(Expressions.ConvBody), scope, filename);
          if (new TypeUtils(scope).isAssignable(foundType, bodyType) === false) {
            throw new Error("CONV: Types not compatible");
          }
          this.addIfInferred(node, scope, filename, foundType);
          return foundType;
        }
        case "REF":
        {
          const foundType = this.determineType(node, scope, filename, targetType);
          const s = new Source().runSyntax(node.findDirectExpression(Expressions.Source), scope, filename);
          if (foundType === undefined && s) {
            return new DataReference(s);
          } else {
            return foundType;
          }
        }
        case "FILTER":
        {
          const foundType = this.determineType(node, scope, filename, targetType);
          const bodyType = new FilterBody().runSyntax(node.findDirectExpression(Expressions.FilterBody), scope, filename, foundType);
          if (foundType === undefined || foundType.isGeneric()) {
            this.addIfInferred(node, scope, filename, bodyType);
          } else {
            this.addIfInferred(node, scope, filename, foundType);
          }
 
          if (foundType && !(foundType instanceof UnknownType)) {
            return foundType;
          } else {
            return bodyType;
          }
        }
        case "CORRESPONDING":
        {
          const foundType = this.determineType(node, scope, filename, targetType);
          new CorrespondingBody().runSyntax(node.findDirectExpression(Expressions.CorrespondingBody), scope, filename, foundType);
          this.addIfInferred(node, scope, filename, foundType);
          return foundType;
        }
        case "EXACT":
          return this.determineType(node, scope, filename, targetType);
        case "VALUE":
        {
          const foundType = this.determineType(node, scope, filename, targetType);
          const bodyType = new ValueBody().runSyntax(node.findDirectExpression(Expressions.ValueBody), scope, filename, foundType);
          if (foundType === undefined || foundType.isGeneric()) {
            this.addIfInferred(node, scope, filename, bodyType);
          } else {
            this.addIfInferred(node, scope, filename, foundType);
          }
          return foundType ? foundType : bodyType;
        }
        default:
          return new UnknownType("todo, Source type " + tok);
      }
    } else if (first === undefined || !(first instanceof ExpressionNode)) {
      return undefined;
    }
 
    let context: AbstractType | undefined = new UnknownType("todo, Source type");
 
    const type = [ReferenceType.DataReadReference];
    if (writeReference) {
      type.push(ReferenceType.DataWriteReference);
    }
 
    let hexExpected = false;
    let hexNext = false;
    while (children.length >= 0) {
      if (first instanceof ExpressionNode && first.get() instanceof Expressions.MethodCallChain) {
        context = new MethodCallChain().runSyntax(first, scope, filename, targetType);
        if (context === undefined) {
          throw new Error("Method has no RETURNING value");
        }
      } else if (first instanceof ExpressionNode && first.get() instanceof Expressions.FieldChain) {
        context = new FieldChain().runSyntax(first, scope, filename, type);
      } else if (first instanceof ExpressionNode && first.get() instanceof Expressions.StringTemplate) {
        context = new StringTemplate().runSyntax(first, scope, filename);
      } else if (first instanceof ExpressionNode && first.get() instanceof Expressions.Source) {
        const found = new Source().runSyntax(first, scope, filename);
        context = this.infer(context, found);
      } else if (first instanceof ExpressionNode && first.get() instanceof Expressions.Constant) {
        const found = new Constant().runSyntax(first);
        context = this.infer(context, found);
      } else if (first instanceof ExpressionNode && first.get() instanceof Expressions.Dereference) {
        context = new Dereference().runSyntax(context);
      } else if (first instanceof ExpressionNode && first.get() instanceof Expressions.ComponentChain) {
        context = new ComponentChain().runSyntax(context, first, scope, filename);
      } else if (first instanceof ExpressionNode && first.get() instanceof Expressions.ArithOperator) {
        if (first.concatTokens() === "**") {
          context = new FloatType();
        }
        const operator = first.concatTokens().toUpperCase();
        if (operator === "BIT-OR" || operator === "BIT-AND" || operator === "BIT-XOR") {
          hexExpected = true;
          hexNext = true;
        }
      } else if (first instanceof ExpressionNode && first.get() instanceof Expressions.AttributeChain) {
        context = new AttributeChain().runSyntax(context, first, scope, filename, type);
      }
 
      if (hexExpected === true) {
        if (!(context instanceof VoidType)
            && !(context instanceof XStringType)
            && !(context instanceof HexType)
            && !(context instanceof XGenericType)
            && !(context instanceof XSequenceType)
            && !(context instanceof UnknownType)) {
          throw new Error("Operator only valid for XSTRING or HEX");
        }
        if (hexNext === false) {
          hexExpected = false;
        }
        hexNext = false;
      }
 
      first = children.shift();
      if (first === undefined) {
        break;
      }
    }
 
    if (node.findDirectTokenByText("&&")) {
      return StringType.get();
    }
 
    return context;
  }
 
////////////////////////////////
 
  private traverseRemainingChildren(children: (ExpressionNode | TokenNode)[], scope: CurrentScope, filename: string) {
    const last = children[children.length - 1];
    if (last && last.get() instanceof Expressions.Source) {
      new Source().runSyntax(last as ExpressionNode, scope, filename);
    }
  }
 
  private infer(context: AbstractType | undefined, found: AbstractType | undefined) {
    if (context instanceof FloatType && found instanceof IntegerType) {
      return context;
    } else {
      return found;
    }
  }
 
  public addIfInferred(
    node: ExpressionNode,
    scope: CurrentScope,
    filename: string,
    inferredType: AbstractType | undefined): void {
 
    const basic = new BasicTypes(filename, scope);
    const typeExpression = node.findFirstExpression(Expressions.TypeNameOrInfer);
    const typeToken = typeExpression?.getFirstToken();
    const typeName = typeToken?.getStr();
 
    if (typeName === "#" && inferredType && typeToken) {
      const found = basic.lookupQualifiedName(inferredType.getQualifiedName());
      if (found) {
        scope.addReference(typeToken, found, ReferenceType.InferredType, filename);
      } else if (inferredType instanceof ObjectReferenceType) {
        const def = scope.findObjectDefinition(inferredType.getQualifiedName());
        if (def) {
          const tid = new TypedIdentifier(typeToken, filename, inferredType);
          scope.addReference(typeToken, tid, ReferenceType.InferredType, filename);
        }
      } else if (inferredType instanceof CharacterType) {
        // character is bit special it does not have a qualified name eg "TYPE c LENGTH 6"
        const tid = new TypedIdentifier(typeToken, filename, inferredType);
        scope.addReference(typeToken, tid, ReferenceType.InferredType, filename);
      }
    }
 
  }
 
  private determineType(
    node: ExpressionNode,
    scope: CurrentScope,
    filename: string,
    targetType: AbstractType | undefined): AbstractType | undefined {
 
    const basic = new BasicTypes(filename, scope);
    const typeExpression = node.findFirstExpression(Expressions.TypeNameOrInfer);
    const typeToken = typeExpression?.getFirstToken();
    const typeName = typeToken?.getStr();
 
    if (typeExpression === undefined) {
      throw new Error("determineType, child TypeNameOrInfer not found");
    } else if (typeName === "#" && targetType) {
      return targetType;
    }
 
    if (typeName !== "#" && typeToken) {
      const found = basic.parseType(typeExpression);
      if (found && found instanceof UnknownType) {
        if (scope.getDDIC().inErrorNamespace(typeName) === false) {
          scope.addReference(typeToken, undefined, ReferenceType.VoidType, filename);
          return new VoidType(typeName);
        } else {
          const tid = new TypedIdentifier(typeToken, filename, found);
          scope.addReference(typeToken, tid, ReferenceType.TypeReference, filename);
          return found;
        }
      } else if (found === undefined) {
        throw new Error("Type \"" + typeName + "\" not found in scope, VALUE");
      }
      return found;
    }
 
    return targetType;
  }
 
}