From 57d200e22b7a3d6dd28b5525e44d2f677fd31b20 Mon Sep 17 00:00:00 2001 From: Ankush Desai Date: Mon, 12 Aug 2024 16:26:42 -0700 Subject: [PATCH] Fixed a bug related to duplicate bindings (#756) --- .../DefaultTranslationErrorHandler.cs | 20 ++++++++++++++----- .../CompilerCore/ITranslationErrorHandler.cs | 1 + .../TypeChecker/ModuleExprVisitor.cs | 7 +++++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/Src/PCompiler/CompilerCore/DefaultTranslationErrorHandler.cs b/Src/PCompiler/CompilerCore/DefaultTranslationErrorHandler.cs index e2a65cfbc..cd2466484 100644 --- a/Src/PCompiler/CompilerCore/DefaultTranslationErrorHandler.cs +++ b/Src/PCompiler/CompilerCore/DefaultTranslationErrorHandler.cs @@ -146,7 +146,8 @@ public Exception BinOpTypeMismatch( public Exception MoreThanOneParameterForHandlers(ParserRuleContext sourceLocation, int count) { - return IssueError(sourceLocation, $"functions at entry or exit and do or goto transitions cannot take more than 1 parameter, provided function expects {count} parameters"); + return IssueError(sourceLocation, + $"functions at entry or exit and do or goto transitions cannot take more than 1 parameter, provided function expects {count} parameters"); } public Exception ParseFailure(FileInfo file, string message) @@ -156,12 +157,14 @@ public Exception ParseFailure(FileInfo file, string message) public Exception IllegalChooseSubExprType(PParser.ChooseExprContext context, PLanguageType subExprType) { - return IssueError(context, $"choose expects a parameter of type int (max value) or a collection type (seq, set, or map) got a parameter of type {subExprType}"); + return IssueError(context, + $"choose expects a parameter of type int (max value) or a collection type (seq, set, or map) got a parameter of type {subExprType}"); } public Exception IllegalChooseSubExprValue(PParser.ChooseExprContext context, int numChoices) { - return IssueError(context, $"choose expects a parameter with at most 10000 choices, got {numChoices} choices instead."); + return IssueError(context, + $"choose expects a parameter with at most 10000 choices, got {numChoices} choices instead."); } public Exception IllegalFunctionUsedInSpecMachine(Function function, Machine callerOwner) @@ -322,7 +325,8 @@ public Exception BareLoopControlFlow(string stmtName, ParserRuleContext context) public Exception ExitFunctionCannotTakeParameters(ParserRuleContext sourceLocation, int count) { - return IssueError(sourceLocation, $"Exit functions cannot have input parameters, the provided function expects {count} parameters"); + return IssueError(sourceLocation, + $"Exit functions cannot have input parameters, the provided function expects {count} parameters"); } private Exception IssueError(ParserRuleContext location, string message) @@ -342,7 +346,13 @@ private string DeclarationName(IPDecl method) public string SpecObservesSetIncompleteWarning(ParserRuleContext ctx, PEvent ev, Machine machine) { - return $"[!Warning!]\n[{locationResolver.GetLocation(ctx, ctx.start)}] Event {ev.Name} is not in the observes list of the spec machine {machine.Name}. The event-handler is never triggered as the event is not observed by the spec.\n[!Warning!]"; + return + $"[!Warning!]\n[{locationResolver.GetLocation(ctx, ctx.start)}] Event {ev.Name} is not in the observes list of the spec machine {machine.Name}. The event-handler is never triggered as the event is not observed by the spec.\n[!Warning!]"; + } + + public Exception DuplicateBindings(ParserRuleContext ctx, Interface @interface) + { + return IssueError(ctx, $"Interface or machine {@interface.Name} is mentioned or bounded multiple times in the module"); } } } \ No newline at end of file diff --git a/Src/PCompiler/CompilerCore/ITranslationErrorHandler.cs b/Src/PCompiler/CompilerCore/ITranslationErrorHandler.cs index 4d7248634..371885fe8 100644 --- a/Src/PCompiler/CompilerCore/ITranslationErrorHandler.cs +++ b/Src/PCompiler/CompilerCore/ITranslationErrorHandler.cs @@ -120,5 +120,6 @@ Exception DuplicateStartState( Exception IllegalFunctionUsedInSpecMachine(Function function, Machine callerOwner); String SpecObservesSetIncompleteWarning(ParserRuleContext loc, PEvent ev, Machine machine); + Exception DuplicateBindings(ParserRuleContext loc, Interface @interface); } } \ No newline at end of file diff --git a/Src/PCompiler/CompilerCore/TypeChecker/ModuleExprVisitor.cs b/Src/PCompiler/CompilerCore/TypeChecker/ModuleExprVisitor.cs index cba686404..ce6bccb13 100644 --- a/Src/PCompiler/CompilerCore/TypeChecker/ModuleExprVisitor.cs +++ b/Src/PCompiler/CompilerCore/TypeChecker/ModuleExprVisitor.cs @@ -98,6 +98,13 @@ public override IPModuleExpr VisitNamedModule([NotNull] PParser.NamedModuleConte public override IPModuleExpr VisitPrimitiveModuleExpr([NotNull] PParser.PrimitiveModuleExprContext context) { var bindings = context._bindslist.Select(VisitBindExpr).ToList(); + var seenInterfaces = new List(); + foreach (var i in bindings.Select(x => x.Item1)) + { + if (seenInterfaces.Contains(i.Name)) + throw handler.DuplicateBindings(context, i); + seenInterfaces.Add(i.Name); + } return new BindModuleExpr(context, bindings); }