< Summary

Class:NanoCLang.Entities.RefinedType
Assembly:NanoCLang
File(s):C:\GitLab-Runner\builds\JxAESPd8\0\chenmichael\nanoc\src\NanoCLang\Entities\RefinedType.cs
Covered lines:44
Uncovered lines:4
Coverable lines:48
Total lines:106
Line coverage:91.6% (44 of 48)
Covered branches:27
Total branches:28
Branch coverage:96.4% (27 of 28)

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%1100%
.ctor(...)100%1100%
.ctor(...)100%2100%
get_RefinementParameter()100%1100%
get_BaseType()100%1100%
get_Refinement()100%1100%
get_Size()100%1100%
WellFormed(...)100%1100%
Tokens()100%12100%
ProperSubType(...)100%2100%
CheckRefinement(...)75%485.71%
CheckRefinement(...)100%2100%
Replace(...)100%1100%
Tokens(...)100%1100%
Equals(...)100%1100%
Equals(...)100%6100%
GetHashCode()100%10%
op_Equality(...)100%10%
op_Inequality(...)100%10%

File(s)

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

#LineLine coverage
 1using NanoCLang.Environemnts;
 2using System;
 3using System.Collections.Generic;
 4
 5namespace NanoCLang.Entities {
 6    /// <summary>
 7    /// Provides a class for refined types.
 8    /// </summary>
 9    public class RefinedType : Type, IEquatable<RefinedType?> {
 10        /// <summary>
 11        /// Gets the default value that the refinement parameter uses.
 12        /// </summary>
 13        private const string DEFAULT_NAME = "v";
 14        /// <summary>
 15        /// Creates a new instance of a refined type that refines the <paramref name="baseType"/> with the <paramref nam
 16        /// </summary>
 17        /// <param name="name">Name of the free variable that is used in the refinement.</param>
 18        /// <param name="baseType">Base type that is refined.</param>
 19        /// <param name="refinement">Refinement expression.</param>
 274620        public RefinedType(string name, BasicType baseType, PureExpression refinement) {
 137321            RefinementParameter = name;
 137322            BaseType = baseType;
 137323            Refinement = refinement;
 137324        }
 25        /// <summary>
 26        /// Creates a new refined type from the <paramref name="refinement"/> function that gives the refinement with re
 27        /// </summary>
 28        /// <param name="baseType">Base type that is refined.</param>
 29        /// <param name="refinement">Refinement function.</param>
 30        /// <param name="refineVar">Name of the free variable that is used in the refinement.</param>
 31        public RefinedType(BasicType baseType, Func<VariableExpression, PureExpression> refinement, string refineVar = D
 141332            : this(refineVar, baseType, refinement(new VariableExpression(refineVar))) { }
 33        /// <summary>
 34        /// Creates a new refined type, that further refines the initial <paramref name="type"/>, from the <paramref nam
 35        /// </summary>
 36        /// <param name="type">Refined type that is further refined.</param>
 37        /// <param name="refinement">Refinement function.</param>
 38        /// <param name="refineVar">Name of the free variable that is used in the refinement.</param>
 39        public RefinedType(Type type, Func<VariableExpression, PureExpression> refinement, string refineVar = DEFAULT_NA
 62440            : this(refineVar, type.BaseType, type is RefinedType
 62441                  ? refinement(new VariableExpression(refineVar)) & type.Refinement
 187242                  : refinement(new VariableExpression(refineVar))) { }
 43        /// <summary>
 44        /// Name of the free variable that is used in the refinement.
 45        /// </summary>
 127946        public string RefinementParameter { get; } // Constant v
 47        /// <summary>
 48        /// Base type that is refined.
 49        /// </summary>
 303850        public override BasicType BaseType { get; }
 51        /// <summary>
 52        /// Refinement expression of the <see cref="RefinementParameter"/>.
 53        /// </summary>
 213454        public override PureExpression Refinement { get; }
 55        /// <inheritdoc/>
 13556        public override int Size => BaseType.Size;
 57        /// <inheritdoc/>
 11958        public override void WellFormed(LocalEnvironment gamma, Heap heap) {
 11959            VerbConsole.WriteLine(VerbosityLevel.Default, $"WF-Type: {this}");
 11960            BaseType.WellFormed(gamma, heap);
 23861            gamma.With(RefinementParameter, BaseType, gamma => Refinement.WellFormed(gamma));
 11962        }
 63        /// <inheritdoc/>
 26564        public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) {
 26565            yield return "{";
 26566            yield return RefinementParameter;
 53067            if (args.SpaceBeforeTypeOperator) yield return " ";
 26568            yield return ":";
 53069            if (args.SpaceAfterTypeOperator) yield return " ";
 423970            foreach (var tk in BaseType.Tokens(args)) yield return tk;
 53071            if (args.SpaceBeforeRefineOperator) yield return " ";
 26572            yield return "|";
 53073            if (args.SpaceAfterRefineOperator) yield return " ";
 1069574            foreach (var tk in Refinement.Tokens(args)) yield return tk;
 26575            yield return "}";
 26576        }
 77        /// <inheritdoc/>
 14978        public override bool ProperSubType(LocalEnvironment gamma, Type other) => BaseType.SubType(gamma, other.BaseType
 14879        private bool CheckRefinement(LocalEnvironment gamma, Type other) {
 14880            return other switch {
 2981                BasicType _ => true,
 11982                RefinedType refined => CheckRefinement(gamma, refined),
 083                _ => throw new InvalidProgramException("Expected basic type or refined type here!")
 14884            };
 14885        }
 11986        private bool CheckRefinement(LocalEnvironment gamma, RefinedType refined) => Refinement == refined.Refinement ||
 23687            gamma => NanoCSMT.Default.ValidImplication(gamma, Refinement, refined.Refinement));
 88        /// <inheritdoc/>
 89        public override Type Replace(Substitutor sub)
 14790            => new RefinedType(RefinementParameter, (BasicType)BaseType.Replace(sub), Refinement.Replace(sub));
 91        /// <inheritdoc/>
 8592        public override IEnumerable<StringFormatterToken> Tokens(CSourceFormat args) => BaseType.Tokens(args);
 93        #region Equality checks
 94        /// <inheritdoc/>
 23795        public override bool Equals(object? obj) => Equals(obj as RefinedType);
 96        /// <inheritdoc/>
 23797        public bool Equals(RefinedType? other) => !(other is null) && RefinementParameter == other.RefinementParameter &
 98        /// <inheritdoc/>
 099        public override int GetHashCode() => System.HashCode.Combine(RefinementParameter, BaseType, Refinement);
 100        /// <inheritdoc/>
 0101        public static bool operator ==(RefinedType? left, RefinedType? right) => EqualityComparer<RefinedType?>.Default.
 102        /// <inheritdoc/>
 0103        public static bool operator !=(RefinedType? left, RefinedType? right) => !(left == right);
 104        #endregion
 105    }
 106}