public override IPStmt VisitPrintStmt(PParser.PrintStmtContext context) { string message = context.StringLiteral().GetText(); message = message.Substring(1, message.Length - 2); // strip beginning / end double quote int numNecessaryArgs = TypeCheckingUtils.PrintStmtNumArgs(message); if (numNecessaryArgs == -1) { throw handler.InvalidPrintFormat(context, context.StringLiteral().Symbol); } List <IPExpr> args = TypeCheckingUtils.VisitRvalueList(context.rvalueList(), exprVisitor).ToList(); foreach (IPExpr arg in args) { if (arg is LinearAccessRefExpr) { throw handler.PrintStmtLinearArgument(arg.SourceLocation); } } if (args.Count != numNecessaryArgs) { throw handler.IncorrectArgumentCount(context, args.Count, numNecessaryArgs); } return(new PrintStmt(context, message, args)); }
public static void ValidatePayloadTypes(ITranslationErrorHandler handler, ParserRuleContext context, PLanguageType payloadType, IReadOnlyList <IPExpr> arguments) { if (arguments.Count == 0) { if (!payloadType.IsSameTypeAs(PrimitiveType.Null)) { throw handler.TypeMismatch(context, PrimitiveType.Null, payloadType); } } else if (arguments.Count == 1) { CheckArgument(handler, context, payloadType, arguments[0]); } else if (payloadType.Canonicalize() is TupleType tuple) { foreach (Tuple <IPExpr, PLanguageType> pair in arguments.Zip(tuple.Types, Tuple.Create)) { CheckArgument(handler, context, pair.Item2, pair.Item1); } } else { throw handler.IncorrectArgumentCount(context, arguments.Count, 1); } }
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)); }
public override IPStmt VisitPrintStmt(PParser.PrintStmtContext context) { string message = context.StringLiteral().GetText(); int numNecessaryArgs = (from Match match in Regex.Matches(message, @"(?:{{|}}|{(\d+)}|[^{}]+|{|})") where match.Groups[1].Success select int.Parse(match.Groups[1].Value) + 1) .Concat(new[] { 0 }) .Max(); var args = (from arg in context.rvalueList()?.rvalue() ?? Enumerable.Empty <PParser.RvalueContext>() select exprVisitor.Visit(arg)).ToList(); if (args.Count != numNecessaryArgs) { throw handler.IncorrectArgumentCount((ParserRuleContext)context.rvalueList() ?? context, args.Count, numNecessaryArgs); } return(new PrintStmt(context, message, args)); }
private static void ValidateTransitions(ITranslationErrorHandler handler, Machine machine) { foreach (var state in machine.AllStates()) { foreach (var pair in state.AllEventHandlers) { var handledEvent = pair.Key; switch (pair.Value) { case EventDoAction eventDoAction: if (eventDoAction.Target != null) { ValidateEventPayloadToTransitionTarget(handler, eventDoAction.SourceLocation, handledEvent.PayloadType, eventDoAction.Target); } break; case EventGotoState eventGotoState: if (eventGotoState.Target.Entry != null) { ValidateEventPayloadToTransitionTarget(handler, eventGotoState.SourceLocation, handledEvent.PayloadType, eventGotoState.Target.Entry); } if (eventGotoState.TransitionFunction != null) { ValidateEventPayloadToTransitionTarget(handler, eventGotoState.SourceLocation, handledEvent.PayloadType, eventGotoState.TransitionFunction); } break; case EventPushState eventPushState: if (eventPushState.Target.Entry != null) { ValidateEventPayloadToTransitionTarget(handler, eventPushState.SourceLocation, handledEvent.PayloadType, eventPushState.Target.Entry); } break; } } if (state.Exit?.Signature.Parameters.Count > 0) { throw handler.IncorrectArgumentCount(state.SourceLocation, state.Exit.Signature.Parameters.Count, 0); } } }