1 //
2 // Copyright (c) 2010-2019 Antmicro
3 //
4 // This file is licensed under the MIT License.
5 // Full license text is available in 'licenses/MIT.txt'.
6 //
7 
8 using System.Collections.Generic;
9 using System.Linq;
10 
11 namespace Antmicro.Renode.PlatformDescription.Syntax
12 {
13     public sealed class IrqAttribute : Attribute
14     {
IrqAttribute(IEnumerable<SingleOrMultiIrqEnd> sources, IEnumerable<IrqDestinations> destinations)15         public IrqAttribute(IEnumerable<SingleOrMultiIrqEnd> sources, IEnumerable<IrqDestinations> destinations)
16         {
17             Sources = sources;
18             Destinations = destinations;
19         }
20 
SingleAttributeWithInheritedPosition(SingleOrMultiIrqEnd source, IrqReceiver destinationPeripheral, SingleOrMultiIrqEnd destination)21         public IrqAttribute SingleAttributeWithInheritedPosition(SingleOrMultiIrqEnd source, IrqReceiver destinationPeripheral, SingleOrMultiIrqEnd destination)
22         {
23             var copy = SerializationProvider.Instance.DeepClone(this);
24             copy.Sources = new[] { source };
25             copy.Destinations = new[] { new IrqDestinations(destinationPeripheral, new[] { destination }) };
26             return copy;
27         }
28 
SetDefaultSource(string propertyName)29         public void SetDefaultSource(string propertyName)
30         {
31             Sources = new[] { new SingleOrMultiIrqEnd(new[] { new IrqEnd(propertyName, 0) }) };
32         }
33 
ToString()34         public override string ToString()
35         {
36             if(Sources == null)
37             {
38                 return "";
39             }
40             return $"{Sources.Select(x => x.ToString()).Aggregate((x, y) => x + "," + y)} -> {PrettyPrintDestinations(Destinations)}";
41         }
42 
Visit()43         public override IEnumerable<object> Visit()
44         {
45             var sourceOrEmpty = Sources ?? Enumerable.Empty<SingleOrMultiIrqEnd>();
46             if(Destinations != null)
47             {
48                 return sourceOrEmpty.Cast<object>().Concat(Destinations);
49             }
50             return sourceOrEmpty;
51         }
52 
53         public IEnumerable<SingleOrMultiIrqEnd> Sources { get; private set; }
54         public IEnumerable<IrqDestinations> Destinations { get; private set; }
55 
PrettyPrintDestinations(IEnumerable<IrqDestinations> destinations)56         private static string PrettyPrintDestinations(IEnumerable<IrqDestinations> destinations)
57         {
58             if(destinations.Count() == 1)
59             {
60                 return destinations.ToString();
61             }
62             return destinations.Select(x => x.ToString()).Aggregate((x, y) => x + " | " + y);
63         }
64     }
65 }
66