forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaParameter.swift
More file actions
110 lines (94 loc) · 2.86 KB
/
JavaParameter.swift
File metadata and controls
110 lines (94 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import SwiftJavaJNICore
/// Represent a parameter in Java code.
struct JavaParameter {
enum ParameterType: CustomStringConvertible {
case concrete(JavaType)
case generic(name: String, extends: [JavaType])
var jniTypeSignature: String {
switch self {
case .concrete(let type):
return type.jniTypeSignature
case .generic(_, let extends):
guard !extends.isEmpty else {
return "Ljava/lang/Object;"
}
// Generics only use the first type for JNI
return extends.first!.jniTypeSignature
}
}
var isPrimitive: Bool {
switch self {
case .concrete(let javaType):
javaType.isPrimitive
case .generic:
false
}
}
/// Returns the concrete JavaType, or `.class` for generics.
var javaType: JavaType {
switch self {
case .concrete(let type): type
case .generic: .class(package: "java.lang", name: "Object")
}
}
var jniTypeName: String {
switch self {
case .concrete(let type): type.jniTypeName
case .generic: "jobject?"
}
}
var description: String {
switch self {
case .concrete(let type): type.description
case .generic(let name, _): name
}
}
}
var name: String
var type: ParameterType
/// Parameter annotations are used in parameter declarations like this: `@Annotation int example`
let annotations: [JavaAnnotation]
init(name: String, type: ParameterType, annotations: [JavaAnnotation] = []) {
self.name = name
self.type = type
self.annotations = annotations
}
init(name: String, type: JavaType, annotations: [JavaAnnotation] = []) {
self.name = name
self.type = .concrete(type)
self.annotations = annotations
}
func renderParameter() -> String {
if annotations.isEmpty {
return "\(type) \(name)"
}
let annotationsStr = annotations.map({ $0.render() }).joined(separator: "")
return "\(annotationsStr) \(type) \(name)"
}
}
struct OutCallback {
/// Name of the "function" that we'll use as the out callback / upcall, e.g. "$_upcall_initialize"
var name: String {
willSet {
precondition("\(newValue)".starts(with: "$"), "OutCallback class names should stat with $")
}
}
var members: [String]
var parameters: [JavaParameter]
// FIXME: compute this instead
var cFunc: CFunction
var body: String
}