| | | 1 | | using NanoCLang.Environemnts; |
| | | 2 | | using System; |
| | | 3 | | |
| | | 4 | | namespace NanoCLang.Entities { |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides a superclass for all basic type objects. |
| | | 7 | | /// </summary> |
| | | 8 | | public abstract class BasicType : Type, ICloneable { |
| | | 9 | | /// <inheritdoc/> |
| | 353 | 10 | | public override BasicType BaseType => this; |
| | | 11 | | /// <inheritdoc/> |
| | 57 | 12 | | public override bool ProperSubType(LocalEnvironment gamma, Type other) { |
| | | 13 | | return other switch { |
| | 50 | 14 | | BasicType o => ProperBasicSubType(o), |
| | | 15 | | // Base type can only be a subtype of a refined type if the refined type has a trivially true refinement |
| | 14 | 16 | | RefinedType o => gamma.With("v", BaseType, gamma => NanoCSMT.Default.TryEvalConstExpression(gamma, o.Ref |
| | 0 | 17 | | _ => throw new InvalidProgramException("Expected basic or refined type here!") |
| | | 18 | | }; |
| | 57 | 19 | | } |
| | 50 | 20 | | private bool ProperBasicSubType(BasicType other) { |
| | 50 | 21 | | switch (BaseType) { |
| | 37 | 22 | | case IntegerType t when other.BaseType is IntegerType o && t.Size == o.Size && t.Values <= o.Values: |
| | 35 | 23 | | VerbConsole.WriteLine(VerbosityLevel.Default, $"<:-Int: {this} <: {other}"); |
| | 35 | 24 | | return true; |
| | 13 | 25 | | case ReferenceType t when other.BaseType is ReferenceType o && t.Location == o.Location && t.Offsets <= o.Of |
| | 11 | 26 | | VerbConsole.WriteLine(VerbosityLevel.Default, $"<:-Ref: {this} <: {other}"); |
| | 11 | 27 | | return true; |
| | 2 | 28 | | case IntegerType t when t.Values == Index.Zero && other.BaseType is ReferenceType o && o.Abstract: |
| | 1 | 29 | | VerbConsole.WriteLine(VerbosityLevel.Default, $"<:-NullPtr: {this} <: {other}"); |
| | 1 | 30 | | return true; |
| | 2 | 31 | | case ReferenceType t when t.Concrete && other.BaseType is ReferenceType o && o.Abstract && (Base)t.Offsets = |
| | 1 | 32 | | VerbConsole.WriteLine(VerbosityLevel.Default, $"<:-Abstract: {this} <: {other}"); |
| | 1 | 33 | | return true; |
| | 2 | 34 | | default: return false; |
| | | 35 | | } |
| | 50 | 36 | | } |
| | | 37 | | /// <inheritdoc/> |
| | | 38 | | public abstract object Clone(); |
| | | 39 | | /// <inheritdoc/> |
| | 260 | 40 | | public override Type Replace(Substitutor rep) => (Type)Clone(); |
| | | 41 | | /// <inheritdoc/> |
| | 3 | 42 | | public override PureExpression Refinement => new BooleanConstant(true); |
| | | 43 | | /// <summary> |
| | | 44 | | /// Calculates the closest related ancestor of both basic type and returns it. |
| | | 45 | | /// Both basic types are a subtype of the return value. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="baseType1">First input type.</param> |
| | | 48 | | /// <param name="baseType2">Second input type.</param> |
| | | 49 | | /// <returns>Supertype of both if they are compatible.</returns> |
| | | 50 | | /// <exception cref="IllFormedException">Incompatible basic types.</exception> |
| | | 51 | | public static BasicType SuperType(BasicType baseType1, BasicType baseType2) |
| | 6 | 52 | | => baseType1 == baseType2 |
| | 6 | 53 | | ? baseType1 |
| | 6 | 54 | | : (baseType1.ProperBasicSubType(baseType2) |
| | 6 | 55 | | ? baseType2 |
| | 6 | 56 | | : (baseType2.ProperBasicSubType(baseType1) |
| | 6 | 57 | | ? baseType1 |
| | 6 | 58 | | : throw new IllFormedException(baseType1, $"Incompatible base types {baseType1} and {baseType2}!"))) |
| | | 59 | | } |
| | | 60 | | } |