public override TranslationResult Translate(TranslationContext context) { Silvernode sn = null; switch (this.kind) { case LiteralKind.Boolean: sn = this.booleanValue ? new TextSilvernode("true", this.OriginalNode) : new TextSilvernode("false", this.OriginalNode); break; case LiteralKind.Int32: sn = new TextSilvernode(this.integerValue.ToString(), this.OriginalNode); break; case LiteralKind.Null: sn = new TextSilvernode("null", this.OriginalNode); break; } if (sn != null) { return(TranslationResult.FromSilvernode(sn)); } else { return(TranslationResult.Error(this.OriginalNode, Diagnostics.SSIL101_UnknownNode, this.OriginalNode.Kind())); } }
public override TranslationResult Translate(TranslationContext translationContext) { if (this.reason != null) { return(TranslationResult.Error(this.OriginalNode, Diagnostics.SSIL108_FeatureNotSupported, this.reason)); } return(TranslationResult.Error(this.OriginalNode, Diagnostics.SSIL101_UnknownNode, this.OriginalNode.Kind())); }
public override TranslationResult Translate(TranslationContext context) { if (this.errorneousResult != null) { return(TranslationResult.Error(this.errorneousResult.Node, this.errorneousResult.Diagnostic, this.errorneousResult.DiagnosticArguments)); } return(TranslationResult.Error(this.OriginalNode, Diagnostics.SSIL127_LambdasOnlyInContracts)); }
public override TranslationResult Translate(TranslationContext context) { // see thesis for details if (error != null) { return(TranslationResult.Error(error)); } List <Error> errors = new List <Error>(); var temporaryHoldingVariable = context.Process.IdentifierTranslator.RegisterNewUniqueIdentifier(); // ReSharper disable once UseObjectOrCollectionInitializer var arguments = new List <Silvernode>(); var prepend = new List <StatementSilvernode>(); arguments.Add("Seq("); foreach (var arg in this.Arguments) { var res = arg.Translate(context.ChangePurityContext(PurityContext.Purifiable)); arguments.Add(res.Silvernode); if (arg != this.Arguments.Last()) { arguments.Add(", "); } errors.AddRange(res.Errors); prepend.AddRange(res.PrependTheseSilvernodes); } arguments.Add(")"); Silvernode arrayConstruction = new SimpleSequenceSilvernode(null, arguments.ToArray()); prepend.AddRange(new StatementSilvernode[] { new VarStatementSilvernode(temporaryHoldingVariable, SilverType.Ref, this.OriginalNode), new SimpleSequenceStatementSilvernode(this.OriginalNode, new IdentifierSilvernode(temporaryHoldingVariable), " := ", "new(", ArraysTranslator.IntegerArrayContents, ")"), new AssignmentSilvernode( new SimpleSequenceSilvernode(this.OriginalNode, new IdentifierSilvernode(temporaryHoldingVariable), ".", ArraysTranslator.IntegerArrayContents), arrayConstruction, this.OriginalNode) }); switch (context.PurityContext) { case PurityContext.PurityNotRequired: case PurityContext.Purifiable: return(TranslationResult.FromSilvernode(new IdentifierSilvernode(temporaryHoldingVariable), errors).AndPrepend(prepend)); case PurityContext.PureOrFail: return(TranslationResult.Error(this.OriginalNode, Diagnostics.SSIL114_NotPureContext, "Array creation is inherently impure.")); } throw new Exception("This should never be reached."); }
public override TranslationResult Translate(TranslationContext context) { if (!(this.Identifier is IdentifierExpressionSharpnode)) { return(TranslationResult.Error(this.OriginalNode, Diagnostics.SSIL110_InvalidSyntax, "illegal goto target")); } IdentifierExpressionSharpnode identifierExpression = (IdentifierExpressionSharpnode)this.Identifier; ISymbol symbol = identifierExpression.GetIdentifierSymbol(context); var identifier = context.Process.IdentifierTranslator.GetIdentifierReference(symbol); return(TranslationResult.FromSilvernode( new GotoSilvernode(identifier, this.OriginalNode) )); }
public override TranslationResult Translate(TranslationContext context) { var constructorSymbol = context.Semantics.GetSymbolInfo(this.OriginalNode).Symbol as IMethodSymbol; var classSymbol = constructorSymbol.ContainingType; bool isDefaultConstructor = constructorSymbol.IsImplicitlyDeclared; // Special case: if (classSymbol.GetQualifiedName() == SeqTranslator.SeqClassWithoutEndDot) { return(SeqTranslator.Constructor(this.Arguments, context, classSymbol.TypeArguments[0], this.OriginalNode)); } var identifier = context.Process.IdentifierTranslator.RegisterNewUniqueIdentifier(); // Normal case: As method call var arguments = new List <Silvernode>(); var errors = new List <Error>(); var prependThese = new List <StatementSilvernode>(); foreach (var arg in this.Arguments) { var res = arg.Translate(context.ChangePurityContext(PurityContext.Purifiable)); arguments.Add(res.Silvernode); errors.AddRange(res.Errors); prependThese.AddRange(res.PrependTheseSilvernodes); } Silvernode constructorCall = new CallSilvernode(context.Process.IdentifierTranslator.GetIdentifierReferenceWithTag(classSymbol, isDefaultConstructor ? Constants.InitializerTag : Constants.ConstructorTag), arguments, SilverType.Ref, this.OriginalNode); prependThese.Add(new VarStatementSilvernode(identifier, SilverType.Ref, this.OriginalNode)); prependThese.Add(new AssignmentSilvernode(new IdentifierSilvernode(identifier), constructorCall, this.OriginalNode)); switch (context.PurityContext) { case PurityContext.PurityNotRequired: case PurityContext.Purifiable: return(TranslationResult.FromSilvernode(new IdentifierSilvernode(identifier), errors).AndPrepend( prependThese )); case PurityContext.PureOrFail: return(TranslationResult.Error(this.OriginalNode, Diagnostics.SSIL114_NotPureContext, "Object creation is inherently impure.")); } throw new System.Exception("This should never be reached."); }
public override TranslationResult Translate(TranslationContext context) { // see thesis for details SymbolInfo symbolInfo = context.Semantics.GetSymbolInfo(this.eaes); ISymbol symbol = symbolInfo.Symbol; string accessorName = symbol?.GetQualifiedName(); var errors = new List <Error>(); var container = this.Container.Translate(context); var index = this.Index.Translate(context.ChangePurityContext(PurityContext.Purifiable)); errors.AddRange(container.Errors); errors.AddRange(index.Errors); if (accessorName == SeqTranslator.SeqAccess) { return(TranslationResult.FromSilvernode( new SimpleSequenceSilvernode(this.OriginalNode, container.Silvernode, "[", index.Silvernode, "]" ), errors ).AndPrepend(container.PrependTheseSilvernodes.Concat(index.PrependTheseSilvernodes))); } else { var typeInfo = context.Semantics.GetTypeInfo(Container.OriginalNode); var t = typeInfo.Type; if (t.Kind == SymbolKind.ArrayType) { // Let's assume that this is an array read. // If this is an array write, the parent will usee ArraysContainer and ArraysIndex instead var readsilvernode = context.Process.ArraysTranslator.ArrayRead(this.OriginalNode, container.Silvernode, index.Silvernode); TranslationResult read = TranslationResult.FromSilvernode(readsilvernode, errors).AndPrepend(container.PrependTheseSilvernodes.Concat(index.PrependTheseSilvernodes)); read.ArraysContainer = container; read.ArraysIndex = index; return(read); } else { return(TranslationResult.Error(this.OriginalNode, Diagnostics.SSIL128_IndexersAreOnlyForSeqsAndArrays)); } } }
public override TranslationResult Translate(TranslationContext context) { var exResult = this.Expression.Translate(context); if (exResult.Silvernode.IsContract()) { return(exResult); } else if (exResult.Silvernode is CallSilvernode) { // It is a method call -- and the result is ignored. CallSilvernode call = (CallSilvernode)(exResult.Silvernode); if (call.Type != SilverType.Void) { var tempVar = context.Process.IdentifierTranslator.RegisterNewUniqueIdentifier(); var sequence = new StatementsSequenceSilvernode(this.OriginalNode, new VarStatementSilvernode(tempVar, call.Type, null), new AssignmentSilvernode( new IdentifierSilvernode(tempVar), call, null) ); return(TranslationResult.FromSilvernode(sequence, exResult.Errors)); } else { return(TranslationResult.FromSilvernode(call, exResult.Errors)); } } else if (exResult.Silvernode is BinaryExpressionSilvernode) { return(exResult); } else if (exResult.Silvernode is AssignmentSilvernode) { return(exResult); } else if (exResult.Silvernode is StatementSilvernode) { return(exResult); } return(TranslationResult.Error(this.Expression.OriginalNode, Diagnostics.SSIL107_ThisExpressionCannotBeStatement)); }
public override TranslationResult Translate(TranslationContext context) { var classSymbol = context.Semantics.GetDeclaredSymbol(this.OriginalNode as ClassDeclarationSyntax); // Should translate at all? var attributes = classSymbol.GetAttributes(); switch (VerificationSettings.ShouldVerify(attributes, context.VerifyUnmarkedItems)) { case VerificationSetting.DoNotVerify: return(TranslationResult.FromSilvernode(new SinglelineCommentSilvernode($"Class {classSymbol.GetQualifiedName()} skipped.", this.OriginalNode))); case VerificationSetting.Contradiction: return(TranslationResult.Error(this.OriginalNode, Diagnostics.SSIL113_VerificationSettingsContradiction)); } // Combine all members return(CommonUtils.GetHighlevelSequence(this.children.Select(child => child.Translate(context)))); }
/// <summary> /// Prepares for insertion into quantifier. /// Returns true if this lambda expression is valid for translation inside a ForAll or Exists call; false otherwise. /// </summary> /// <param name="context">The context.</param> public bool PrepareForInsertionIntoQuantifier(TranslationContext context) { if (this.errorneousResult != null) { this.failedResult = TranslationResult.Error(this.errorneousResult.Node, this.errorneousResult.Diagnostic, this.errorneousResult.DiagnosticArguments); return(false); } // Translate the single parameter's type var parameterSymbol = context.Semantics.GetDeclaredSymbol(this.parameter.ParameterSyntax); this.VariableIdentifier = context.Process.IdentifierTranslator.RegisterAndGetIdentifier(parameterSymbol); this.VariableSilverType = TypeTranslator.TranslateType(parameterSymbol.Type, this.parameter.OriginalNode, out this.errorneousResult); if (this.errorneousResult != null) { this.failedResult = TranslationResult.Error(this.errorneousResult.Node, this.errorneousResult.Diagnostic, this.errorneousResult.DiagnosticArguments); return(false); } // Translate the lambda's body TranslationResult res = this.body.Translate(context.ChangePurityContext(PurityContext.PureOrFail)); if (res.WasTranslationSuccessful) { this.BodySilvernode = res.Silvernode; } else { this.failedResult = res; return(false); } // Nothing went wrong. return(true); }
public override TranslationResult Translate(TranslationContext translationContext) { return(TranslationResult.Error(this.OriginalNode, Diagnostics.SSIL101_UnknownNode, this.OriginalNode.Kind())); }
public override TranslationResult Translate(TranslationContext context) { // Get symbol from semantic analysis var method = context.Semantics.GetSymbolInfo(this.MethodGroup); var methodSymbol = method.Symbol as IMethodSymbol; if (methodSymbol == null) { return(TranslationResult.Error(this.OriginalNode, Diagnostics.SSIL123_ThereIsThisCSharpError, "This name cannot be unambiguously mapped to a single method.")); } var methodName = methodSymbol.GetQualifiedName(); // There are many special cases where we don't want to translate a method call as a Viper method or function call // Each case is a subclass of InvocationTranslation. See InvocationTranslation for details. // These special cases all concern invocation targets within Soothsharp.Contracts and are translated into // keyworded Viper constructs InvocationTranslation translationStyle; switch (methodName) { case ContractsTranslator.ContractEnsures: case ContractsTranslator.ContractRequires: case ContractsTranslator.ContractInvariant: translationStyle = new InvocationContract(methodName); break; case ContractsTranslator.Implication: translationStyle = new InvocationImplicationEquivalence("==>", this.MethodGroup); break; case ContractsTranslator.Equivalence: translationStyle = new InvocationImplicationEquivalence("<==>", this.MethodGroup); break; case ContractsTranslator.ForAll: translationStyle = new InvocationQuantifier(QuantifierKind.ForAll); break; case ContractsTranslator.Exists: translationStyle = new InvocationQuantifier(QuantifierKind.Exists); break; case ContractsTranslator.ContractAssert: translationStyle = new InvocationPhpStatement("assert"); break; case ContractsTranslator.ContractAssume: translationStyle = new InvocationPhpStatement("assume"); break; case ContractsTranslator.ContractInhale: translationStyle = new InvocationPhpStatement("inhale"); break; case ContractsTranslator.ContractExhale: translationStyle = new InvocationPhpStatement("exhale"); break; case ContractsTranslator.ContractAcc: translationStyle = new InvocationViperBuiltInFunction("acc", false); break; case ContractsTranslator.ContractAccArray: translationStyle = new InvocationAccArray(); break; case ContractsTranslator.Old: translationStyle = new InvocationViperBuiltInFunction("old", false); break; case ContractsTranslator.Fold: translationStyle = new InvocationPhpStatement("fold"); break; case ContractsTranslator.Unfold: translationStyle = new InvocationPhpStatement("unfold"); break; case ContractsTranslator.PermissionCreate: translationStyle = new InvocationPermissionCreate(); break; case ContractsTranslator.PermissionFromLocation: translationStyle = new InvocationViperBuiltInFunction("perm", false); break; case SeqTranslator.Contains: translationStyle = new InvocationSeqContains(this.MethodGroup, this.methodGroupSharpnode); break; case SeqTranslator.Drop: translationStyle = new InvocationSeqTakeDrop(false, true, this.MethodGroup, this.methodGroupSharpnode); break; case SeqTranslator.Take: translationStyle = new InvocationSeqTakeDrop(true, false, this.MethodGroup, this.methodGroupSharpnode); break; case SeqTranslator.TakeDrop: translationStyle = new InvocationSeqTakeDrop(true, true, this.MethodGroup, this.methodGroupSharpnode); break; case ContractsTranslator.Result: translationStyle = new InvocationResult(); break; case ContractsTranslator.Folding: translationStyle = new InvocationFoldingUnfolding(true); break; case ContractsTranslator.Unfolding: translationStyle = new InvocationFoldingUnfolding(false); break; default: // Standard case translationStyle = new InvocationStandardMethod(this.MethodGroup, this.methodGroupSharpnode, method); break; } // Perform the translation translationStyle.Run(this.Arguments, this.OriginalNode, context); // Get the result Silvernode silvernode = translationStyle.Silvernode; TranslationResult result = TranslationResult.FromSilvernode(silvernode, translationStyle.Errors).AndPrepend(translationStyle.Prependors.ToArray()); // Maybe prepending is required. translationStyle.PostprocessPurity(result, context); return(result); }
public override TranslationResult Translate(TranslationContext translationContext) { return(TranslationResult.Error(this.OriginalNode, this.diagnostic, this.parameters)); }