f676661acf5a8d5ec22ad703a544b9558d7aed88
[Mograsim.git] /
1 package net.mograsim.logic.model.verilog.model.statements;
2
3 import java.util.List;
4 import java.util.Objects;
5 import java.util.Set;
6 import java.util.stream.Collectors;
7
8 import net.mograsim.logic.model.verilog.model.VerilogComponentDeclaration;
9 import net.mograsim.logic.model.verilog.model.signals.IOPort;
10 import net.mograsim.logic.model.verilog.model.signals.Signal;
11
12 public class ComponentReference extends Statement
13 {
14         private final String name;
15         private final VerilogComponentDeclaration referencedComponent;
16         private final List<Signal> arguments;
17
18         public ComponentReference(String name, VerilogComponentDeclaration referencedComponent, List<Signal> arguments)
19         {
20                 this.name = Objects.requireNonNull(name);
21                 this.referencedComponent = Objects.requireNonNull(referencedComponent);
22                 this.arguments = List.copyOf(arguments);
23
24                 check();
25         }
26
27         private void check()
28         {
29                 List<IOPort> ioPorts = referencedComponent.getIOPorts();
30                 if (ioPorts.size() != arguments.size())
31                         throw new IllegalArgumentException(
32                                         "Incorrect nubmer of arguments given: " + arguments.size() + ", but should be " + ioPorts.size());
33
34                 for (int i = 0; i < ioPorts.size(); i++)
35                         if (ioPorts.get(i).getWidth() != arguments.get(i).getWidth())
36                                 throw new IllegalArgumentException("Argument #" + i + "(" + ioPorts.get(i) + "): Incorrect width: "
37                                                 + arguments.get(i).getWidth() + ", but shoud be " + ioPorts.get(i).getWidth());
38         }
39
40         public String getName()
41         {
42                 return name;
43         }
44
45         public VerilogComponentDeclaration getReferencedComponent()
46         {
47                 return referencedComponent;
48         }
49
50         public List<Signal> getArguments()
51         {
52                 return arguments;
53         }
54
55         @Override
56         public String toVerilogCode()
57         {
58                 StringBuilder sb = new StringBuilder();
59
60                 sb.append(referencedComponent.getID() + " " + name);
61                 sb.append(arguments.stream().map(Signal::toReferenceVerilogCode).collect(Collectors.joining(", ", "(", ")")));
62                 sb.append(";");
63
64                 return sb.toString();
65         }
66
67         @Override
68         public Set<String> getDefinedNames()
69         {
70                 return Set.of(name);
71         }
72
73         @Override
74         public Set<Signal> getDefinedSignals()
75         {
76                 return Set.of();
77         }
78
79         @Override
80         public Set<Signal> getReferencedSignals()
81         {
82                 return Set.copyOf(arguments);
83         }
84
85         @Override
86         public String toString()
87         {
88                 return name + "[" + referencedComponent.getID() + "]";
89         }
90
91         @Override
92         public int hashCode()
93         {
94                 final int prime = 31;
95                 int result = 1;
96                 result = prime * result + ((arguments == null) ? 0 : arguments.hashCode());
97                 result = prime * result + ((name == null) ? 0 : name.hashCode());
98                 result = prime * result + ((referencedComponent == null) ? 0 : referencedComponent.hashCode());
99                 return result;
100         }
101
102         @Override
103         public boolean equals(Object obj)
104         {
105                 if (this == obj)
106                         return true;
107                 if (obj == null)
108                         return false;
109                 if (getClass() != obj.getClass())
110                         return false;
111                 ComponentReference other = (ComponentReference) obj;
112                 if (arguments == null)
113                 {
114                         if (other.arguments != null)
115                                 return false;
116                 } else if (!arguments.equals(other.arguments))
117                         return false;
118                 if (name == null)
119                 {
120                         if (other.name != null)
121                                 return false;
122                 } else if (!name.equals(other.name))
123                         return false;
124                 if (referencedComponent == null)
125                 {
126                         if (other.referencedComponent != null)
127                                 return false;
128                 } else if (!referencedComponent.equals(other.referencedComponent))
129                         return false;
130                 return true;
131         }
132 }