Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2026 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
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax

/// A nominal type declaration (struct, enum, class, actor, protocol).
@_spi(_QualifiedLookup) public struct NominalTypeDeclSyntax: SyntaxProtocol, Hashable {
public private(set) var _syntaxNode: Syntax

public init?(_ node: __shared some SyntaxProtocol) {
switch node.kind {
case .structDecl, .enumDecl, .classDecl, .actorDecl, .protocolDecl:
_syntaxNode = node._syntaxNode
default:
return nil
}
}

public static var structure: SyntaxNodeStructure {
SyntaxNodeStructure.choices([
.node(StructDeclSyntax.self),
.node(EnumDeclSyntax.self),
.node(ClassDeclSyntax.self),
.node(ActorDeclSyntax.self),
.node(ProtocolDeclSyntax.self),
])
}
}

extension NominalTypeDeclSyntax: DeclGroupSyntax {
private func _getGroupProp<T>(_ prop: KeyPath<any DeclGroupSyntax & NamedDeclSyntax, T>) -> T {
switch _syntaxNode.as(SyntaxEnum.self) {
case .structDecl(let declGroup):
return declGroup[keyPath: prop]
case .enumDecl(let declGroup):
return declGroup[keyPath: prop]
case .classDecl(let declGroup):
return declGroup[keyPath: prop]
case .actorDecl(let declGroup):
return declGroup[keyPath: prop]
case .protocolDecl(let declGroup):
return declGroup[keyPath: prop]
default:
fatalError("[Internal Error] Invalid syntax kind for NominalTypeDeclSyntax \(_syntaxNode.kind)")
}
}

private mutating func _setGroupProp<T>(
_ keyPath: WritableKeyPath<any DeclGroupSyntax & NamedDeclSyntax, T>,
newValue: T
) {
switch _syntaxNode.as(SyntaxEnum.self) {
case .structDecl(let declGroup):
var box: any DeclGroupSyntax & NamedDeclSyntax = declGroup
box[keyPath: keyPath] = newValue
_syntaxNode = box._syntaxNode
case .enumDecl(let declGroup):
var box: any DeclGroupSyntax & NamedDeclSyntax = declGroup
box[keyPath: keyPath] = newValue
_syntaxNode = box._syntaxNode
case .classDecl(let declGroup):
var box: any DeclGroupSyntax & NamedDeclSyntax = declGroup
box[keyPath: keyPath] = newValue
_syntaxNode = box._syntaxNode
case .actorDecl(let declGroup):
var box: any DeclGroupSyntax & NamedDeclSyntax = declGroup
box[keyPath: keyPath] = newValue
_syntaxNode = box._syntaxNode
case .protocolDecl(let declGroup):
var box: any DeclGroupSyntax & NamedDeclSyntax = declGroup
box[keyPath: keyPath] = newValue
_syntaxNode = box._syntaxNode
default:
fatalError("[Internal Error] Invalid syntax kind for DeclGroupSyntaxType: \(_syntaxNode.kind)")
}
}

public var name: TokenSyntax {
get { _getGroupProp(\.name) }
set { _setGroupProp(\.name, newValue: newValue) }
}

public var attributes: AttributeListSyntax {
get { _getGroupProp(\.attributes) }
set { _setGroupProp(\.attributes, newValue: newValue) }
}

public var modifiers: DeclModifierListSyntax {
get { _getGroupProp(\.modifiers) }
set { _setGroupProp(\.modifiers, newValue: newValue) }
}
public var introducer: TokenSyntax {
get { _getGroupProp(\.introducer) }
set { _setGroupProp(\.introducer, newValue: newValue) }
}

public var inheritanceClause: InheritanceClauseSyntax? {
get { _getGroupProp(\.inheritanceClause) }
set { _setGroupProp(\.inheritanceClause, newValue: newValue) }
}

public var genericWhereClause: GenericWhereClauseSyntax? {
get { _getGroupProp(\.genericWhereClause) }
set { _setGroupProp(\.genericWhereClause, newValue: newValue) }
}

public var memberBlock: MemberBlockSyntax {
get { _getGroupProp(\.memberBlock) }
set { _setGroupProp(\.memberBlock, newValue: newValue) }
}
}
89 changes: 89 additions & 0 deletions Sources/SwiftLexicalLookup/QualifiedLookup/TypeDeclSyntax.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2026 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
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax

/// A nominal type declaration (struct, enum, class, actor, protocol), type
/// alias, associated type or generic parameter.
@_spi(_QualifiedLookup) public struct TypeDeclSyntax: Hashable, SyntaxProtocol {
public private(set) var _syntaxNode: Syntax

public init?(_ node: __shared some SyntaxProtocol) {
switch node.kind {
case .structDecl, .enumDecl, .classDecl, .actorDecl, .protocolDecl, .typeAliasDecl, .associatedTypeDecl,
.genericParameter:
_syntaxNode = node._syntaxNode
default:
return nil
}
}

public static var structure: SyntaxNodeStructure {
SyntaxNodeStructure.choices([
.node(StructDeclSyntax.self),
.node(EnumDeclSyntax.self),
.node(ClassDeclSyntax.self),
.node(ActorDeclSyntax.self),
.node(ProtocolDeclSyntax.self),
.node(TypeAliasDeclSyntax.self),
.node(AssociatedTypeDeclSyntax.self),
.node(GenericParameterSyntax.self),
])
}
}

// MARK: Name

extension TypeDeclSyntax {
public var name: TokenSyntax {
switch _syntaxNode.as(SyntaxEnum.self) {
case .structDecl(let structDecl):
return structDecl.name
case .enumDecl(let enumDecl):
return enumDecl.name
case .classDecl(let classDecl):
return classDecl.name
case .actorDecl(let actorDecl):
return actorDecl.name
case .protocolDecl(let protocolDecl):
return protocolDecl.name
case .typeAliasDecl(let typeAliasDecl):
return typeAliasDecl.name
case .associatedTypeDecl(let associatedTypeDecl):
return associatedTypeDecl.name
case .genericParameter(let genericParameter):
return genericParameter.name
default:
fatalError("[Internal Error] Invalid syntax kind for TypeDeclSyntax: \(_syntaxNode.kind)")
}
}
}

// MARK: Upcasts

extension TypeDeclSyntax {
public init(_ nominalType: NominalTypeDeclSyntax) {
self = Syntax(nominalType).cast(TypeDeclSyntax.self)
}

public init(_ typeAlias: TypeAliasDeclSyntax) {
self = Syntax(typeAlias).cast(TypeDeclSyntax.self)
}

public init(_ associatedType: AssociatedTypeDeclSyntax) {
self = Syntax(associatedType).cast(TypeDeclSyntax.self)
}

public init(_ genericParameter: GenericParameterSyntax) {
self = Syntax(genericParameter).cast(TypeDeclSyntax.self)
}
}
40 changes: 40 additions & 0 deletions Sources/SwiftLexicalLookup/QualifiedLookup/TypeLikeSyntax.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2026 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
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax

/// A protocol for ``TypeLikeSyntax`` nodes.
@_spi(_QualifiedLookup) public protocol TypeLikeSyntaxProtocol: SyntaxProtocol {}

@_spi(_QualifiedLookup) extension TypeSyntax: TypeLikeSyntaxProtocol {}
@_spi(_QualifiedLookup) extension NominalTypeDeclSyntax: TypeLikeSyntaxProtocol {}

/// Either ``TypeSyntax`` or a nominal type. Helps us track which syntax is
/// responsible for a given type-resolution request.
@_spi(_QualifiedLookup) public struct TypeLikeSyntax: Sendable, Hashable, TypeLikeSyntaxProtocol {
public private(set) var _syntaxNode: Syntax

public init?(_ node: __shared some SyntaxProtocol) {
guard node.is(TypeSyntax.self) || node.is(NominalTypeDeclSyntax.self) else { return nil }
_syntaxNode = Syntax(node)
}

public init(_ typeLikeSyntax: TypeLikeSyntaxProtocol) {
self._syntaxNode = typeLikeSyntax._syntaxNode
}

// TODO: Are we allowed to have non-primitive node types??
public static let structure = SyntaxNodeStructure.choices([
SyntaxNodeStructure.SyntaxChoice.node(TypeSyntax.self),
SyntaxNodeStructure.SyntaxChoice.node(NominalTypeDeclSyntax.self),
])
}
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ extension ValueDeclSyntax {

// Protocols
extension ValueDeclSyntax {
init(fromProtocol syntax: borrowing some NominalTypeDeclSyntax) {
init(fromProtocol syntax: borrowing some NonProtocolNominalTypeDeclSyntax) {
// We know this cast is going to succeed. Go through `init(_: SyntaxData)` just to double-check and
// verify the kind matches in debug builds and get maximum performance in release builds.
self = Syntax(syntax).cast(ValueDeclSyntax.self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
import SwiftSyntax

@_spi(Experimental)
public protocol NominalTypeDeclSyntax: LookInMembersScopeSyntax, DeclGroupSyntax, NamedDeclSyntax,
public protocol NonProtocolNominalTypeDeclSyntax: LookInMembersScopeSyntax, DeclGroupSyntax, NamedDeclSyntax,
WithGenericParametersScopeSyntax
{
var genericParameterClause: GenericParameterClauseSyntax? { get }
}

extension NominalTypeDeclSyntax {
extension NonProtocolNominalTypeDeclSyntax {
@_spi(Experimental) public var lookupMembersPosition: AbsolutePosition {
name.positionAfterSkippingLeadingTrivia
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,22 +363,22 @@ import SwiftSyntax
}
}

@_spi(Experimental) extension ActorDeclSyntax: NominalTypeDeclSyntax {
@_spi(Experimental) extension ActorDeclSyntax: NonProtocolNominalTypeDeclSyntax {
@_spi(Experimental) public var scopeDebugName: String {
"ActorDeclScope"
}
}
@_spi(Experimental) extension ClassDeclSyntax: NominalTypeDeclSyntax {
@_spi(Experimental) extension ClassDeclSyntax: NonProtocolNominalTypeDeclSyntax {
@_spi(Experimental) public var scopeDebugName: String {
"ClassDeclScope"
}
}
@_spi(Experimental) extension StructDeclSyntax: NominalTypeDeclSyntax {
@_spi(Experimental) extension StructDeclSyntax: NonProtocolNominalTypeDeclSyntax {
@_spi(Experimental) public var scopeDebugName: String {
"StructDeclScope"
}
}
@_spi(Experimental) extension EnumDeclSyntax: NominalTypeDeclSyntax {
@_spi(Experimental) extension EnumDeclSyntax: NonProtocolNominalTypeDeclSyntax {
@_spi(Experimental) public var scopeDebugName: String {
"EnumDeclScope"
}
Expand Down