All files / src/abap/5_syntax _procedural.ts

83.21% Statements 114/137
65.51% Branches 19/29
100% Functions 4/4
83.21% Lines 114/137

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 1371x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4440x 4440x 4440x 1x 1x 1394x 1394x 1382x 1382x 159x 159x 1382x 1394x 1394x 1394x 12x 12x 1382x 1382x 1394x 23x 23x 18x 18x 23x 1382x 1x 1x 48x 48x 48x 48x     48x 48x 48x 48x 26x 26x 16x 16x 26x 32x 32x 48x 18x 18x 14x 14x 14x 14x 14x 14x 4x 4x 14x 14x 10x 10x 10x 1x 1x 3x     3x 3x 3x 3x 3x 3x 3x     3x 3x 3x 3x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x                   1x 1x           1x     1x 1x 1x 3x 1x 1x
import * as Expressions from "../2_statements/expressions";
import * as Statements from "../2_statements/statements";
import * as Structures from "../3_structures/structures";
import {StatementNode} from "../nodes";
import {ABAPObject} from "../../objects/_abap_object";
import {FormDefinition, FunctionModuleParameterDirection} from "../types";
import {CurrentScope} from "./_current_scope";
import {ScopeType} from "./_scope_type";
import {FunctionGroup} from "../../objects";
import {IRegistry} from "../../_iregistry";
import {TypedIdentifier} from "../types/_typed_identifier";
import {TableType, UnknownType, AnyType, VoidType, StructureType} from "../types/basic";
import {DDIC} from "../../ddic";
import {AbstractType} from "../types/basic/_abstract_type";
import {ABAPFile} from "../abap_file";
 
export class Procedural {
  private readonly scope: CurrentScope;
  private readonly reg: IRegistry;
 
  public constructor(reg: IRegistry, scope: CurrentScope) {
    this.scope = scope;
    this.reg = reg;
  }
 
  public addAllFormDefinitions(file: ABAPFile, obj: ABAPObject) {
    const structure = file.getStructure();
    if (structure) {
      const dummy = CurrentScope.buildDefault(this.reg, obj);
      for (const found of structure.findAllStructures(Structures.Form)) {
        this.scope.addFormDefinitions([new FormDefinition(found, file.getFilename(), dummy)]);
      }
    }
 
    const stru = file.getStructure();
    if (stru === undefined) {
      return;
    }
 
    const includes = stru.findAllStatements(Statements.Include);
    for (const node of includes) {
      const found = this.findInclude(node, obj);
      if (found) {
        this.addAllFormDefinitions(found, obj);
      }
    }
  }
 
  public findInclude(node: StatementNode, obj: ABAPObject): ABAPFile | undefined {
// assumption: no cyclic includes, includes not found are reported by rule "check_include"
// todo: how to make sure code is not duplicated here and in rule "check_include" / include graph?
    const expr = node.findFirstExpression(Expressions.IncludeName);
    if (expr === undefined) {
      return undefined;
    }
    const name = expr.getFirstToken().getStr();
 
    // look in the current function group
    if (obj instanceof FunctionGroup) {
      const incl = obj.getInclude(name);
      if (incl !== undefined) {
        return incl;
      }
    }
 
    const prog = this.reg.getObject("PROG", name) as ABAPObject | undefined;
    if (prog !== undefined) {
      return prog.getABAPFiles()[0];
    }
 
    // todo, this is slow, try determining the FUGR name from the include name
    for (const fugr of this.reg.getObjectsByType("FUGR")) {
      if (fugr instanceof FunctionGroup) {
        const found = fugr.getInclude(name);
        if (found) {
          return found;
        }
      }
    }
 
    return undefined;
  }
 
  public findFunctionScope(obj: ABAPObject, node: StatementNode, filename: string) {
    if (!(obj instanceof FunctionGroup)) {
      throw new Error("findFunctionScope, expected function group input");
    }
 
    const nameToken = node.findFirstExpression(Expressions.Field)!.getFirstToken();
    const name = nameToken.getStr();
    this.scope.push(ScopeType.FunctionModule, name, node.getFirstToken().getStart(), filename);
 
    const definition = obj.getModule(name);
    if (definition === undefined) {
      throw new Error("Function module definition \"" + name + "\" not found");
    }
 
    const ddic = new DDIC(this.reg);
 
    for (const param of definition.getParameters()) {
      let found: AbstractType | undefined = undefined;
      if (param.type === undefined || param.type === "") {
        found = new AnyType();
      } else {
        found = ddic.lookup(param.type).type;
      }
      if (param.direction === FunctionModuleParameterDirection.tables
          && !(found instanceof TableType)) {
        found = new TableType(found, {withHeader: true});
      }
 
      if (found instanceof UnknownType && param.type?.includes("-")) {
        const [name, field] = param.type.split("-");
        const f = ddic.lookupTableOrView(name).type;
        if (f && f instanceof StructureType) {
          const c = f.getComponentByName(field);
          if (c) {
            found = c;
          }
        }
      }
 
      if (found instanceof UnknownType && param.type) {
        const f = ddic.lookupBuiltinType(param.type);
        if (f) {
          found = f;
        }
      }
      if (found instanceof UnknownType && new DDIC(this.reg).inErrorNamespace(param.type) === false) {
        found = new VoidType(param.type);
      }
      const type = new TypedIdentifier(nameToken, filename, found);
      this.scope.addNamedIdentifier(param.name, type);
    }
  }
 
}