9999import dev .askov .mjcompiler .ast .VoidFormPars ;
100100import dev .askov .mjcompiler .ast .VoidReturnType ;
101101import dev .askov .mjcompiler .ast .VoidSuperclass ;
102- import dev .askov .mjcompiler .exceptions .WrongObjKindException ;
103- import dev .askov .mjcompiler .exceptions .WrongStructKindException ;
104102import dev .askov .mjcompiler .inheritancetree .InheritanceTree ;
105103import dev .askov .mjcompiler .loggers .SemanticErrorMJLogger ;
106104import dev .askov .mjcompiler .loggers .SemanticErrorMJLogger .SemanticErrorKind ;
111109import dev .askov .mjcompiler .methodsignature .MethodSignatureGenerator ;
112110import dev .askov .mjcompiler .mjsymboltable .MJTab ;
113111import dev .askov .mjcompiler .util .MJUtils ;
112+ import java .util .Optional ;
114113import java .util .Stack ;
115114import rs .etf .pp1 .symboltable .Tab ;
116115import rs .etf .pp1 .symboltable .concepts .Obj ;
@@ -244,13 +243,8 @@ private Obj findNearestDeclaration(MethodSignature classMethodSignature, Obj cls
244243 while (currentClass != null ) {
245244 var method = currentClass .getMembersTable ().searchKey (classMethodSignature .getMethodName ());
246245 if (method != null && method != Tab .noObj && method .getKind () == Obj .Meth ) {
247- try {
248- if (new ClassMethodSignature (method , MJTab .noType )
249- .isInvokableBy (classMethodSignature )) {
250- return method ;
251- }
252- } catch (WrongObjKindException e ) {
253- e .printStackTrace ();
246+ if (new ClassMethodSignature (method , MJTab .noType ).isInvokableBy (classMethodSignature )) {
247+ return method ;
254248 }
255249 }
256250 currentClass = currentClass .getElemType ();
@@ -265,17 +259,13 @@ private void validateOverriding(MethodDecl methodDecl) {
265259 while (clss != null ) {
266260 var overriddenMethod =
267261 clss .getMembersTable ().searchKey (methodDecl .getMethodName ().obj .getName ());
268- try {
269- if (MJUtils .haveSameSignatures (overridingMethod , overriddenMethod )
270- && !MJUtils .returnTypesAssignmentCompatible (overridingMethod , overriddenMethod )) {
271- detectSemanticError (
272- null ,
273- methodDecl ,
274- SemanticErrorKind .INCOMPATIBLE_RET_TYPE ,
275- new ClassMethodSignature (overriddenMethod , clss ));
276- }
277- } catch (WrongObjKindException e ) {
278- e .printStackTrace ();
262+ if (MJUtils .haveSameSignatures (overridingMethod , overriddenMethod )
263+ && !MJUtils .returnTypesAssignmentCompatible (overridingMethod , overriddenMethod )) {
264+ detectSemanticError (
265+ null ,
266+ methodDecl ,
267+ SemanticErrorKind .INCOMPATIBLE_RET_TYPE ,
268+ new ClassMethodSignature (overriddenMethod , clss ));
279269 }
280270 clss = clss .getElemType ();
281271 }
@@ -529,11 +519,7 @@ public void visit(NonVoidSuperclass nonVoidSuperclass) {
529519 if (superclassType != MJTab .noType ) {
530520 if (superclassType .getKind () == Struct .Class && superclassType != currentClassObj .getType ()) {
531521 var superclassObj = nonVoidSuperclass .getType ().obj ;
532- try {
533- InheritanceTree .addNodeForClass (currentClassObj , superclassObj );
534- } catch (WrongObjKindException | WrongStructKindException e ) {
535- e .printStackTrace ();
536- }
522+ InheritanceTree .addNodeForClass (currentClassObj , superclassObj );
537523 currentClassObj .setAdr (superclassObj .getAdr ());
538524 currentClassObj .getType ().setElementType (superclassType );
539525 } else {
@@ -549,11 +535,7 @@ public void visit(VoidSuperclass voidSuperclass) {
549535 MJTab .insert (Obj .Fld , VMT_POINTER , MJTab .intType );
550536 MJTab .insert (Obj .Fld , CLASS_ID , MJTab .intType );
551537 currentClassObj .setAdr (1 );
552- try {
553- InheritanceTree .addNodeForClass (currentClassObj );
554- } catch (WrongObjKindException | WrongStructKindException e ) {
555- e .printStackTrace ();
556- }
538+ InheritanceTree .addNodeForClass (currentClassObj );
557539 }
558540
559541 @ Override
@@ -808,38 +790,36 @@ public void visit(MethodCallDesignatorStatement methodCallDesignatorStatement) {
808790 }
809791 }
810792 } else {
811- MethodSignature methodSignature = null ;
812- try {
813- if (isGlobalMethod (methodObj )) {
814- methodSignature = new GlobalMethodSignature (methodObj );
815- } else {
816- methodSignature = new ClassMethodSignature (methodObj , thisParameterObjs .peek ().getType ());
817- }
818- } catch (WrongObjKindException ignored ) {
819- }
820- if (methodSignature != null ) {
821- if (!methodSignature .isInvokableBy (invokedMethodSignatureGenerator .getMethodSignature ())) {
822- var overriddenMethodObj =
823- findNearestDeclaration (
824- invokedMethodSignatureGenerator .getMethodSignature (), thisParameterObjs .pop ());
825- if (overriddenMethodObj .equals (Tab .noObj )) {
826- if (invokedMethodSignatureGenerator .getMethodSignature ().allTypesAreKnown ()) {
827- detectSemanticError (
828- null ,
829- methodCallDesignatorStatement ,
830- SemanticErrorKind .INAPPLICABLE_METHOD ,
831- methodSignature .toString (),
832- invokedMethodSignatureGenerator .getMethodSignature ().getParameterList ());
833- } else {
834- detectSemanticError ();
793+ Optional <MethodSignature > methodSignatureOpt =
794+ isGlobalMethod (methodObj )
795+ ? GlobalMethodSignature .from (methodObj ).map (m -> (MethodSignature ) m )
796+ : ClassMethodSignature .from (methodObj , thisParameterObjs .peek ().getType ())
797+ .map (m -> (MethodSignature ) m );
798+ methodSignatureOpt .ifPresentOrElse (
799+ methodSignature -> {
800+ if (!methodSignature .isInvokableBy (
801+ invokedMethodSignatureGenerator .getMethodSignature ())) {
802+ var overriddenMethodObj =
803+ findNearestDeclaration (
804+ invokedMethodSignatureGenerator .getMethodSignature (),
805+ thisParameterObjs .pop ());
806+ if (overriddenMethodObj .equals (Tab .noObj )) {
807+ if (invokedMethodSignatureGenerator .getMethodSignature ().allTypesAreKnown ()) {
808+ detectSemanticError (
809+ null ,
810+ methodCallDesignatorStatement ,
811+ SemanticErrorKind .INAPPLICABLE_METHOD ,
812+ methodSignature .toString (),
813+ invokedMethodSignatureGenerator .getMethodSignature ().getParameterList ());
814+ } else {
815+ detectSemanticError ();
816+ }
817+ } else {
818+ methodCallDesignatorStatement .getDesignator ().obj = overriddenMethodObj ;
819+ }
835820 }
836- } else {
837- methodCallDesignatorStatement .getDesignator ().obj = overriddenMethodObj ;
838- }
839- }
840- } else {
841- detectSemanticError ();
842- }
821+ },
822+ this ::detectSemanticError );
843823 }
844824 }
845825
@@ -1216,16 +1196,13 @@ public void visit(MethodCallFactor methodCallFactor) {
12161196 }
12171197 }
12181198 } else {
1219- MethodSignature methodSignature = null ;
1220- try {
1221- if (isGlobalMethod (methodObj )) {
1222- methodSignature = new GlobalMethodSignature (methodObj );
1223- } else {
1224- methodSignature = new ClassMethodSignature (methodObj , thisParameterObjs .peek ().getType ());
1225- }
1226- } catch (WrongObjKindException ignored ) {
1227- }
1228- if (methodSignature != null ) {
1199+ Optional <MethodSignature > methodSignatureOpt =
1200+ isGlobalMethod (methodObj )
1201+ ? GlobalMethodSignature .from (methodObj ).map (m -> (MethodSignature ) m )
1202+ : ClassMethodSignature .from (methodObj , thisParameterObjs .peek ().getType ())
1203+ .map (m -> (MethodSignature ) m );
1204+ if (methodSignatureOpt .isPresent ()) {
1205+ var methodSignature = methodSignatureOpt .get ();
12291206 if (!methodSignature .isInvokableBy (invokedMethodSignatureGenerator .getMethodSignature ())) {
12301207 var overriddenMethodObj =
12311208 findNearestDeclaration (
0 commit comments