@@ -21,8 +21,46 @@ public protocol ApplySite : Instruction {
2121 var operands : OperandArray { get }
2222 var numArguments : Int { get }
2323 var substitutionMap : SubstitutionMap { get }
24+
25+ /// Converts an argument index of the apply to the corresponding argument index of the callee.
26+ ///
27+ /// For a FullApplySite this is always a 1-to-1 mapping.
28+ /// For a `partial_apply` the callee index can be higher than the caller's argument index
29+ /// because the arguments to `partial_apply` are a suffix of the callee.
30+ ///
31+ /// Example:
32+ /// ```
33+ /// func callee(a, b, c, d, e) { }
34+ ///
35+ /// %pa = partial_apply @callee(c, d, e)
36+ /// // caller indices: 0, 1, 2
37+ /// // callee indices: 2, 3, 4
38+ ///
39+ /// %a = apply %pa (a, b)
40+ /// // caller indices: 0, 1
41+ /// // callee indices: 0, 1
42+ /// ```
2443 func calleeArgIndex( callerArgIndex: Int ) -> Int
44+
45+ /// Converts an argument index of a callee to the corresponding argument index of the apply.
46+ ///
47+ /// If the apply does not actually apply that argument, it returns nil.
48+ /// Otherwise, for a FullApplySite this is always a 1-to-1 mapping.
49+ /// For a `partial_apply` the caller index can be lower than the callee's argument index
50+ /// because the arguments to `partial_apply` are a suffix of the callee.
51+ ///
52+ /// Example:
53+ /// ```
54+ /// func callee(a, b, c, d, e) { }
55+ /// // callee indices: 0, 1, 2, 3, 4
56+ /// // caller indices in %pa: -, -, 0, 1, 2 ("-" == nil)
57+ /// // caller indices in %a: 0, 1, -, -, -
58+ ///
59+ /// %pa = partial_apply @callee(c, d, e)
60+ /// %a = apply %pa (a, b)
61+ /// ```
2562 func callerArgIndex( calleeArgIndex: Int ) -> Int ?
63+
2664 func getArgumentConvention( calleeArgIndex: Int ) -> ArgumentConvention
2765}
2866
@@ -66,6 +104,15 @@ public protocol FullApplySite : ApplySite {
66104}
67105
68106extension FullApplySite {
69- public func calleeArgIndex( callerArgIndex: Int ) -> Int { callerArgIndex }
70- public func callerArgIndex( calleeArgIndex: Int ) -> Int ? { calleeArgIndex }
107+ public func calleeArgIndex( callerArgIndex: Int ) -> Int {
108+ assert ( callerArgIndex >= 0 && callerArgIndex < numArguments)
109+ return callerArgIndex
110+ }
111+
112+ public func callerArgIndex( calleeArgIndex: Int ) -> Int ? {
113+ if calleeArgIndex < numArguments {
114+ return calleeArgIndex
115+ }
116+ return nil
117+ }
71118}
0 commit comments