Skip to content

Commit

Permalink
Support common protocols with SVD types (#109)
Browse files Browse the repository at this point in the history
Adds mostly synthesized conformance to a few common Swift "currency"
protocols so SVD type can be more easily used outside the SVD module.
Specially this commit makes all SVD types conform to the `Decodable`,
`Encodable`, `Equatable`, and `Hashable` protocols.
  • Loading branch information
rauhul authored Aug 9, 2024
1 parent 314dbd2 commit ffa2b75
Show file tree
Hide file tree
Showing 41 changed files with 482 additions and 47 deletions.
8 changes: 8 additions & 0 deletions Sources/SVD/Models/SVDAccess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public enum SVDAccess {
case readWriteOnce
}

extension SVDAccess: Decodable {}

extension SVDAccess: Encodable {}

extension SVDAccess: Equatable {}

extension SVDAccess: Hashable {}

extension SVDAccess: XMLNodeInitializable {
init(_ node: XMLNode) throws {
let stringValue = try String(node)
Expand Down
8 changes: 8 additions & 0 deletions Sources/SVD/Models/SVDAddressBlock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ public struct SVDAddressBlock {
/// Set the protection level for an address block.
public var protection: SVDProtection?
}

extension SVDAddressBlock: Decodable {}

extension SVDAddressBlock: Encodable {}

extension SVDAddressBlock: Equatable {}

extension SVDAddressBlock: Hashable {}
16 changes: 12 additions & 4 deletions Sources/SVD/Models/SVDAddressBlockUsage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,27 @@ import Foundation
import FoundationXML
#endif

public enum SVDAddressBlockUsage {
public enum SVDAddressBlockUsage: String {
case registers
case buffer
case reserved
}

extension SVDAddressBlockUsage: Decodable {}

extension SVDAddressBlockUsage: Encodable {}

extension SVDAddressBlockUsage: Equatable {}

extension SVDAddressBlockUsage: Hashable {}

extension SVDAddressBlockUsage: XMLNodeInitializable {
init(_ node: XMLNode) throws {
let stringValue = try String(node)
switch stringValue {
case "registers": self = .registers
case "buffer": self = .buffer
case "reserved": self = .reserved
case Self.registers.rawValue: self = .registers
case Self.buffer.rawValue: self = .buffer
case Self.reserved.rawValue: self = .reserved
// FIXME: esp8266.svd
// FIXME: esp32.svd
// These SVDs have invalid usage strings
Expand Down
8 changes: 8 additions & 0 deletions Sources/SVD/Models/SVDBitRange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ public enum SVDBitRange {
case literal(SVDBitRangeLiteralContainer)
}

extension SVDBitRange: Decodable {}

extension SVDBitRange: Encodable {}

extension SVDBitRange: Equatable {}

extension SVDBitRange: Hashable {}

extension SVDBitRange: XMLElementInitializable {
init(_ element: XMLElement) throws {
if let value = try? SVDBitRangeLsbMsb(element) {
Expand Down
14 changes: 9 additions & 5 deletions Sources/SVD/Models/SVDBitRangeLiteral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ import MMIOUtilities
import FoundationXML
#endif

@XMLElement
public struct SVDBitRangeLiteralContainer {
public var bitRange: SVDBitRangeLiteral
}

/// A string in the format: "[<msb>:<lsb>]"
public struct SVDBitRangeLiteral {
public var lsb: UInt64
Expand All @@ -31,6 +26,15 @@ extension SVDBitRangeLiteral: CustomStringConvertible {
public var description: String { "[\(self.msb):\(self.lsb)]" }
}

// FIXME: encode/decode as single value
extension SVDBitRangeLiteral: Decodable {}

extension SVDBitRangeLiteral: Encodable {}

extension SVDBitRangeLiteral: Equatable {}

extension SVDBitRangeLiteral: Hashable {}

extension SVDBitRangeLiteral: LosslessStringConvertible {
public init?(_ description: String) {
var description = description[...]
Expand Down
30 changes: 30 additions & 0 deletions Sources/SVD/Models/SVDBitRangeLiteralContainer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===----------------------------------------------------------*- swift -*-===//
//
// This source file is part of the Swift MMIO open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

import Foundation
import MMIOUtilities

#if canImport(FoundationXML)
import FoundationXML
#endif

@XMLElement
public struct SVDBitRangeLiteralContainer {
public var bitRange: SVDBitRangeLiteral
}

extension SVDBitRangeLiteralContainer: Decodable {}

extension SVDBitRangeLiteralContainer: Encodable {}

extension SVDBitRangeLiteralContainer: Equatable {}

extension SVDBitRangeLiteralContainer: Hashable {}
8 changes: 8 additions & 0 deletions Sources/SVD/Models/SVDBitRangeLsbMsb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ public struct SVDBitRangeLsbMsb {
public var lsb: UInt64
public var msb: UInt64
}

extension SVDBitRangeLsbMsb: Decodable {}

extension SVDBitRangeLsbMsb: Encodable {}

extension SVDBitRangeLsbMsb: Equatable {}

extension SVDBitRangeLsbMsb: Hashable {}
8 changes: 8 additions & 0 deletions Sources/SVD/Models/SVDBitRangeOffsetWidth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ public struct SVDBitRangeOffsetWidth {
public var bitOffset: UInt64
public var bitWidth: UInt64?
}

extension SVDBitRangeOffsetWidth: Decodable {}

extension SVDBitRangeOffsetWidth: Encodable {}

extension SVDBitRangeOffsetWidth: Equatable {}

extension SVDBitRangeOffsetWidth: Hashable {}
8 changes: 8 additions & 0 deletions Sources/SVD/Models/SVDCPU.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,11 @@ public struct SVDCPU {
/// the settings are described here.
public var sauRegionsConfig: SVDSAURegions?
}

extension SVDCPU: Decodable {}

extension SVDCPU: Encodable {}

extension SVDCPU: Equatable {}

extension SVDCPU: Hashable {}
8 changes: 8 additions & 0 deletions Sources/SVD/Models/SVDCPUEndianness.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ public enum SVDCPUEndianness: String {
case other
}

extension SVDCPUEndianness: Decodable {}

extension SVDCPUEndianness: Encodable {}

extension SVDCPUEndianness: Equatable {}

extension SVDCPUEndianness: Hashable {}

extension SVDCPUEndianness: XMLNodeInitializable {}
61 changes: 56 additions & 5 deletions Sources/SVD/Models/SVDCPUName.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,59 @@ public enum SVDCPUName {
case other(String)
}

extension SVDCPUName: XMLNodeInitializable {
init(_ node: XMLNode) throws {
let stringValue = try String(node)
switch stringValue {
extension SVDCPUName: CustomStringConvertible {
public var description: String {
switch self {
case .armCortexM0: "CM0"
case .armCortexM0p: "CM0+"
case .armCortexM1: "CM1"
case .armCortexM3: "CM3"
case .armCortexM4: "CM4"
case .armCortexM7: "CM7"
case .armCortexM23: "CM23"
case .armCortexM33: "CM33"
case .armCortexM35P: "CM35P"
case .armCortexM55: "CM55"
case .armCortexM85: "CM85"
case .armSecureCoreSC000: "SC000"
case .armSecureCoreSC300: "SC300"
case .armCortexA5: "CA5"
case .armCortexA7: "CA7"
case .armCortexA8: "CA8"
case .armCortexA9: "CA9"
case .armCortexA15: "CA15"
case .armCortexA17: "CA17"
case .armCortexA53: "CA53"
case .armCortexA57: "CA57"
case .armCortexA72: "CA72"
case .armChinaSTARMC1: "SMC1"
case .other(let description): description
}
}
}

extension SVDCPUName: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let description = try container.decode(String.self)
self = Self(description)
}
}

extension SVDCPUName: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self.description)
}
}

extension SVDCPUName: Equatable {}

extension SVDCPUName: Hashable {}

extension SVDCPUName: LosslessStringConvertible {
public init(_ description: String) {
switch description {
case "CM0": self = .armCortexM0
case "CM0PLUS", "CM0+": self = .armCortexM0p
case "CM1": self = .armCortexM1
Expand All @@ -93,7 +142,9 @@ extension SVDCPUName: XMLNodeInitializable {
case "CA57": self = .armCortexA57
case "CA72": self = .armCortexA72
case "SMC1": self = .armChinaSTARMC1
default: self = .other(stringValue)
default: self = .other(description)
}
}
}

extension SVDCPUName: XMLNodeInitializable {}
4 changes: 4 additions & 0 deletions Sources/SVD/Models/SVDCPURevision.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ extension SVDCPURevision: Encodable {
}
}

extension SVDCPURevision: Equatable {}

extension SVDCPURevision: Hashable {}

extension SVDCPURevision: LosslessStringConvertible {
public init?(_ description: String) {
// Some SVD files use a single int value instead of proper revision, parse
Expand Down
8 changes: 8 additions & 0 deletions Sources/SVD/Models/SVDCluster.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,11 @@ public struct SVDCluster {
/// Define the sequence of registers.
public var register: [SVDRegister]?
}

extension SVDCluster: Decodable {}

extension SVDCluster: Encodable {}

extension SVDCluster: Equatable {}

extension SVDCluster: Hashable {}
8 changes: 8 additions & 0 deletions Sources/SVD/Models/SVDDataType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ public enum SVDDataType: String {
case int64Pointer = "int64_t *"
}

extension SVDDataType: Decodable {}

extension SVDDataType: Encodable {}

extension SVDDataType: Equatable {}

extension SVDDataType: Hashable {}

extension SVDDataType: XMLNodeInitializable {}
8 changes: 8 additions & 0 deletions Sources/SVD/Models/SVDDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,11 @@ public struct SVDDevice {
// /// the silicon vendor to specify a schema for this section.
// var vendorExtensions: [AnyHashable: Any]
}

extension SVDDevice: Decodable {}

extension SVDDevice: Encodable {}

extension SVDDevice: Equatable {}

extension SVDDevice: Hashable {}
8 changes: 8 additions & 0 deletions Sources/SVD/Models/SVDDimensionArrayIndex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ public struct SVDDimensionArrayIndex {
/// Specify the values contained in the enumeration.
public var enumeratedValue: [SVDEnumerationCase]
}

extension SVDDimensionArrayIndex: Decodable {}

extension SVDDimensionArrayIndex: Encodable {}

extension SVDDimensionArrayIndex: Equatable {}

extension SVDDimensionArrayIndex: Hashable {}
8 changes: 8 additions & 0 deletions Sources/SVD/Models/SVDDimensionElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ public struct SVDDimensionElement {
/// Grouping element to create enumerations in the header file.
public var dimArrayIndex: SVDDimensionArrayIndex?
}

extension SVDDimensionElement: Decodable {}

extension SVDDimensionElement: Encodable {}

extension SVDDimensionElement: Equatable {}

extension SVDDimensionElement: Hashable {}
8 changes: 8 additions & 0 deletions Sources/SVD/Models/SVDEnumeration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,11 @@ public struct SVDEnumeration {
/// field.
public var enumeratedValue: [SVDEnumerationCase]
}

extension SVDEnumeration: Decodable {}

extension SVDEnumeration: Encodable {}

extension SVDEnumeration: Equatable {}

extension SVDEnumeration: Hashable {}
8 changes: 8 additions & 0 deletions Sources/SVD/Models/SVDEnumerationCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ public struct SVDEnumerationCase {
@XMLInlineElement
public var data: SVDEnumerationCaseData
}

extension SVDEnumerationCase: Decodable {}

extension SVDEnumerationCase: Encodable {}

extension SVDEnumerationCase: Equatable {}

extension SVDEnumerationCase: Hashable {}
9 changes: 9 additions & 0 deletions Sources/SVD/Models/SVDEnumerationCaseData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ extension SVDEnumerationCaseData: XMLElementInitializable {
}
}
}

// FIXME: encoding / decoding container
extension SVDEnumerationCaseData: Decodable {}

extension SVDEnumerationCaseData: Encodable {}

extension SVDEnumerationCaseData: Equatable {}

extension SVDEnumerationCaseData: Hashable {}
8 changes: 8 additions & 0 deletions Sources/SVD/Models/SVDEnumerationCaseDataDefault.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ import FoundationXML
public struct SVDEnumerationCaseDataDefault {
public var isDefault: Bool
}

extension SVDEnumerationCaseDataDefault: Decodable {}

extension SVDEnumerationCaseDataDefault: Encodable {}

extension SVDEnumerationCaseDataDefault: Equatable {}

extension SVDEnumerationCaseDataDefault: Hashable {}
Loading

0 comments on commit ffa2b75

Please sign in to comment.