public override void Run(List <ExpressionSharpnode> arguments, SyntaxNode originalNode, TranslationContext context) { var expressions = ConvertToSilver(arguments, context); Silvernode = new SimpleSequenceSilvernode(originalNode, _isFolding ? "folding" : "unfolding", " ", expressions[0], " in ", expressions[1] ); }
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."); }
/// <summary> /// Generates the Viper text that contains fields and the initializer of this class. /// </summary> /// <param name="process">The TranslationProcess.</param> public Silvernode GenerateGlobalSilvernode(TranslationProcess process) { var node = new SimpleSequenceSilvernode(null); // Translate fields foreach (CollectedField field in this.InstanceFields) { node.List.Add("field "); node.List.Add(new IdentifierSilvernode(field.Name)); node.List.Add(": "); node.List.Add(new TypeSilvernode(null, field.SilverType)); if (this.InstanceFields[this.InstanceFields.Count - 1] != field) { node.List.Add("\n"); } } // Translate the initializer/default constructor if (!this._isStatic) { Identifier initializer = process.IdentifierTranslator.RegisterAndGetIdentifierWithTag(this.ClassSymbol, Constants.InitializerTag); // The initializer gives write access to all fields var accessToAllFields = new List <ContractSilvernode>(); foreach (CollectedField field in this.InstanceFields) { var protectedField = new SimpleSequenceSilvernode(null, Constants.SilverThis, ".", new IdentifierSilvernode(field.Name) ); accessToAllFields.Add(new EnsuresSilvernode(new AccSilvernode(protectedField, "write", null), null)); } var initializerContents = new BlockSilvernode(null, new List <StatementSilvernode>()); initializerContents.Add(new AssignmentSilvernode(Constants.SilverThis, new NewStarSilvernode(null), null)); var initializerMethod = new MethodSilvernode(null, new IdentifierSilvernode(initializer), new List <ParameterSilvernode>(), Constants.SilverThis, new TypeSilvernode(null, SilverType.Ref), accessToAllFields, initializerContents); node.List.Add("\n"); node.List.Add(initializerMethod); } return(node); }
public static TranslationResult Constructor(List <ExpressionSharpnode> arguments, TranslationContext context, ITypeSymbol typeArgument, SyntaxNode originalNode) { var silvernodes = new List <Silvernode>(); var errors = new List <Error>(); List <StatementSilvernode> prepend = new List <Trees.Silver.StatementSilvernode>(); // Translate initial members foreach (var arg in arguments) { var res = arg.Translate(context.ChangePurityContext(PurityContext.Purifiable)); silvernodes.Add(res.Silvernode); errors.AddRange(res.Errors); prepend.AddRange(res.PrependTheseSilvernodes); } Silvernode result; if (arguments.Count == 0) { // No arguments = use the Seq[Int] construction Error err; SilverType silverType = TypeTranslator.TranslateType(typeArgument, null, out err); if (err != null) { errors.Add(err); } result = new SimpleSequenceSilvernode(originalNode, "Seq[", new TypeSilvernode(null, silverType), "]()"); } else { // Some arguments - use the Seq construction with type inference // ReSharper disable once UseObjectOrCollectionInitializer List <Silvernode> args = new List <Silvernode>(); args.Add("Seq("); for (int i = 0; i < silvernodes.Count; i++) { args.Add(silvernodes[i]); if (i != silvernodes.Count - 1) { args.Add(", "); } } args.Add(")"); result = new SimpleSequenceSilvernode(originalNode, args.ToArray()); } return(TranslationResult.FromSilvernode(result, errors).AndPrepend(prepend)); }