You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add support for function pointers
Add support for function pointers
Add support for function pointers
fix md and links
fix md
Add support for function pointers
Add support for function pointers
Add support for function pointers
disable undefined output
Update unsafe-code.md
fix formatting
fix link
fix link
fix link
Overhaul pointer_type grammar and description
fix formatting
fix grammar rule name spelling
fix merge issues
fix link
An *unfixed* typevariable `Xᵢ` *dependsdirectlyon* an *unfixed* typevariable `Xₑ` ifforsomeargument `Eᵥ` withtype `Tᵥ` `Xₑ` occursinan *inputtype* of `Eᵥ` withtype `Tᵥ` and `Xᵢ` occursinan *outputtype* of `Eᵥ` withtype `Tᵥ`.
@@ -810,6 +818,8 @@ An *output type inference* is made *from* an expression `E` *to* a type `T` in
810
818
-If `E` isatupleexpressionwitharity `N` andelements `Eᵢ`, and `T` isatupletypewitharity `N` withcorrespondingelementtypes `Tₑ` or `T` isanullablevaluetype `T0?` and `T0` isatupletypewitharity `N` thathasacorrespondingelementtype `Tₑ`, thenfor each `Eᵢ` an output type inference is made from `Eᵢ` to `Tₑ`.
811
819
- If `E` is an anonymous function with inferred return type `U` ([§12.6.3.14](expressions.md#126314-inferred-return-type)) and `T` isadelegatetypeorexpressiontreetypewithreturntype `Tₓ`, thena*lower-boundinference* ([§12.6.3.11](expressions.md#126311-lower-bound-inferences)) ismade *from* `U` *to* `Tₓ`.
- `V` isoneof `IEnumerable<V₁>`, `ICollection<V₁>`, `IReadOnlyList<V₁>>`, `IReadOnlyCollection<V₁>` or `IList<V₁>` and `U` isasingle-dimensionalarraytype `U₁[]`
843
853
- `V` isaconstructed `class`, `struct`, `interface` or `delegate` type `C<V₁...Vₑ>` and there is a unique type `C<U₁...Uₑ>` such that `U` (or, if `U` isa type `parameter`, itseffectivebaseclassoranymemberofitseffectiveinterfaceset) is identical to, `inherits` from (directlyorindirectly), orimplements (directlyorindirectly) `C<U₁...Uₑ>`.
- (The “uniqueness” restrictionmeansthatinthecaseinterface `C<T>{} classU: C<X>, C<Y>{}`, thennoinferenceismade when inferring from `U` to `C<T>` because `U₁` could be `X` or `Y`.)
@@ -861,14 +882,25 @@ An *upper-bound inference from* a type `U` *to* a type `V` is made as follows:
861
882
- `U` isoneof `IEnumerable<Uₑ>`, `ICollection<Uₑ>`, `IReadOnlyList<Uₑ>`, `IReadOnlyCollection<Uₑ>` or `IList<Uₑ>` and `V` isasingle-dimensionalarraytype `Vₑ[]`
862
883
- `U` isthetype `U1?` and `V` isthetype `V1?`
863
884
- `U` isconstructedclass, struct, interfaceordelegatetype `C<U₁...Uₑ>` and `V` isa `class, struct, interface` or `delegate` typewhichis `identical` to, `inherits` from (directlyorindirectly), orimplements (directlyorindirectly) auniquetype `C<V₁...Vₑ>`
885
+
- `U` isafunctionpointertype (§function-pointers) then `delegate*<U2..Uk, U1>` and `V` isafunctionpointertypewhichisidenticalto `delegate*<V2..Vk, V1>`, andthecallingconventionof `U` isidenticalto `V`, andtherefnessof `Ui` isidenticalto `Vi`.
- (The “uniqueness” restrictionmeansthatgivenaninterface `C<T>{} classV<Z>: C<X<Z>>, C<Y<Z>>{}`, thennoinferenceismade when inferring from `C<U₁>` to `V<Q>`. Inferences are not made from `U₁` to either `X<Q>` or `Y<Q>`.)
> *Note*:Thisisonlyapplicableinunsafecode, andpermitsoverloadingon `void*` anda `delegate*` allowing the `&` operator to distinguish between the two. *end note*
1132
+
1097
1133
#### 12.6.4.4 Better parameter-passing mode
1098
1134
1099
1135
It is permitted to have corresponding parameters in two overloaded methods differ only by parameter-passing mode provided one of the two parameters has value-passing mode, asfollows:
@@ -1114,6 +1150,8 @@ Given an implicit conversion `C₁` that converts from an expression `E` to a ty
1114
1150
1115
1151
- `E` exactly matches `T₁` and `E` does not exactly match `T₂` ([§12.6.4.6](expressions.md#12646-exactly-matching-expression))
1116
1152
- `E` exactlymatchesbothorneitherof `T₁` and `T₂`, and `T₁` isabetterconversiontargetthan `T₂` ([§12.6.4.7](expressions.md#12647-better-conversion-target))
- `E` isamethodgroup ([§12.2](expressions.md#122-expression-classifications)), `T₁` iscompatible ([§21.4](delegates.md#214-delegate-compatibility)) withthesinglebestmethodfromthemethodgroupfor conversion `C₁`, and `T₂` is not compatible with the single best method from the method group for conversion `C₂`
1118
1156
1119
1157
#### 12.6.4.6 Exactly matching expression
@@ -2001,6 +2039,25 @@ The result of evaluating an *invocation_expression* is classified as follows:
2001
2039
- Otherwise, if the *invocation_expression* invokes a returns-by-ref method ([§15.6.1](classes.md#1561-general)) or a returns-by-ref delegate, the result is a variable with an associated type of the return type of the method or delegate. If the invocation is of an instance method, and the receiver is of a class type `T`, the associated type is picked from the first declaration or override of the method found when starting with `T` and searching through its base classes.
2002
2040
- Otherwise, the *invocation_expression* invokes a returns-by-value method ([§15.6.1](classes.md#1561-general)) or returns-by-value delegate, and the result is a value, with an associated type of the return type of the method or delegate. If the invocation is of an instance method, and the receiver is of a class type `T`, the associated type is picked from the first declaration or override of the method found when starting with `T` and searching through its base classes.
2003
2041
2042
+
> *Note*: The following is only applicable in unsafe code. *end note*
2043
+
2044
+
The *method_declaration*[§15.6.1](classes.md#1561-general) for an unmanaged method shall have the attribute `System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute`. The use of this attribute on a method results in the following constraints:
2045
+
2046
+
- It is an error to directly call that method from C#. Instead, one can obtain a function pointer (§function-pointers) to that method and then invoke the method via that pointer.
2047
+
- It is an error for that method to have a parameter or return type that is not an `unmanaged_type` ([§8.8](types.md#88-unmanaged-types)).
2048
+
- It is an error for that method to have type parameters, even if those type parameters are constrained to `unmanaged`.
2049
+
- It is an error for that method to be in a generic type.
2050
+
- It is an error to convert that method to a delegate type.
2051
+
2052
+
For a function pointer invocation, the *primary_expression* of the *invocation_expression* shall be a value of a *funcptr_type*. Furthermore, considering the method being pointed to to be a function member with the same parameter list as the *funcptr_type*, the *funcptr_type* shall be applicable ([§12.6.4.2](expressions.md#12642-applicable-function-member)) with respect to the *argument_list* of the *invocation_expression*.
2053
+
2054
+
The run-time processing of a function pointer invocation of the form `F(A)`, where `F` is a *primary_expression* of a *funcptr_type* and `A` is an optional *argument_list*, consists of the following steps:
2055
+
2056
+
-`F` is evaluated. If this evaluation causes an exception, no further steps are executed.
2057
+
- The argument list `A` is evaluated. If this evaluation causes an exception, no further steps are executed.
2058
+
- The value of `F` is checked to be valid. If that value is `null`, an implementation-defined exception is thrown, and no further steps are executed.
2059
+
- Otherwise, `F` points to a method. Function member invocation ([§12.6.6](expressions.md#1266-function-member-invocation)) is performed on the method to which `F` points.
2060
+
2004
2061
#### 12.8.10.2 Method invocations
2005
2062
2006
2063
For a method invocation, the *primary_expression* of the *invocation_expression* shall be a method group. The method group identifies the one method to invoke or the set of overloaded methods from which to choose a specific method to invoke. In the latter case, determination of the specific method to invoke is based on the context provided by the types of the arguments in the *argument_list*.
Copy file name to clipboardExpand all lines: standard/portability-issues.md
+5-1Lines changed: 5 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,10 +11,11 @@ This annex collects some information about portability that appears in this spec
11
11
The behavior is undefined in the following circumstances:
12
12
13
13
1. The behavior of the enclosing async function when an awaiter’s implementation of the interface methods `INotifyCompletion.OnCompleted` and `ICriticalNotifyCompletion.UnsafeOnCompleted` does not cause the resumption delegate to be invoked at most once ([§12.9.9.4](expressions.md#12994-run-time-evaluation-of-await-expressions)).
14
-
1. Passing pointers as reference or output parameters ([§24.3](unsafe-code.md#243-pointer-types)).
14
+
1. Passing pointers as `ref` or `out` parameters (§data-pointers).
15
15
1. When dereferencing the result of converting one pointer type to another and the resulting pointer is not correctly aligned for the pointed-to type. ([§24.5.1](unsafe-code.md#2451-general)).
16
16
1. When the unary `*` operator is applied to a pointer containing an invalid value ([§24.6.2](unsafe-code.md#2462-pointer-indirection)).
17
17
1. When a pointer is subscripted to access an out-of-bounds element ([§24.6.4](unsafe-code.md#2464-pointer-element-access)).
18
+
1. When comparing values of *funcptr_type*s, or `void*` copies thereof ([§24.6.8](unsafe-code.md#2468-pointer-comparison).
18
19
1. Modifying objects of managed type through fixed pointers ([§24.7](unsafe-code.md#247-the-fixed-statement)).
19
20
1. The content of memory newly allocated by `stackalloc` ([§12.8.22](expressions.md#12822-stack-allocation)).
20
21
1. Attempting to allocate a negative number of items using `stackalloc`([§12.8.22](expressions.md#12822-stack-allocation)).
@@ -44,13 +45,16 @@ A conforming implementation is required to document its choice of behavior in ea
44
45
1. The impact of thread termination when a thread has no handler for an exception, and the thread is itself terminated. ([§13.10.6](statements.md#13106-the-throw-statement))
45
46
1. The mechanism by which linkage to an external method is achieved. ([§15.6.8](classes.md#1568-external-methods))
46
47
1. The impact of thread termination when no matching `catch` clause is found for an exception and the code that initially started that thread is reached. ([§22.4](exceptions.md#224-how-exceptions-are-handled)).
48
+
1. The token name mapping and semantics of unmanaged calling conventions beyond those required by this specification, and the set of valid combinations of those tokens (§function-pointers).
47
49
1. An execution environment may provide additional attributes that affect the execution of a C# program. ([§23.5.1](attributes.md#2351-general))
48
50
1. The mappings between pointers and integers. ([§24.5.1](unsafe-code.md#2451-general))
49
51
1. The effect of applying the unary `*` operator to a `null` pointer. ([§24.6.2](unsafe-code.md#2462-pointer-indirection))
52
+
1. The type of exception thrown when the *primary_expression* of an *invocation_expression* is a function pointer with value `null`, and an attempt is made to invoke the (non-existent) pointed-to method ([§12.8.10](expressions.md#12810-invocation-expressions)).
50
53
1. The behavior when pointer arithmetic overflows the domain of the pointer type. ([§24.6.6](unsafe-code.md#2466-pointer-increment-and-decrement), [§24.6.7](unsafe-code.md#2467-pointer-arithmetic))
51
54
1. The result of the `sizeof` operator for non-pre-defined value types. ([§24.6.9](unsafe-code.md#2469-the-sizeof-operator))
52
55
1. The behavior of the `fixed` statement if the array expression is `null` or if the array has zero elements. ([§24.7](unsafe-code.md#247-the-fixed-statement))
53
56
1. The behavior of the `fixed` statement if the string expression is `null`. ([§24.7](unsafe-code.md#247-the-fixed-statement))
57
+
1. The value returned when a stack allocation of size zero is made ([§12.8.22](expressions.md#12822-stack-allocation)).
0 commit comments