tryPort method
- String portRefString
Tries to create a port reference from a string representation with advanced parsing.
This method handles both simple ports and complex port references including:
- Simple port names: "clk", "reset_n"
- Bit ranges and indices: "data
7:0", "addr5" - Struct member access: "mystruct.field" (using structMap lookup)
- Renamed ports: automatically resolves to original port names
For struct ports (containing '.'), the method looks up the actual bit slice in structMap. For renamed ports, it automatically resolves to the original port name while preserving any range specification.
Returns a PortReference that can be used for connections and operations,
or null if the referenced port access is valid but the port does not
exist.
Throws an Exception if portRefString is not a valid port access
string.
Implementation
PortReference? tryPort(String portRefString) {
if (portRefString.contains('.')) {
return structMap[portRefString];
}
final portRefComponents =
SlicePortReference.extractPortAccessSliceComponents(portRefString);
final portName = portRefComponents.portName;
final resolvedPortName = renamedPorts[portName] ?? portName;
if (tryInput(resolvedPortName) == null &&
tryOutput(resolvedPortName) == null &&
tryInOut(resolvedPortName) == null) {
return null;
}
if (renamedPorts.containsKey(portName)) {
return PortReference.fromString(
this, portRefString.replaceFirst(portName, resolvedPortName));
}
return PortReference.fromString(this, portRefString);
}