< Summary

Class:NanoCLang.Entities.AllocationExpression
Assembly:NanoCLang
File(s):C:\GitLab-Runner\builds\JxAESPd8\0\chenmichael\nanoc\src\NanoCLang\Entities\Expression\AllocationExpression.cs
Covered lines:51
Uncovered lines:5
Coverable lines:56
Total lines:92
Line coverage:91% (51 of 56)
Covered branches:21
Total branches:28
Branch coverage:75% (21 of 28)

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%1100%
get_Location()100%1100%
get_Size()100%1100%
DoInferWorld(...)100%895.45%
Tokens()75%4100%
Tokens()66.66%12100%
RequiredFunctions()100%10%
Equals(...)100%1100%
Equals(...)50%4100%
GetHashCode()100%10%
op_Equality(...)100%10%
op_Inequality(...)100%10%

File(s)

C:\GitLab-Runner\builds\JxAESPd8\0\chenmichael\nanoc\src\NanoCLang\Entities\Expression\AllocationExpression.cs

#LineLine coverage
 1using NanoCLang.Environemnts;
 2using System;
 3using System.Collections.Generic;
 4using System.Linq;
 5
 6namespace NanoCLang.Entities {
 7    /// <summary>
 8    /// Provides a class for allocation expressions.
 9    /// </summary>
 10    public class AllocationExpression : Expression, IEquatable<AllocationExpression?> {
 11        /// <summary>
 12        /// Creates a new instance of an allocation expression that allocates the <paramref name="size"/> in bytes at th
 13        /// </summary>
 14        /// <param name="location">Location that should be allocated in.</param>
 15        /// <param name="size">Size in bytes of the allocation.</param>
 4216        public AllocationExpression(string location, PureExpression size) {
 2117            Location = location;
 2118            Size = size;
 2119        }
 20        /// <summary>
 21        /// Location that is to be allocated.
 22        /// </summary>
 5223        public string Location { get; }
 24        /// <summary>
 25        /// Size in bytes of the allocated block.
 26        /// </summary>
 5527        public PureExpression Size { get; }
 28        /// <inheritdoc/>
 1329        protected override World DoInferWorld(GlobalEnvironment phi, LocalEnvironment gamma, Heap heap) {
 1330            var labs = new Location(Location, true);
 1331            var lj = new Location(Location, false);
 1332            if (!heap.TryGetBinding(labs, out var b))
 133                throw new IllFormedException(this, $"Allocation invalid: heap does not contain abstract location binding
 1234            if (heap.TryGetBinding(lj, out _))
 135                throw new IllFormedException(this, $"Allocation invalid: abstract location {labs} was already unfolded t
 1136            var st = Size.InferType(gamma);
 1137            if (!(st.BaseType is IntegerType s))
 138                throw new IllFormedException(Size, $"Allocation invalid: expected integral type size but got {st.BaseTyp
 2039            if (!st.SubType(gamma, new RefinedType(new IntegerType(s.Size), v => v > new IntegerConstant(0, s.Size))))
 240                throw new IllFormedException(Size, $"Allocation invalid: size must be a positive integer!");
 41            Heap hout;
 842            try {
 1943                hout = heap * new LocationBinding(lj, b.Select(i => i.BaseType).ToArray());
 844            } catch (ArgumentException e) {
 045                throw new IllFormedException(this, $"Cannot concatenate heaps: {e.Message}!");
 46            }
 847            return new World(
 848                new RefinedType(new ReferenceType(lj, new SingletonIndex(0)),
 849                    v => new UninterpretedApplicationExpression("Safe", v)
 850                    & new UninterpretedApplicationExpression("BLen", v, Size)),
 851                hout);
 852        }
 53        /// <inheritdoc/>
 854        public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) {
 855            yield return "malloc";
 856            if (args.SpaceAfterFunctionName) yield return " ";
 857            yield return "(";
 858            yield return Location;
 859            yield return args.ParameterListSeparator;
 7260            foreach (var tk in Size.Tokens(args)) yield return tk;
 861            yield return ")";
 862        }
 63        /// <inheritdoc/>
 764        public override IEnumerable<StringFormatterToken> Tokens(CSourceFormat args) {
 765            if (FixedWorld is null) throw new InvalidOperationException("Expression must have fixed world when translati
 766            if (args.PureRequiresReturn) yield return "return ";
 767            yield return "((";
 6368            foreach (var tk in FixedWorld.Type.Tokens(args)) yield return tk;
 769            yield return ")";
 770            yield return "malloc";
 771            if (args.SpaceAfterFunctionName) yield return " ";
 772            yield return "(";
 7873            foreach (var tk in Size.Tokens(args)) yield return tk;
 774            yield return "))";
 775            if (args.PureRequiresReturn) yield return ";";
 776        }
 77        /// <inheritdoc/>
 078        public override IEnumerable<string> RequiredFunctions() { yield break; }
 79        #region Equality checks
 80        /// <inheritdoc/>
 981        public override bool Equals(object? obj) => Equals(obj as AllocationExpression);
 82        /// <inheritdoc/>
 983        public bool Equals(AllocationExpression? other) => !(other is null) && Location == other.Location && EqualityCom
 84        /// <inheritdoc/>
 085        public override int GetHashCode() => HashCode.Combine(Location, Size);
 86        /// <inheritdoc/>
 087        public static bool operator ==(AllocationExpression? left, AllocationExpression? right) => EqualityComparer<Allo
 88        /// <inheritdoc/>
 089        public static bool operator !=(AllocationExpression? left, AllocationExpression? right) => !(left == right);
 90        #endregion
 91    }
 92}