Skip to content

[ASTGen] do not merge #80049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
62 changes: 36 additions & 26 deletions lib/ASTGen/Sources/ASTGen/Availability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import SwiftIfConfig
extension ASTGenVisitor {
/// Implementation detail for `generateAvailableAttr(attribute:)` and `generateSpecializeAttr(attribute:)`.
func generateAvailableAttr(
attribute attrNode: AttributeSyntax,
atLoc: BridgedSourceLoc,
range: BridgedSourceRange,
attrName: SyntaxText,
Expand All @@ -43,14 +44,15 @@ extension ASTGenVisitor {
isShorthand = false
}
if isShorthand {
return self.generateAvailableAttrShorthand(atLoc: atLoc, range: range, args: args, isSPI: isSPI)
return self.generateAvailableAttrShorthand(attribute: attrNode, atLoc: atLoc, range: range, args: args, isSPI: isSPI)
}
}

// E.g.
// @available(macOS, introduced: 10.12, deprecated: 11.2)
// @available(*, unavailable, message: "out of service")
let attr = self.generateAvailableAttrExtended(atLoc: atLoc, range: range, args: args, isSPI: isSPI)
let attr = self.generateAvailableAttrExtended(attribute: attrNode, atLoc: atLoc, range: range, args: args, isSPI: isSPI)

if let attr {
return [attr]
} else {
Expand All @@ -66,6 +68,7 @@ extension ASTGenVisitor {
}

func generateAvailableAttrShorthand(
attribute attrNode: AttributeSyntax,
atLoc: BridgedSourceLoc,
range: BridgedSourceRange,
args: AvailabilityArgumentListSyntax,
Expand Down Expand Up @@ -121,6 +124,7 @@ extension ASTGenVisitor {
}

func generateAvailableAttrExtended(
attribute attrNode: AttributeSyntax,
atLoc: BridgedSourceLoc,
range: BridgedSourceRange,
args: AvailabilityArgumentListSyntax,
Expand Down Expand Up @@ -165,16 +169,17 @@ extension ASTGenVisitor {
var renamed: BridgedStringRef? = nil

func generateVersion(arg: AvailabilityLabeledArgumentSyntax, into target: inout VersionAndRange?) {
guard let versionSytnax = arg.value.as(VersionTupleSyntax.self) else {
// TODO: Diagnose
fatalError("expected version after introduced, deprecated, or obsoleted")
}
guard let version = VersionTuple(parsing: versionSytnax.trimmedDescription) else {
// TODO: Diagnose
fatalError("invalid version string")
}
if target != nil {
// TODO: Diagnose duplicated.
diagnose(.duplicatedLabeledArgumentInAttribute(attrNode, argument: arg, name: arg.label.text))
return
}
guard
let versionSytnax = arg.value.as(VersionTupleSyntax.self),
let version = VersionTuple(parsing: versionSytnax.trimmedDescription)
else {
// FIXME: This is already diagnosed in ParserDiagnostics.
// diagnose(.expectedVersionNumberInAvailableAttr(arg))
return
}

target = .init(version: version, range: self.generateSourceRange(versionSytnax))
Expand All @@ -195,7 +200,7 @@ extension ASTGenVisitor {
case "noasync":
attrKind = .noAsync
default:
// TODO: Diagnose
diagnose(.unexpectedArgumentInAttribute(attrNode, arg))
continue
}

Expand Down Expand Up @@ -224,31 +229,35 @@ extension ASTGenVisitor {
generateVersion(arg: arg, into: &obsoleted)
case .message:
guard let literal = arg.value.as(SimpleStringLiteralExprSyntax.self) else {
// TODO: Diagnose.
fatalError("invalid argument type for 'message:'")
diagnose(.expectedArgumentValueInAttribute(attrNode, label: "message", value: "string literal", at: arg.value))
continue
}
guard let _message = self.generateStringLiteralTextIfNotInterpolated(expr: literal) else {
fatalError("invalid literal value")
diagnose(.stringInterpolationNotAllowedInAttribute(attrNode, at: literal))
continue
}
guard message == nil else {
fatalError("duplicated 'message' argument")
diagnose(.duplicatedLabeledArgumentInAttribute(attrNode, argument: arg, name: "message"))
continue
}
message = _message
case .renamed:
guard let literal = arg.value.as(SimpleStringLiteralExprSyntax.self) else {
// TODO: Diagnose.
fatalError("invalid argument type for 'renamed:'")
diagnose(.expectedArgumentValueInAttribute(attrNode, label: "renamed", value: "string literal", at: arg.value))
continue
}
guard let _renamed = self.generateStringLiteralTextIfNotInterpolated(expr: literal) else {
fatalError("invalid literal value")
diagnose(.stringInterpolationNotAllowedInAttribute(attrNode, at: literal))
continue
}
guard renamed == nil else {
fatalError("duplicated 'message' argument")
diagnose(.duplicatedLabeledArgumentInAttribute(attrNode, argument: arg, name: "renamed"))
continue
}
renamed = _renamed
case .invalid:
// TODO: Diagnose
fatalError("invalid labeled argument")
diagnose(.unexpectedArgumentInAttribute(attrNode, arg))
continue
}
}
}
Expand Down Expand Up @@ -333,8 +342,8 @@ extension ASTGenVisitor {
// Was not a macro, it should be a valid platform name.
let platform = self.generateIdentifierAndSourceLoc(domainNode)
guard let version = version else {
// TODO: Diagnostics.
fatalError("expected version")
diagnose(.expectedVersionNumberAfterPlatform(domainNode))
return
}
// FIXME: Wasting ASTContext memory.
// 'AvailabilitySpec' is 'ASTAllocated' but created spec is ephemeral in context of `@available` attributes.
Expand All @@ -361,8 +370,9 @@ extension ASTGenVisitor {
case .availabilityVersionRestriction(let platformVersion):
handle(domainNode: platformVersion.platform, versionNode: platformVersion.version)
default:
// TODO: Diagnostics.
fatalError("invalid argument kind for availability spec")
// FIXME: This is unreachable? ParserDiagnostics emits 'expected platform name'.
let token = parsed.argument.firstToken(viewMode: .sourceAccurate)!
diagnose(.unexpectedArgumentInAvailabilitySpecList(token))
}
}

Expand Down
23 changes: 16 additions & 7 deletions lib/ASTGen/Sources/ASTGen/BuiltinPound.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,27 @@ extension ASTGenVisitor {
func generatePoundAssertStmt(freestandingMacroExpansion node: some FreestandingMacroExpansionSyntax) -> BridgedPoundAssertStmt? {
assert(self.ctx.langOptsHasFeature(.StaticAssert))
var args = node.arguments[...]
let conditionExpr = self.generateConsumingAttrOption(args: &args, label: nil) { conditionNode in
self.generate(expr: conditionNode)
}
guard let conditionExpr else {
return nil
guard let arg = args.popFirst(), arg.label == nil else {
// TODO: Diagnose.
fatalError("expected condition expression in #assert")
}
let conditionExpr = self.generate(expr: arg.expression)

let message: BridgedStringRef?
if !args.isEmpty {
message = self.generateConsumingSimpleStringLiteralAttrOption(args: &args)
if let arg = args.popFirst() {
guard arg.label == nil else {
// TODO: Diagnose.
fatalError("unexpected label")
}
message = self.generateStringLiteralTextIfNotInterpolated(expr: arg.expression)
// TODO: Diagnose if nil.
} else {
message = nil
}
guard args.isEmpty else {
// TODO: Diagnose.
fatalError("unexpected label")
}

return .createParsed(
self.ctx,
Expand Down
1 change: 1 addition & 0 deletions lib/ASTGen/Sources/ASTGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_pure_swift_host_library(swiftASTGen STATIC CXX_INTEROP
CompilerBuildConfiguration.swift
DeclAttrs.swift
Decls.swift
DiagnosticMessages.swift
Diagnostics.swift
DiagnosticsBridge.swift
Exprs.swift
Expand Down
Loading