All files / src/abap/types class_definition.ts

93.84% Statements 183/195
85.71% Branches 36/42
77.77% Functions 14/18
93.84% Lines 183/195

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 1951x 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 904x     904x 904x 904x 904x 904x 904x 904x 904x 904x 904x 904x 904x 904x 904x 904x 904x 904x 904x 904x 904x 904x 904x 904x 904x 904x 904x 5x 5x 845x 845x 845x 845x 845x 845x 845x 845x 845x 845x 904x 904x 1x 1x     1x 1x 428x 428x 1x 1x 907x 907x 1x 1x 1348x 1348x 1x 1x 3545x 3545x 1x 1x 2395x 2395x 1x 1x     1x 1x 2x 2x 1x 1x 2180x 2180x 1x 1x 791x 791x 1x 1x     1x 1x 54x 54x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 903x 903x 903x 903x 903x 1x 1x 903x 903x 1x 1x 1x 1x 1x 903x 903x 1x 1x 904x 904x 233x 233x 175x 233x 42x 42x 233x 904x 1x 1x 903x 73x 73x 73x     73x 73x 73x 73x 73x 50x 73x 2x 23x 21x 21x 73x 903x 903x 903x 1x 1x
import {StatementNode, StructureNode} from "../nodes";
import {MethodDefinitions} from "./method_definitions";
import {SuperClassName} from "../2_statements/expressions";
import * as Statements from "../2_statements/statements";
import * as Structures from "../3_structures/structures";
import * as Expressions from "../2_statements/expressions";
import {Attributes} from "./class_attributes";
import {Identifier} from "../4_file_information/_identifier";
import {Aliases} from "./aliases";
import {CurrentScope} from "../5_syntax/_current_scope";
import {IClassDefinition} from "./_class_definition";
import {TypeDefinitions} from "./type_definitions";
import {ScopeType} from "../5_syntax/_scope_type";
import {EventDefinition} from "./event_definition";
import {Visibility} from "../4_file_information/visibility";
import {IEventDefinition} from "./_event_definition";
import {IMethodDefinitions} from "./_method_definitions";
import {IAliases} from "./_aliases";
import {ObjectOriented} from "../5_syntax/_object_oriented";
import {IImplementing} from "./_interface_definition";
import {ReferenceType} from "../5_syntax/_reference";
import {Token} from "../1_lexer/tokens/_token";
 
export class ClassDefinition extends Identifier implements IClassDefinition {
  private readonly node: StructureNode;
  private readonly methodDefs: MethodDefinitions;
  private readonly types: TypeDefinitions;
  private readonly attributes: Attributes;
  private readonly events: IEventDefinition[];
  private readonly friends: string[];
  private readonly superClass: string | undefined;
  private readonly implementing: IImplementing[];
  private readonly testing: boolean;
  private readonly abstract: boolean;
  private readonly sharedMemory: boolean;
  private aliases: IAliases;
 
  public constructor(node: StructureNode, filename: string, scope: CurrentScope) {
    if (!(node.get() instanceof Structures.ClassDefinition)) {
      throw new Error("ClassDefinition, unexpected node type");
    }
 
    const def = node.findFirstStatement(Statements.ClassDefinition);
    const name = def!.findDirectExpression(Expressions.ClassName)!.getFirstToken();
    super(name, filename);
    scope.addClassDefinition(this);
 
    this.node = node;
    this.events = [];
    this.implementing = [];
 
    scope.push(ScopeType.ClassDefinition, name.getStr(), name.getStart(), filename);
 
    this.superClass = this.findSuper(def, filename, scope);
    this.friends = this.findFriends(def, filename, scope);
 
    this.parse(filename, scope);
 
    const helper = new ObjectOriented(scope);
    helper.fromSuperClassesAndInterfaces(this);
    helper.addAliasedTypes(this.aliases);
 
    this.attributes = new Attributes(this.node, this.filename, scope);
    this.types = this.attributes.getTypes();
 
    const events = this.node.findAllStatements(Statements.Events);
    for (const e of events) {
      this.events.push(new EventDefinition(e, Visibility.Public, this.filename, scope)); // todo, all these are not Public
    }
 
    this.methodDefs = new MethodDefinitions(this.node, this.filename, scope);
 
    scope.pop(node.getLastToken().getEnd());
 
    const cdef = this.node.findFirstStatement(Statements.ClassDefinition);
    const concat = cdef!.concatTokens().toUpperCase();
 
    this.testing = concat.includes(" FOR TESTING");
    this.sharedMemory = concat.includes(" SHARED MEMORY ENABLED");
    this.abstract = cdef?.findDirectTokenByText("ABSTRACT") !== undefined;
  }
 
  public getFriends() {
    return this.friends;
  }
 
  public getEvents() {
    return this.events;
  }
 
  public getMethodDefinitions(): IMethodDefinitions {
    return this.methodDefs;
  }
 
  public getTypeDefinitions(): TypeDefinitions {
    return this.types;
  }
 
  public getSuperClass(): string | undefined {
    return this.superClass;
  }
 
  public getAttributes(): Attributes {
    return this.attributes;
  }
 
  public isGlobal(): boolean {
    return this.node.findFirstExpression(Expressions.ClassGlobal) !== undefined;
  }
 
  public isFinal(): boolean {
    return this.node.findFirstExpression(Expressions.ClassFinal) !== undefined;
  }
 
  public getImplementing(): readonly IImplementing[] {
    return this.implementing;
  }
 
  public getAliases(): IAliases {
    return this.aliases;
  }
 
  public isForTesting(): boolean {
    return this.testing;
  }
 
  public isAbstract(): boolean {
    return this.abstract;
  }
 
  public isSharedMemory(): boolean {
    return this.sharedMemory;
  }
 
/*
  public getEvents() {
  }
*/
 
  ///////////////////
 
  private findSuper(def: StatementNode | undefined, filename: string, scope: CurrentScope): string | undefined {
    const token = def?.findDirectExpression(SuperClassName)?.getFirstToken();
    this.addReference(token, filename, scope);
    const name = token?.getStr();
    return name;
  }
 
  private findFriends(def: StatementNode | undefined, filename: string, scope: CurrentScope): string[] {
    const result: string[] = [];
    for (const n of def?.findDirectExpression(Expressions.ClassFriends)?.findDirectExpressions(Expressions.ClassName) || []) {
      const token = n.getFirstToken();
      this.addReference(token, filename, scope);
      const name = token.getStr();
      result.push(name);
    }
    return result;
  }
 
  private addReference(token: Token | undefined, filename: string, scope: CurrentScope) {
    const name = token?.getStr();
    if (name) {
      const s = scope.findClassDefinition(name);
      if (s) {
        scope.addReference(token, s, ReferenceType.ObjectOrientedReference, filename, {ooName: name.toUpperCase(), ooType: "CLAS"});
      } else if (scope.getDDIC().inErrorNamespace(name) === false) {
        scope.addReference(token, undefined, ReferenceType.ObjectOrientedVoidReference, filename);
      }
    }
  }
 
  private parse(filename: string, scope: CurrentScope) {
    for (const node of this.node.findAllStatements(Statements.InterfaceDef)) {
      const partial = node.concatTokens().toUpperCase().includes(" PARTIALLY IMPLEMENTED");
      const token = node.findFirstExpression(Expressions.InterfaceName)?.getFirstToken();
      if (token === undefined) {
        throw new Error("ClassDefinition, unable to find interface token");
      }
      const name = token.getStr().toUpperCase();
      this.implementing.push({name, partial});
 
      const intf = scope.findInterfaceDefinition(name);
      if (intf) {
        scope.addReference(token, intf, ReferenceType.ObjectOrientedReference, filename, {ooName: name.toUpperCase(), ooType: "INTF"});
      } else if (scope.getDDIC().inErrorNamespace(name) === false) {
        scope.addReference(token, undefined, ReferenceType.ObjectOrientedVoidReference, filename, {ooName: name.toUpperCase(), ooType: "INTF"});
      } else {
        scope.addReference(token, undefined, ReferenceType.ObjectOrientedUnknownReference, filename, {ooName: name.toUpperCase(), ooType: "INTF"});
      }
    }
 
    this.aliases = new Aliases(this.node, this.filename, scope);
  }
 
}