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

89.32% Statements 226/253
76.19% Branches 64/84
100% Functions 7/7
89.32% Lines 226/253

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 2541x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2944x 2944x 2944x 1x 1x 2440x 2440x 2430x 2430x 2430x 2430x 2430x 2430x 186x 186x 2430x 2440x 2440x 2440x 10x 10x 2430x 2430x 2440x 45x 45x 34x 34x 45x 2430x 1x 1x 92x 92x 92x 92x     92x 92x 92x 92x 48x 48x 26x 26x 48x 66x 66x 92x 40x 40x 26x 26x 26x 26x 26x 26x 4x 4x 26x 26x 22x 22x 22x 1x 1x 14x 12x 10x 10x 2x 2x 2x 12x     2x 12x 12x 12x     2x 2x 14x 1x 1x 12x     12x 12x 12x 12x 12x 12x 12x     12x 2x 2x 10x 10x 10x 1x 1x 12x 12x 12x 12x 12x 13x 13x   13x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 12x 12x 13x 13x 1x   1x 1x 1x 1x 13x 13x 1x 1x 1x           1x 1x 1x 1x 1x 1x 1x 1x 1x 13x               13x 13x 10x 10x 8x 8x 10x 2x 2x 1x 1x 2x 10x 1x 1x 1x 1x 1x 10x 13x 13x     13x 13x 1x 1x 1x 13x 12x 12x 12x 2x 2x 2x 2x 12x 10x 10x 12x 12x 13x 12x 1x 1x 12x 12x 7x 12x 4x 5x   1x 1x 1x 12x 7x 7x 12x 12x 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, FunctionModuleDefinition, FunctionModuleParameterDirection, IFunctionModuleParameter} from "../types";
import {CurrentScope} from "./_current_scope";
import {ScopeType} from "./_scope_type";
import {FunctionGroup} from "../../objects";
import {IRegistry} from "../../_iregistry";
import {IdentifierMeta, TypedIdentifier} from "../types/_typed_identifier";
import {TableType, UnknownType, AnyType, VoidType, StructureType, TableKeyType} from "../types/basic";
import {DDIC} from "../../ddic";
import {AbstractType} from "../types/basic/_abstract_type";
import {ABAPFile} from "../abap_file";
import {ObjectOriented} from "./_object_oriented";
import {ReferenceType} from "./_reference";
import {AbstractToken} from "../1_lexer/tokens/abstract_token";
import {Identifier as IdentifierToken} from "../1_lexer/tokens";
 
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 input = {
        scope: CurrentScope.buildDefault(this.reg, obj),
        filename: file.getFilename(),
        issues: [],
      };
      for (const found of structure.findAllStructures(Structures.Form)) {
        this.scope.addFormDefinitions([new FormDefinition(found, input)]);
      }
    }
 
    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 findFunctionGroupScope(fg: FunctionGroup) {
    for (const module of fg.getModules()) {
      if (module.isGlobalParameters() === false) {
        continue;
      }
      // console.dir(fg.getSequencedFiles());
 
      const fmFile = fg.getSequencedFiles().find((f) => { return f.getFilename().endsWith(".fugr." + module.getName().toLowerCase().replace(/\//g, "#") + ".abap"); });
      if (fmFile === undefined) {
        continue;
      }
 
      const nameToken = fmFile.getStructure()?.findFirstStatement(Statements.FunctionModule
      )?.findFirstExpression(Expressions.Field)?.getFirstToken();
      if (nameToken === undefined) {
        continue;
      }
      this.addFunctionScope(module, nameToken, fmFile.getFilename(), true);
    }
  }
 
  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");
    }
    if (definition.isGlobalParameters() === true) {
      return; // already added at global level
    }
 
    this.addFunctionScope(definition, nameToken, filename);
  }
 
  private addFunctionScope(definition: FunctionModuleDefinition, nameToken: AbstractToken, filename: string,
                           ignoreIfAlreadyExists = false) {
    const ddic = new DDIC(this.reg);
 
    const allNames = new Set<string>();
    for (const param of definition.getParameters()) {
      let found: AbstractType | undefined = undefined;
      if (param.type === undefined || param.type === "") {
        found = AnyType.get();
      } else if (param.type.includes("=>")) {
        // then its a type from global INTF or CLAS
        const [clas, name] = param.type.split("=>");
        const def = this.scope.findObjectDefinition(clas);
        if (def) {
          const type = def.getTypeDefinitions().getByName(name);
          if (type) {
            this.scope.addReference(nameToken, type, ReferenceType.TypeReference, filename);
            found = type.getType();
          }
        }
      }
      if (found === undefined) {
        found = ddic.lookup(param.type!).type;
      }
 
      if (param.direction === FunctionModuleParameterDirection.tables) {
        if (found instanceof TableType) {
          found = new TableType(found.getRowType(), {withHeader: true, keyType: TableKeyType.default});
        } else {
          found = new TableType(found, {withHeader: true, keyType: TableKeyType.default});
        }
      }
 
      if ((found instanceof UnknownType || found instanceof VoidType) && 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 === undefined || found instanceof UnknownType || found instanceof VoidType) {
          const f = this.scope.findType(name)?.getType();
          if (f && f instanceof StructureType) {
            const c = f.getComponentByName(field);
            if (c) {
              found = c;
            }
          }
        }
      } else if ((found instanceof UnknownType || found instanceof VoidType) && param.type?.includes("=>")) {
        const [name, field] = param.type.split("=>");
        const def = this.scope.findObjectDefinition(name);
        const c = new ObjectOriented(this.scope).searchTypeName(def, field);
        if (c) {
          found = c.getType();
        }
      }
 
      if ((found instanceof UnknownType || found instanceof VoidType) && param.type) {
        const f = ddic.lookupBuiltinType(param.type);
        if (f) {
          found = f;
        }
        if (found === undefined || found instanceof UnknownType || found instanceof VoidType) {
          const f = this.scope.findType(param.type)?.getType();
          if (f) {
            found = f;
          }
        }
        if (found === undefined || found instanceof UnknownType || found instanceof VoidType) {
          const f = this.scope.findTypePoolType(param.type)?.getType();
          if (f) {
            found = f;
          }
        }
      }
 
      if (found instanceof UnknownType && new DDIC(this.reg).inErrorNamespace(param.type) === false) {
        found = VoidType.get(param.type);
      }
 
      if (allNames.has(param.name.toUpperCase())) {
        // yea, IMPORTING and EXPORTING can have the same name
        // workaround to avoid false postivies, can be improved
        continue;
      } else {
        const token = new IdentifierToken(nameToken.getStart(), param.name);
        const type = new TypedIdentifier(token, filename, found, this.functionModuleParameterMeta(param));
        if (ignoreIfAlreadyExists === true) {
          const exists = this.scope.findVariable(param.name);
          if (exists === undefined) {
            this.scope.addNamedIdentifier(param.name, type);
          }
        } else {
          this.scope.addNamedIdentifier(param.name, type);
        }
        allNames.add(param.name.toUpperCase());
      }
    }
  }
 
  private functionModuleParameterMeta(param: IFunctionModuleParameter): IdentifierMeta[] {
    const ret: IdentifierMeta[] = [];
    if (param.direction === FunctionModuleParameterDirection.importing) {
      ret.push(IdentifierMeta.FunctionModuleImporting);
    } else if (param.direction === FunctionModuleParameterDirection.exporting) {
      ret.push(IdentifierMeta.FunctionModuleExporting);
    } else if (param.direction === FunctionModuleParameterDirection.changing) {
      ret.push(IdentifierMeta.FunctionModuleChanging);
    } else if (param.direction === FunctionModuleParameterDirection.tables) {
      ret.push(IdentifierMeta.FunctionModuleTables);
    }
    if (param.passByValue) {
      ret.push(IdentifierMeta.PassByValue);
    }
    return ret;
  }
 
}