public override IPStmt VisitFunCallStmt(PParser.FunCallStmtContext context) { string funName = context.fun.GetText(); List <IPExpr> argsList = TypeCheckingUtils.VisitRvalueList(context.rvalueList(), exprVisitor).ToList(); if (!table.Lookup(funName, out Function fun)) { throw handler.MissingDeclaration(context.fun, "function or function prototype", funName); } if (fun.Signature.Parameters.Count != argsList.Count) { throw handler.IncorrectArgumentCount((ParserRuleContext)context.rvalueList() ?? context, argsList.Count, fun.Signature.Parameters.Count); } foreach (Tuple <Variable, IPExpr> pair in fun.Signature.Parameters.Zip(argsList, Tuple.Create)) { TypeCheckingUtils.CheckArgument(handler, context, pair.Item1.Type, pair.Item2); } method.AddCallee(fun); return(new FunCallStmt(context, fun, argsList)); }
public override IPExpr VisitFunCallExpr(PParser.FunCallExprContext context) { string funName = context.fun.GetText(); if (!table.Lookup(funName, out Function function)) { throw handler.MissingDeclaration(context.fun, "function", funName); } // Check the arguments IPExpr[] arguments = TypeCheckingUtils.VisitRvalueList(context.rvalueList(), this).ToArray(); ISet <Variable> linearVariables = new HashSet <Variable>(); if (function.Signature.Parameters.Count != arguments.Length) { throw handler.IncorrectArgumentCount(context, arguments.Length, function.Signature.Parameters.Count); } for (int i = 0; i < arguments.Length; i++) { IPExpr argument = arguments[i]; PLanguageType paramType = function.Signature.Parameters[i].Type; if (!paramType.IsAssignableFrom(argument.Type)) { throw handler.TypeMismatch(context.rvalueList().rvalue(i), argument.Type, paramType); } if (argument is ILinearRef linearRef) { if (linearRef.LinearType == LinearType.Swap && !linearRef.Type.IsSameTypeAs(paramType)) { throw handler.TypeMismatch(context, linearRef.Type, paramType); } if (linearVariables.Contains(linearRef.Variable)) { throw handler.RelinquishedWithoutOwnership(linearRef); } linearVariables.Add(linearRef.Variable); } } method.AddCallee(function); return(new FunCallExpr(context, function, arguments)); }