Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package at.tugraz.ist.cc.ClassBodyInfo;
- import at.tugraz.ist.cc.InformationContainer.ClassContainer;
- import at.tugraz.ist.cc.InformationContainer.MethodContainer;
- import at.tugraz.ist.cc.JovaParser;
- import at.tugraz.ist.cc.TypeInfo.GetType;
- import at.tugraz.ist.cc.TypeInfo.TypeInfoChecker;
- import at.tugraz.ist.cc.error.ErrorHandler;
- import at.tugraz.ist.cc.process.AccessTheMembers;
- import at.tugraz.ist.cc.process.AssignmentClass;
- import at.tugraz.ist.cc.process.Interposed;
- import at.tugraz.ist.cc.process.PartBlock;
- import at.tugraz.ist.cc.process.right_values.*;
- import org.antlr.v4.runtime.tree.TerminalNode;
- import java.util.LinkedList;
- import java.util.List;
- import static java.awt.SystemColor.info;
- public class ClassBodyMethodChecker extends Exception {
- class InvalidStatementException extends RuntimeException {
- InvalidStatementException() { super("");
- }
- }
- public ClassBodyInfoChecker bodyInfoChecker;
- public JovaParser.Method_declContext method_declContext;
- public JovaParser.TypeContext typeContext;
- public MethodContainer methodContainer;
- public ClassBodyMethodChecker(ClassBodyInfoChecker bodyInfoChecker, JovaParser.TypeContext typeContext,
- JovaParser.Method_declContext method_declContext, MethodContainer methodContainer)
- {
- this.bodyInfoChecker = bodyInfoChecker;
- this.typeContext = typeContext;
- this.method_declContext = method_declContext;
- this.methodContainer = methodContainer;
- }
- public ClassBodyMethodChecker(MethodContainer container, ClassBodyInfoChecker checker)
- {
- methodContainer = container;
- bodyInfoChecker = checker;
- }
- public void methodBodyChecker(JovaParser.Method_bodyContext method_bodyContext) {
- for(JovaParser.DeclarationContext declarationsContext: method_bodyContext.declarations().declaration())
- {
- ClassContainer var_type = null;
- String prim_type = null;
- GetType getType = new GetType(typeContext);
- if(!getType.isPrimitiveType())
- var_type = bodyInfoChecker.checker_.getNotPrimitiveType(typeContext);
- else
- prim_type = bodyInfoChecker.checker_.getPrimitiveType(method_declContext);
- if(var_type == null && prim_type == null)
- {
- ErrorHandler.INSTANCE.addUnknownTypeError(declarationsContext.getStart().getLine(),
- declarationsContext.getStart().getCharPositionInLine(),
- declarationsContext.getStart().getText());
- continue;
- }
- if(var_type.getClassName().equals("Main") || prim_type.equals("Main"))
- {
- ErrorHandler.INSTANCE.addMainInstatiationError(declarationsContext.getStart().getLine(), declarationsContext.getStart().getCharPositionInLine());
- continue;
- }
- // System.out.println("primitive = " + primitive_var.getText());
- //find existing expression and check stand for left-hand side and right-hand side
- automaticCastingChecker(declarationsContext, prim_type, var_type, getType);
- }
- }
- public void automaticCastingChecker(JovaParser.DeclarationContext declarationContext, String prim_type, ClassContainer var_type, GetType getType)
- {
- for(JovaParser.Id_listContext id_listContext: declarationContext.id_lists().id_list())
- {
- if(methodContainer.name_Of_scopes.containsKey(id_listContext.ID().getText()))
- {
- ErrorHandler.INSTANCE.addVarDoubleDefError(declarationContext.getStart().getLine(), declarationContext.getStart().getCharPositionInLine(),
- id_listContext.ID().getText(), declarationContext.getStart().getText(), methodContainer.method_name_, methodContainer.convertParameterNames());
- continue;
- }
- Interposed leftValue = null;
- if(getType.isPrimitiveType())
- leftValue = methodContainer.instantiate_primitive_type(prim_type);
- else
- leftValue = methodContainer.instantiate_notPrimitive_type(var_type);
- methodContainer.name_Of_scopes.put(id_listContext.ID().getText() , leftValue.getMethod_index());
- JovaParser.ExprContext exprContext = id_listContext.expr();
- if(exprContext != null)
- {
- //TODO getRightValue == unix !!!
- //TODO == isEitherOfNeitherType
- //TODO convertToBool == intToBool
- //TODO convertToInteger == boolToInt
- //RightValue rightValue = getRightValue();
- //if(rightValue.getType != leftValue)
- //{
- // rightValue = getRightValue();
- // if(isEitherofNeitherType())
- // {
- // ErrorHandler.INSTANCE.addBinaryTypeError(
- // id_listContext.getStart().getName(),
- // id_listContext.getStart().getCharPositionInLine(),
- // leftValue.getType().getName(),
- // rightValue.getType.getName(),
- // id_listContext.ASSIGN.getText());
- // //TODO da li treba exception???
- // }
- // ErrorHandler.INSTANCE.addBinaryTypeCoercionWarning(
- // id_listContext.ASSIGN.getSymbol.getLine(),
- // id_listContext.ASSIGN.getSymbol.getCharPositionInLine(),
- // id_listContext.ASSIGN.getText(),
- // leftValue.getType().getName(),
- // rightValue.getType().getName(),
- // leftValue.getType().getName(),
- // rightValue.getType().getName());
- //
- // if(leftValue.getType() == PrimitiveType.BOOLEAN)
- // rightValue = convertToBool();
- // else
- // rightValue = convertToInteger();
- }
- }
- }
- ///-----------------------------------------------------------------------------------------------------------------
- public RightValueNode castIntegerToBoolean(RightValueNode val, PartBlock block)
- {
- return toNodeValue(block, new RightValueBinaryOperator(val, new RightValueInteger(0), OperatorRelationRightValue.OPERATOR_NOT_EQUAL));
- }
- public boolean checkTypeValid(RightValueNode val)
- {
- boolean a = false;
- if (val.part_to_single_part() == "int")
- {
- a = true;
- }
- if (val.part_to_single_part() == "boolean")
- {
- a = true;
- }
- return a;
- }
- public RightValueNode toNodeValue(PartBlock block, RightValue val)
- {
- GetType type = new GetType();
- Interposed caller = null;
- boolean tip = type.isPrimitiveType((JovaParser.TypeContext) val);
- if(tip)
- caller = methodContainer.instantiate_primitive_type((String)val.part_to_single_part());
- else
- caller = methodContainer.instantiate_notPrimitive_type((ClassContainer)val.part_to_single_part());
- block.body_part.add(new AssignmentClass(val,caller));
- //System.out.println("tip ovaj = "+ tip);
- return caller;
- }
- public RightValue castExprToRightValue(JovaParser.ExprContext ctx)
- {
- RightValue rv = null;
- rv = (RightValue) ctx.expr();
- return rv;
- }
- public RightValueNode castBooleanToInt(RightValueNode cond, PartBlock block)
- {
- RightValueNode rv = null;
- rv = toNodeValue(block, new RightValueTernaryOperator(cond, new RightValueInteger(1), new RightValueInteger(0)));
- return rv;
- }
- public LeftRightValueNode handleMemberAccess(JovaParser.Member_accessContext ctx, PartBlock block)
- {
- return null;
- }
- public RightValue resolveReturnValue(TerminalNode node)
- {
- return null;
- }
- public RightValue exprToRightValue(JovaParser.ExprContext expr_ctx, PartBlock block)
- {
- if (expr_ctx.operand() != null)
- {
- System.out.println("nije nula = " + expr_ctx.operand().getText());
- return castOperandRightValue(expr_ctx.operand(),block);
- }
- System.out.println("expr ctx OPERATOR = " + expr_ctx.OPERATOR().getText());
- TerminalNode operator = expr_ctx.OPERATOR();
- RightValue op1, op2;
- System.out.println("size expr ??? = " + expr_ctx.expr().size());
- // a = castExprToRightValue(expr_ctx.expr(0));
- op1 = (RightValue) expr_ctx.expr(0);
- op2 = (RightValue) expr_ctx.expr(1);
- if (operator != null)
- {
- /// handle all operators
- // handle not
- if (operator.getText().equals("NOT"))
- {
- }
- else if (operator.getText().equals("SIGN"))
- {
- }
- else if (operator.getText().equals("RELOP"))
- {
- }
- else if (operator.getText().equals("AND"))
- {
- }
- else if (operator.getText().equals("OR"))
- {
- }
- if(op1.getClass().getTypeName().equals("boolean") || op1.getClass().getTypeName().equals("int"))
- {
- System.out.println("exprtoRV op1.getClass.getTypeName = " + op1.getClass().getTypeName());
- ErrorHandler.INSTANCE.addBinaryTypeError(operator.getSymbol().getLine(), operator.getSymbol().getCharPositionInLine(),
- op1.getClass().getTypeName(), op2.getClass().getTypeName(), operator.getText());
- // throw new InvalidStatementException();
- }
- else if(op2.getClass().getTypeName().equals("boolean") || op2.getClass().getTypeName().equals("int"))
- {
- System.out.println("exprtoRV op2.getClass.getTypeName = " + op2.getClass().getTypeName());
- ErrorHandler.INSTANCE.addBinaryTypeError(operator.getSymbol().getLine(), operator.getSymbol().getCharPositionInLine(),
- op1.getClass().getTypeName(), op2.getClass().getTypeName(), operator.getText());
- // throw new InvalidStatementException();
- }
- /// treba mi tip operanda, nije ovo to to.....
- if (!op1.getClass().getTypeName().equals("int"))
- {
- if (!op1.getClass().getTypeName().equals("boolen"))
- {
- /// castBooleanToInt
- }
- else
- {
- /// rejected type - error
- }
- }
- if (!op2.getClass().getTypeName().equals("int"))
- {
- if (!op2.getClass().getTypeName().equals("boolen"))
- {
- /// castBooleanToInt
- }
- else
- {
- /// rejected type - error
- }
- }
- if (!op1.getClass().getTypeName().equals("int") || !op2.getClass().getTypeName().equals("int"))
- {
- ErrorHandler.INSTANCE.addBinaryTypeCoercionWarning(operator.getSymbol().getLine(), operator.getSymbol().getCharPositionInLine(),
- operator.getSymbol().getText(), op1.getClass().getName(), op2.getClass().getName(), "int", "int");
- }
- }
- return null;
- }
- public RightValue castOperandRightValue(JovaParser.OperandContext operand_ctx, PartBlock block)
- {
- RightValue retType = null;
- /// check operand type
- TerminalNode integer_val = operand_ctx.INT();
- TerminalNode string_val = operand_ctx.STRING_LIT();
- TerminalNode bool_val = operand_ctx.STRING_LIT();
- /// handle primitive type
- if (integer_val != null)
- {
- System.out.println("1");
- retType = new RightValueInteger(Integer.parseInt(integer_val.getText()));
- return retType;
- }
- else if (string_val != null)
- {
- System.out.println("2");
- retType = new RightValueString(integer_val.getText());
- return retType;
- }
- else if (bool_val != null)
- {
- System.out.println("3");
- retType = new RightValueBool(Boolean.parseBoolean(bool_val.getText()));
- return retType;
- }
- TerminalNode nix = operand_ctx.KEY_NIX();
- if (nix != null)
- {
- retType = new SpecifierNixRightValue(null);
- return retType;
- }
- JovaParser.Negate_operandContext not = operand_ctx.negate_operand();
- JovaParser.Sign_operandContext sign = operand_ctx.sign_operand();
- JovaParser.Object_allocContext alloc = operand_ctx.object_alloc();
- JovaParser.Id_operandContext id = operand_ctx.id_operand();
- JovaParser.ExprContext expr = operand_ctx.expr();
- // RightValueNode e = toNodeValue();
- if (not != null)
- {
- System.out.println("negation ");
- // System.out.println("operand = " + operand_ctx.NOT());
- RightValueNode right_value = toNodeValue(block, castOperandRightValue(operand_ctx.negate_operand().operand(), block));
- boolean isTypeValid = checkTypeValid(right_value);
- if (!isTypeValid)
- {
- ErrorHandler.INSTANCE.addUnaryTypeError(operand_ctx.getStart().getLine(), operand_ctx.getStart().getCharPositionInLine(),
- (String) right_value.part_to_single_part(), operand_ctx.negate_operand().NOT().getText());
- /// INVALID STMT EXCEPTION
- }
- if (right_value.part_to_single_part().equals("int"))
- {
- ErrorHandler.INSTANCE.addUnaryTypeCoercionWarning(operand_ctx.negate_operand().NOT().getSymbol().getLine(),
- operand_ctx.negate_operand().NOT().getSymbol().getCharPositionInLine(), operand_ctx.negate_operand().NOT().getText(),
- (String) right_value.part_to_single_part(),"boolean");
- right_value = castIntegerToBoolean(right_value, block);
- }
- return new RightValueUnaryOperator(right_value, OperatorLogicUnaryRightValue.OPERATOR_NOT);
- }
- else if (sign != null)
- {
- RightValueNode right_value = toNodeValue(block, castOperandRightValue(sign.operand(), block));
- if (checkTypeValid(right_value))
- {
- ErrorHandler.INSTANCE.addUnaryTypeError(operand_ctx.getStart().getLine(), operand_ctx.getStart().getCharPositionInLine(),
- (String) right_value.part_to_single_part(), sign.SIGN().getText());
- // throw new InvalidStatementException();
- }
- if (right_value.part_to_single_part().equals("boolean"))
- {
- ErrorHandler.INSTANCE.addUnaryTypeCoercionWarning(sign.SIGN().getSymbol().getLine(), sign.SIGN().getSymbol().getCharPositionInLine(),
- sign.SIGN().getText(), (String) right_value.part_to_single_part(), "int");
- right_value = castBooleanToInt(right_value, block);
- }
- if (sign.SIGN().getText().equals("-"))
- {
- return new RightValueUnaryOperator(right_value, OperatorArithmeticUnaryRightValue.OPERATOR_INVERS);
- }
- else
- {
- return right_value;
- }
- }
- else if (alloc != null)
- {
- String class_name = alloc.CLASS_TYPE().getText();
- if (!bodyInfoChecker.checker_.collection_of_classes.containsKey(class_name))
- {
- ErrorHandler.INSTANCE.addUnknownTypeError(operand_ctx.getStart().getLine(), operand_ctx.getStart().getCharPositionInLine(), class_name);
- // throw new InvalidStatementException();
- }
- if (class_name.equals("Main"))
- {
- ErrorHandler.INSTANCE.addMainInstatiationError(
- operand_ctx.getStart().getLine(),
- operand_ctx.getStart().getCharPositionInLine()
- );
- // throw new InvalidStatementException();
- }
- return new RightValueAllocation(bodyInfoChecker.checker_.collection_of_classes.get(class_name));
- }
- else if (id != null)
- {
- return castIdOperandToRightValue(block, operand_ctx.id_operand());
- }
- else if (expr != null)
- {
- return castExprToRightValue(operand_ctx.expr());
- }
- return null;
- }
- public void addNodesToList(JovaParser.ExprContext context_of_arguments, PartBlock block, List<RightValueNode> list_of_nodes, List<String> types_of_arguments)
- {
- RightValueNode node;
- node = toNodeValue(block, exprToRightValue(context_of_arguments, block));
- list_of_nodes.add(node);
- types_of_arguments.add((String)node.part_to_single_part());
- }
- public RightValue castIdOperandToRightValue(PartBlock block, JovaParser.Id_operandContext idoperand_ctx)
- {
- RightValue rvalue;
- List<String> types_of_arguments = new LinkedList<>();
- List<RightValueNode> list_of_nodes = new LinkedList<>();
- if (idoperand_ctx.arg_list() == null)
- {
- LeftRightValueNode ret = handleMemberAccess(idoperand_ctx.member_access(), block);
- return ret;
- }
- if (idoperand_ctx.member_access().member_access() == null) {
- rvalue = null;
- }
- else {
- rvalue = handleMemberAccess(idoperand_ctx.member_access().member_access(), block);
- }
- for (JovaParser.ExprContext context_of_arguments : idoperand_ctx.arg_list().args().expr())
- {
- addNodesToList(context_of_arguments, block, list_of_nodes, types_of_arguments);
- }
- GetType typechecker = new GetType();
- boolean check = typechecker.isPrimitiveType((JovaParser.TypeContext) rvalue);
- System.out.println("ispis moj je " + rvalue.toString());
- if (check)
- {
- errorHandlerDoYourThing(idoperand_ctx, rvalue, types_of_arguments, 1);
- }
- MethodContainer method = ((ClassContainer) rvalue.part_to_single_part()).getMethod(idoperand_ctx.member_access().member_access().ID().getText(), types_of_arguments);
- if (method == null)
- {
- if (idoperand_ctx.member_access().member_access() != null )
- {
- errorHandlerDoYourThing(idoperand_ctx, rvalue, types_of_arguments, 2);
- }
- else{
- errorHandlerDoYourThing(idoperand_ctx, rvalue, types_of_arguments, 3);
- }
- throw new InvalidStatementException();
- }
- if (((ClassContainer)rvalue.part_to_single_part() != this.methodContainer.container_))
- {
- if(method.method_privacy_flag_)
- {
- ErrorHandler.INSTANCE.addMethodAccessError(
- idoperand_ctx.getStart().getLine(),
- idoperand_ctx.getStart().getCharPositionInLine(),
- idoperand_ctx.member_access().ID().getText(),
- (String)((ClassContainer) rvalue.part_to_single_part()).getClassName(),
- method.convertParameterNames()
- );
- throw new InvalidStatementException();
- }
- }
- return new CallerOfMethodRightValue(method,toNodeValue(block, rvalue), list_of_nodes);
- }
- public void errorHandlerDoYourThing(JovaParser.Id_operandContext idoperand_ctx, RightValue rvalue, List<String> types_of_arguments, int flag)
- {
- System.out.println("ispis moj je " + rvalue.toString());
- if(flag == 1)
- {
- ErrorHandler.INSTANCE.addCannotInvokeError(idoperand_ctx.getStart().getLine(), idoperand_ctx.getStart().getCharPositionInLine(),
- ((JovaParser.TypeContext) rvalue).PRIMITIVE_TYPE().getText(), idoperand_ctx.member_access().ID().getText(),
- MethodContainer.convertNames(types_of_arguments)
- );throw new InvalidStatementException();
- }
- if(flag == 2)
- {
- ErrorHandler.INSTANCE.addCannotInvokeError(
- idoperand_ctx.getStart().getLine(),
- idoperand_ctx.getStart().getCharPositionInLine(),
- (String)((ClassContainer) rvalue.part_to_single_part()).getClassName(),
- idoperand_ctx.member_access().ID().getText(),
- MethodContainer.convertNames(types_of_arguments)
- );
- }
- if(flag == 3)
- {
- ErrorHandler.INSTANCE.addUndefMethodError(idoperand_ctx.getStart().getLine(), idoperand_ctx.getStart().getCharPositionInLine(), idoperand_ctx.member_access().ID().getText(),
- MethodContainer.convertNames(types_of_arguments)
- );
- }
- }
- private void invokeErrorHandlerErrors(JovaParser.Member_accessContext context, RightValue rvalue, int flag)
- {
- if(flag == 1)
- ErrorHandler.INSTANCE.addDoesNotHaveFieldError(context.getStart().getLine(), context.getStart().getCharPositionInLine(), rvalue.toString(), context.ID().getText());
- if(flag == 2)
- ErrorHandler.INSTANCE.addDoesNotHaveFieldError(context.getStart().getLine(), context.getStart().getCharPositionInLine(), ((ClassContainer) rvalue.part_to_single_part()).getClassName(),
- context.ID().getText());
- }
- public LeftRightValueNode sortingOutAccessOfMembers(JovaParser.Member_accessContext context, PartBlock body)
- {
- int flag = 0;
- RightValue rvalue;
- if (context.member_access() != null) {
- rvalue = sortingOutAccessOfMembers(context.member_access(), body);
- }
- else
- {
- rvalue = null;
- flag = 1;
- }
- GetType typechecker = new GetType();
- boolean check = typechecker.isPrimitiveType((JovaParser.TypeContext) rvalue);
- if (check)
- {
- invokeErrorHandlerErrors(context, rvalue, 1);
- throw new InvalidStatementException();
- }
- if (((ClassContainer) rvalue.part_to_single_part()).name_of_variable.get(context.ID().getText()) == null)
- {
- if (flag!=1)
- {
- invokeErrorHandlerErrors(context, rvalue, 2);
- }
- else
- {
- ErrorHandler.INSTANCE.addUndefIdError(context.getStart().getLine(), context.getStart().getCharPositionInLine(), context.ID().getText()
- );
- }
- throw new InvalidStatementException(); // TODO: change invalidstmt..
- }
- if (this.methodContainer.container_ !=((ClassContainer) rvalue.part_to_single_part() ))
- {
- if(((ClassContainer) rvalue.part_to_single_part()).private_variables.contains(((ClassContainer) rvalue.part_to_single_part()).name_of_variable.get(context.ID().getText())))
- {
- ErrorHandler.INSTANCE.addMemberAccessError(context.getStart().getLine(), context.getStart().getCharPositionInLine(), context.ID().getText(),
- ((ClassContainer) rvalue.part_to_single_part()).getClassName()
- );
- throw new InvalidStatementException();
- }
- }
- return new AccessTheMembers(((ClassContainer) rvalue.part_to_single_part()).name_of_variable.get(context.ID().getText()), toNodeValue(body,rvalue));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement