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
5 changes: 4 additions & 1 deletion Sources/SwiftLexicalLookup/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ add_swift_syntax_library(SwiftLexicalLookup
Scopes/GenericParameterScopeSyntax.swift
Scopes/IntroducingToSequentialParentScopeSyntax.swift
Scopes/LookInMembersScopeSyntax.swift
Scopes/NominalTypeDeclSyntax.swift
Scopes/NominalTypeDeclScopeSyntax.swift
Scopes/ScopeImplementations.swift
Scopes/ScopeSyntax.swift
Scopes/SequentialScopeSyntax.swift
Expand All @@ -28,6 +28,9 @@ add_swift_syntax_library(SwiftLexicalLookup
QualifiedLookup/DeclName.swift
QualifiedLookup/DeclScope.swift
QualifiedLookup/MemberKind.swift
QualifiedLookup/NominalTypeDeclSyntax.swift
QualifiedLookup/TypeDeclSyntax.swift
QualifiedLookup/TypeLikeSyntax.swift
QualifiedLookup/ValueDeclSyntax.swift
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ import SwiftSyntax
}
}

public init(exactly node: some DeclGroupSyntax) {
self.init(node)!
public init(_ syntax: __shared some DeclGroupSyntax) {
self = Syntax(syntax).cast(Self.self)
}

public var attributes: AttributeListSyntax {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
//===----------------------------------------------------------------------===//
//
// 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

/// Protocol for `NominalTypeDeclSyntax`. Only `[Struct/Enum/Class/Actor/Protocol]DeclSyntax`
/// should conform.
@_spi(_QualifiedLookup) public protocol NominalTypeDeclSyntaxProtocol: DeclGroupSyntax, NamedDeclSyntax {}

/// A nominal type declaration (struct, enum, class, actor, protocol).
@_spi(_QualifiedLookup) public struct NominalTypeDeclSyntax: NominalTypeDeclSyntaxProtocol, SyntaxHashable {
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 NominalTypeDeclSyntaxProtocol, 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 NominalTypeDeclSyntaxProtocol, T>,
newValue: T
) {
switch _syntaxNode.as(SyntaxEnum.self) {
case .structDecl(let declGroup):
var box: any NominalTypeDeclSyntaxProtocol = declGroup
box[keyPath: keyPath] = newValue
_syntaxNode = box._syntaxNode
case .enumDecl(let declGroup):
var box: any NominalTypeDeclSyntaxProtocol = declGroup
box[keyPath: keyPath] = newValue
_syntaxNode = box._syntaxNode
case .classDecl(let declGroup):
var box: any NominalTypeDeclSyntaxProtocol = declGroup
box[keyPath: keyPath] = newValue
_syntaxNode = box._syntaxNode
case .actorDecl(let declGroup):
var box: any NominalTypeDeclSyntaxProtocol = declGroup
box[keyPath: keyPath] = newValue
_syntaxNode = box._syntaxNode
case .protocolDecl(let declGroup):
var box: any NominalTypeDeclSyntaxProtocol = declGroup
box[keyPath: keyPath] = newValue
_syntaxNode = box._syntaxNode
default:
fatalError("[Internal Error] Invalid syntax kind for DeclGroupSyntaxType: \(_syntaxNode.kind)")
}
}

public init(_ syntax: __shared some NominalTypeDeclSyntaxProtocol) {
self = Syntax(syntax).cast(Self.self)
}

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),
])
}
Loading
Loading