| | | 1 | | using NanoCLang.Environemnts; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | |
| | | 5 | | namespace NanoCLang.Entities { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides a class for NanoC reference types. |
| | | 8 | | /// </summary> |
| | | 9 | | public class ReferenceType : BasicType, IEquatable<ReferenceType?> { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Creates a new instance of a reference type. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="location">Location that the reference refers to.</param> |
| | | 14 | | /// <param name="offsets">Offset at this <paramref name="location"/>.</param> |
| | 442 | 15 | | public ReferenceType(Location location, Index offsets) { |
| | 221 | 16 | | Location = location; |
| | 221 | 17 | | Offsets = offsets; |
| | 221 | 18 | | } |
| | | 19 | | /// <summary> |
| | | 20 | | /// Location that the type refers to. |
| | | 21 | | /// </summary> |
| | 845 | 22 | | public Location Location { get; } |
| | | 23 | | /// <summary> |
| | | 24 | | /// Offset of the reference at the given <see cref="Location"/>. |
| | | 25 | | /// </summary> |
| | 730 | 26 | | public Index Offsets { get; } |
| | | 27 | | /// <inheritdoc/> |
| | 46 | 28 | | public override int Size => PointerSize; |
| | | 29 | | /// <inheritdoc cref="Size"/> |
| | 47 | 30 | | public static int PointerSize { get; set; } = 8; |
| | | 31 | | /// <inheritdoc/> |
| | 3 | 32 | | public override object Clone() => new ReferenceType((Location)Location.Clone(), (Index)Offsets.Clone()); |
| | | 33 | | /// <inheritdoc/> |
| | | 34 | | public override Type Replace(Substitutor sub) |
| | 73 | 35 | | => new ReferenceType(Location.Replace(sub), Offsets.Replace(sub)); |
| | | 36 | | /// <summary> |
| | | 37 | | /// <c>true</c> iff the reference points to an abstract location, else <see langword="false"/>. |
| | | 38 | | /// </summary> |
| | 36 | 39 | | public bool Abstract => Location.Abstract; |
| | | 40 | | /// <summary> |
| | | 41 | | /// <c>true</c> iff the reference points to an concrete location, else <see langword="false"/>. |
| | | 42 | | /// </summary> |
| | 9 | 43 | | public bool Concrete => Location.Concrete; |
| | | 44 | | /// <inheritdoc/> |
| | 51 | 45 | | public override void WellFormed(LocalEnvironment gamma, Heap heap) { |
| | 51 | 46 | | VerbConsole.WriteLine(VerbosityLevel.Default, $"WF-Ref: {this}"); |
| | 51 | 47 | | if (!heap.ContainsAbstract(Location.Name)) |
| | 0 | 48 | | throw new IllFormedException(Location, $"Cannot refer to invalid location {Location}!"); |
| | 51 | 49 | | } |
| | | 50 | | /// <inheritdoc/> |
| | 190 | 51 | | public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) { |
| | 190 | 52 | | yield return "ref"; |
| | 190 | 53 | | if (args.SpaceAfterFunctionName) yield return " "; |
| | 190 | 54 | | yield return "("; |
| | 1602 | 55 | | foreach (var tk in Location.Tokens(args)) yield return tk; |
| | 190 | 56 | | yield return args.ParameterListSeparator; |
| | 2016 | 57 | | foreach (var tk in Offsets.Tokens(args)) yield return tk; |
| | 190 | 58 | | yield return ")"; |
| | 190 | 59 | | } |
| | | 60 | | /// <inheritdoc/> |
| | 47 | 61 | | public override IEnumerable<StringFormatterToken> Tokens(CSourceFormat args) { |
| | 47 | 62 | | yield return "int8_t"; |
| | 47 | 63 | | yield return "*"; |
| | 47 | 64 | | } |
| | | 65 | | #region Equality checks |
| | | 66 | | /// <inheritdoc/> |
| | 175 | 67 | | public override bool Equals(object? obj) => Equals(obj as ReferenceType); |
| | | 68 | | /// <inheritdoc/> |
| | 175 | 69 | | public bool Equals(ReferenceType? other) => !(other is null) && EqualityComparer<Location>.Default.Equals(Locati |
| | | 70 | | /// <inheritdoc/> |
| | 0 | 71 | | public override int GetHashCode() => HashCode.Combine(Location, Offsets); |
| | | 72 | | /// <inheritdoc/> |
| | 0 | 73 | | public static bool operator ==(ReferenceType? left, ReferenceType? right) => EqualityComparer<ReferenceType?>.De |
| | | 74 | | /// <inheritdoc/> |
| | 0 | 75 | | public static bool operator !=(ReferenceType? left, ReferenceType? right) => !(left == right); |
| | | 76 | | #endregion |
| | | 77 | | } |
| | | 78 | | } |