public override void Visit(MethodInvocation node) { ValidateMethodCall(node); }
private void ValidateMethodCall(MethodInvocation node) { if (node.MethodOwner.Type is ArrayType || node.ReferencedMethod == null) { // Array's length method does not need to be checked because it cannot // take parameters. If ReferencedMethod is set to null, the method // could not be resolved and this error has already been reported. return; } if (node.ReferencedMethod.IsStatic) { // Note: this is not an actual error in Java, where static methods can // be referenced even through instances. However, since in MiniJava the // only static method is the main method, I have prevented calls to static // methods altogether. (In Java, of course, even the main method CAN be // called from inside the program, but who would want to do that?) ReportError(ErrorTypes.MethodReference, String.Format("Cannot call static method {0}.", node.MethodName), node); return; } if (node.CallParameters.Count != node.ReferencedMethod.Formals.Count) { ReportError( ErrorTypes.TypeError, String.Format("Wrong number of arguments to method {0} ({1} for {2}).", node.MethodName, node.CallParameters.Count, node.ReferencedMethod.Formals.Count), node); return; } ValidateCallParameterTypes(node, node.ReferencedMethod); }
public override void Visit(MethodInvocation node) { HandleExpressionOrStatementNode(node); }
private void ValidateCallParameterTypes(MethodInvocation node, MethodDeclaration methodDecl) { var callParamTypes = node.CallParameters.Select<IExpression, IType>((expr) => expr.Type).ToList(); for (int i = 0; i < methodDecl.Formals.Count; i++) { var callParamType = callParamTypes[i]; var formalParamType = methodDecl.Formals[i].Type; if (!callParamType.IsAssignableTo(formalParamType)) { var msg = String.Format( "Wrong type of argument to method {0}. Expected {1} but found {2}.", node.MethodName, formalParamType.Name, callParamType.Name); ReportError(ErrorTypes.TypeError, msg, node); } } }