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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | 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 1x 1x 7186x 7186x 7186x 7186x 7186x 7186x 7186x 7186x 7186x 7186x 7186x 7186x 7186x 7186x 7186x 7186x 7186x 7186x 7186x 7186x 7186x 7186x 7186x 7186x 7186x 7186x 7186x 7186x 21x 21x 3x 3x 3x 18x 18x 7073x 7073x 7073x 7073x 7073x 7073x 7073x 7073x 7073x 7186x 7186x 33x 7186x 7x 7040x 7033x 7033x 7073x 7073x 7073x 7073x 7073x 7073x 7073x 7073x 7073x 7073x 7073x 1x 1x 35x 35x 1x 1x 888x 888x 1x 1x 34757x 34757x 1x 1x 13333x 13333x 1x 1x 79254x 79254x 1x 1x 40279x 40279x 1x 1x 15052x 15052x 1x 1x 2x 2x 1x 1x 41214x 41214x 1x 1x 22269x 22269x 1x 1x 22544x 22544x 1x 1x 524x 524x 1x 1x 1x 1x 477x 477x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7073x 1x 1x 1x 7073x 1x 1x 1x 7073x 5903x 5903x 1170x 1170x 763x 763x 763x 1170x 1170x 1170x 1170x 1630x 903x 903x 903x 903x 903x 42x 42x 903x 1630x 1170x 1170x 7073x 1142x 1142x 28x 28x 28x 28x 28x 7073x 26x 26x 7073x 54x 54x 54x 54x 54x 54x 54x 26x 26x 26x 26x 54x 54x 28x 1x 1x 7185x 7185x 7185x 7185x 7185x 1x 1x 7073x 21475x 1x 1x 1x 21475x 7073x 1x 1x 7073x 21475x 21468x 21468x 21475x 1x 21475x 4x 4x 21475x 7073x 1x 1x 7073x 7073x 7073x 7073x 7073x 14146x 1x 1x 14146x 7073x 1x 1x 7073x 7073x 7073x 7073x 7073x 7073x 3218x 3218x 17915x 17915x 3x 17915x 17912x 17912x 2015x 2015x 15897x 15897x 15897x 15897x 17915x 3218x 22x 22x 3218x 3218x 7073x 7073x 21475x 21475x 4x 4x 21475x 21475x 21475x 21475x 2x 2x 21475x 7073x 1x 1x 7073x 21475x 21475x 3x 3x 21475x 7073x 1x 1x 7073x 18x 18x 18x 3x 3x 18x 7073x 1x 1x 7185x 7185x 17x 17x 17x 17x 5x 17x 10x 12x 2x 2x 2x 17x 17x 7185x 7185x 1x 1x 7185x 7185x 2799x 2799x 2690x 2799x 79x 79x 2799x 7185x 1x 1x 7185x 2250x 2250x 2250x 2250x 2250x 2250x 2250x 2250x 149x 2250x 2063x 2101x 38x 38x 2250x 7185x 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 {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 {ObjectOriented} from "../5_syntax/_object_oriented";
import {IImplementing} from "./_interface_definition";
import {IReference, ReferenceType} from "../5_syntax/_reference";
import {AbstractToken} from "../1_lexer/tokens/abstract_token";
import {SyntaxInput, syntaxIssue} from "../5_syntax/_syntax_input";
import {Alias} from "./alias";
import {SpaghettiScopeNode} from "../5_syntax/spaghetti_scope";
export class ClassDefinition extends Identifier implements IClassDefinition {
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 finalValue: boolean;
private readonly globalValue: boolean;
private readonly sharedMemory: boolean;
private readonly aliases: Alias[];
private readonly createVisibilityValue: Visibility;
public constructor(node: StructureNode, input: SyntaxInput) {
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, input.filename);
input.scope.addClassDefinition(this);
this.events = [];
this.implementing = [];
this.globalValue = def!.findFirstExpression(Expressions.ClassGlobal) !== undefined;
this.finalValue = def!.findFirstExpression(Expressions.ClassFinal) !== undefined;
input.scope.push(ScopeType.ClassDefinition, name.getStr(), name.getStart(), input.filename);
this.superClass = this.findSuper(def, input);
this.friends = this.findFriends(def, input);
this.parse(input, node);
const helper = new ObjectOriented(input.scope);
helper.fromSuperClassesAndInterfaces(this);
this.attributes = new Attributes(node, input);
this.types = this.attributes.getTypes();
this.aliases = this.attributes.getAliases();
const events = node.findAllStatements(Statements.Events);
for (const e of events) {
const eventName = e.findDirectExpression(Expressions.EventName)?.concatTokens().toUpperCase();
if (this.events.find(ev => ev.getName().toUpperCase() === eventName) !== undefined) {
input.issues.push(syntaxIssue(input, e.getFirstToken(), "Event " + eventName + " already defined"));
continue;
}
this.events.push(new EventDefinition(e, Visibility.Public, input)); // todo, all these are not Public
}
this.methodDefs = new MethodDefinitions(node, input);
const scope = input.scope.pop(node.getLastToken().getEnd());
const concat = def!.concatTokens().toUpperCase();
this.testing = concat.includes(" FOR TESTING");
this.sharedMemory = concat.includes(" SHARED MEMORY ENABLED");
this.abstract = def?.findDirectTokenByText("ABSTRACT") !== undefined;
if (concat.includes(" CREATE PRIVATE")) {
this.createVisibilityValue = Visibility.Private;
} else if (concat.includes(" CREATE PROTECTED")) {
this.createVisibilityValue = Visibility.Protected;
} else {
this.createVisibilityValue = Visibility.Public;
}
// perform checks after everything has been initialized
this.checkInterfaceVisibility(input, node);
this.checkMethodsFromSuperClasses(input);
this.checkMethodNameConflicts(input);
this.checkEventNameConflicts(input);
this.checkClassNameLength(input);
this.checkMethodNameLength(input);
this.checkClassConstructor(input);
this.checkLocalTypesInPublicSections(input, node, scope.getTop());
}
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.globalValue;
}
public isFinal(): boolean {
return this.finalValue;
}
public getImplementing(): readonly IImplementing[] {
return this.implementing;
}
public getAliases() {
return this.aliases;
}
public isForTesting(): boolean {
return this.testing;
}
public isAbstract(): boolean {
return this.abstract;
}
public isSharedMemory(): boolean {
return this.sharedMemory;
}
public getCreateVisibility(): Visibility {
return this.createVisibilityValue;
}
/*
public getEvents() {
}
*/
///////////////////
private checkClassNameLength(input: SyntaxInput) {
if (this.getName().length > 30) {
const message = `Class name "${this.getName()}" is too long, maximum length is 30 characters`;
input.issues.push(syntaxIssue(input, this.getToken(), message));
}
}
// the local types, classes and interfaces of a class pool can only be used in the PRIVATE SECTION of the global class
private checkLocalTypesInPublicSections(input: SyntaxInput, node: StructureNode, scope: SpaghettiScopeNode) {
if (this.globalValue === false || input.filename.toLowerCase().endsWith(".clas.abap") === false) {
return;
}
const prefix = input.filename.toLowerCase().replace(/abap$/, "");
const isLocal = (filename: string) => {
const lower = filename.toLowerCase();
return lower.startsWith(prefix)
&& (lower.endsWith(".clas.locals_def.abap") || lower.endsWith(".clas.locals_imp.abap"));
};
const references: IReference[] = [];
const collect = (n: SpaghettiScopeNode) => {
for (const r of n.getData().references) {
if ((r.referenceType === ReferenceType.TypeReference || r.referenceType === ReferenceType.ObjectOrientedReference)
&& r.position.getFilename() === input.filename
&& r.position.getName().includes("~") === false // lif~meth of an ALIASES, the INTERFACES statement is reported
&& r.resolved !== undefined
&& isLocal(r.resolved.getFilename())) {
references.push(r);
}
}
n.getChildren().forEach(collect);
};
collect(scope);
if (references.length === 0) {
return;
}
const sections = [
{section: node.findDirectStructure(Structures.PublicSection), name: "PUBLIC"},
];
// a FINAL class has no subclasses, so the PROTECTED SECTION is effectively private
if (this.finalValue === false) {
sections.push({section: node.findDirectStructure(Structures.ProtectedSection), name: "PROTECTED"});
}
for (const {section, name} of sections) {
for (const statement of section?.findAllStatementNodes() || []) {
// compare with the tokens, the statements of a chain share the first tokens
const tokens = statement.getTokens();
const found = references
.filter(r => tokens.some(t => t.getStart().equals(r.position.getStart())))
.sort((a, b) => a.position.getStart().isBefore(b.position.getStart()) ? -1 : 1)[0];
if (found !== undefined) {
const message = "\"" + found.position.getName().toUpperCase() +
"\" is local to the class pool and cannot be used in the " + name + " SECTION of the global class";
input.issues.push(syntaxIssue(input, found.position.getToken(), message));
}
}
}
}
private findSuper(def: StatementNode | undefined, input: SyntaxInput): string | undefined {
const token = def?.findDirectExpression(SuperClassName)?.getFirstToken();
this.addReference(token, input);
const name = token?.getStr();
return name;
}
private checkMethodNameLength(input: SyntaxInput) {
for (const m of this.methodDefs.getAll()) {
if (m.getName().length > 30 && m.getName().includes("~") === false) {
const message = `Method name "${m.getName()}" is too long, maximum length is 30 characters`;
input.issues.push(syntaxIssue(input, m.getToken(), message));
}
}
}
private checkClassConstructor(input: SyntaxInput) {
for (const m of this.methodDefs.getAll()) {
if (m.getName().toUpperCase() !== "CLASS_CONSTRUCTOR") {
continue;
}
if (m.isStatic() === false) {
input.issues.push(syntaxIssue(input, m.getToken(), "CLASS_CONSTRUCTOR must be static"));
} else if (m.getVisibility() !== Visibility.Public) {
input.issues.push(syntaxIssue(input, m.getToken(), "CLASS_CONSTRUCTOR must be declared in the public section"));
}
}
}
private checkInterfaceVisibility(input: SyntaxInput, node: StructureNode) {
const sections = [
node.findDirectStructure(Structures.ProtectedSection),
node.findDirectStructure(Structures.PrivateSection),
];
for (const section of sections) {
for (const statement of section?.findAllStatements(Statements.InterfaceDef) || []) {
input.issues.push(syntaxIssue(input, statement.getFirstToken(), "INTERFACES can only be declared in public sections"));
}
}
}
private checkMethodsFromSuperClasses(input: SyntaxInput) {
const scope = input.scope;
let sup = this.getSuperClass();
const names: Set<string> = new Set();
const methods: Map<string, Visibility> = new Map();
while (sup !== undefined) {
const cdef = scope.findClassDefinition(sup);
for (const m of cdef?.getMethodDefinitions()?.getAll() || []) {
const name = m.getName().toUpperCase();
if (m.getVisibility() === Visibility.Private) {
continue;
} else if (name === "CONSTRUCTOR"
|| name === "DESTRUCTOR"
|| name === "CLASS_CONSTRUCTOR") {
continue;
}
names.add(name);
if (methods.has(name) === false) {
methods.set(name, m.getVisibility());
}
}
for (const a of cdef?.getAliases() || []) {
names.add(a.getName().toUpperCase());
}
sup = cdef?.getSuperClass();
}
for (const m of this.getMethodDefinitions().getAll()) {
if (names.has(m.getName().toUpperCase())
&& m.isRedefinition() === false) {
input.issues.push(syntaxIssue(input, m.getToken(), `${m.getName().toUpperCase()} already declared in superclass`));
}
const superVisibility = methods.get(m.getName().toUpperCase());
if (m.isRedefinition() === true
&& superVisibility !== undefined
&& m.getVisibility() !== superVisibility) {
input.issues.push(syntaxIssue(input, m.getToken(), `${m.getName().toUpperCase()} redefinition visibility cannot be changed`));
}
}
}
private checkMethodNameConflicts(input: SyntaxInput) {
for (const m of this.methodDefs.getAll()) {
if (this.attributes.findByName(m.getName()) !== undefined
|| this.types.getByName(m.getName()) !== undefined) {
input.issues.push(syntaxIssue(input, m.getToken(), `"${m.getName()}" already defined`));
}
}
}
private checkEventNameConflicts(input: SyntaxInput) {
for (const e of this.events) {
if (this.attributes.findByName(e.getName()) !== undefined
|| this.types.getByName(e.getName()) !== undefined
|| this.methodDefs.getByName(e.getName()) !== undefined) {
input.issues.push(syntaxIssue(input, (e as EventDefinition).getToken(), `"${e.getName()}" already defined`));
}
}
}
private findFriends(def: StatementNode | undefined, input: SyntaxInput): string[] {
const result: string[] = [];
for (const n of def?.findDirectExpression(Expressions.ClassFriends)?.findDirectExpressions(Expressions.ClassName) || []) {
const token = n.getFirstToken();
const name = token.getStr();
const s = input.scope.findClassDefinition(name);
if (s) {
input.scope.addReference(token, s, ReferenceType.ObjectOrientedReference, input.filename, {ooName: name.toUpperCase(), ooType: "CLAS"});
} else if (input.scope.existsObject(name) !== undefined) {
// DEFINITION DEFERRED friend
} else if (input.scope.getDDIC().inErrorNamespace(name) === false) {
input.scope.addReference(token, undefined, ReferenceType.ObjectOrientedVoidReference, input.filename);
} else {
input.issues.push(syntaxIssue(input, token, "Friend class " + name.toUpperCase() + " does not exist"));
}
result.push(name);
}
return result;
}
private addReference(token: AbstractToken | undefined, input: SyntaxInput) {
const name = token?.getStr();
if (name) {
const s = input.scope.findClassDefinition(name);
if (s) {
input.scope.addReference(token, s, ReferenceType.ObjectOrientedReference, input.filename, {ooName: name.toUpperCase(), ooType: "CLAS"});
} else if (input.scope.getDDIC().inErrorNamespace(name) === false) {
input.scope.addReference(token, undefined, ReferenceType.ObjectOrientedVoidReference, input.filename);
}
}
}
private parse(input: SyntaxInput, inputNode: StructureNode) {
for (const node of inputNode.findAllStatements(Statements.InterfaceDef)) {
const partial = node.findDirectTokenByText("PARTIALLY") !== undefined;
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 = input.scope.findInterfaceDefinition(name);
if (intf) {
input.scope.addReference(token, intf, ReferenceType.ObjectOrientedReference, input.filename, {ooName: name.toUpperCase(), ooType: "INTF"});
} else if (input.scope.getDDIC().inErrorNamespace(name) === false) {
input.scope.addReference(token, undefined, ReferenceType.ObjectOrientedVoidReference, input.filename, {ooName: name.toUpperCase(), ooType: "INTF"});
} else {
input.scope.addReference(token, undefined, ReferenceType.ObjectOrientedUnknownReference, input.filename, {ooName: name.toUpperCase(), ooType: "INTF"});
}
}
}
}
|