-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathBinder_Constraints.cs
235 lines (206 loc) · 10.5 KB
/
Binder_Constraints.cs
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp
{
internal partial class Binder
{
/// <summary>
/// Return a collection of bound constraint clauses indexed by type parameter
/// ordinal. All constraint clauses are bound, even if there are multiple constraints
/// for the same type parameter, or constraints for unrecognized type parameters.
/// Extra constraints are not included in the returned collection however.
/// </summary>
internal ImmutableArray<TypeParameterConstraintClause> BindTypeParameterConstraintClauses(
Symbol containingSymbol,
ImmutableArray<TypeParameterSymbol> typeParameters,
SyntaxList<TypeParameterConstraintClauseSyntax> clauses,
DiagnosticBag diagnostics)
{
Debug.Assert(this.Flags.Includes(BinderFlags.GenericConstraintsClause));
Debug.Assert((object)containingSymbol != null);
Debug.Assert((containingSymbol.Kind == SymbolKind.NamedType) || (containingSymbol.Kind == SymbolKind.Method));
Debug.Assert(typeParameters.Length > 0);
Debug.Assert(clauses.Count > 0);
int n = typeParameters.Length;
// Create a map from type parameter name to ordinal.
// No need to report duplicate names since duplicates
// are reported when the type parameters are bound.
var names = new Dictionary<string, int>(n);
foreach (var typeParameter in typeParameters)
{
var name = typeParameter.Name;
if (!names.ContainsKey(name))
{
names.Add(name, names.Count);
}
}
// An array of constraint clauses, one for each type parameter, indexed by ordinal.
var results = new TypeParameterConstraintClause[n];
// Bind each clause and add to the results.
foreach (var clause in clauses)
{
var name = clause.Name.Identifier.ValueText;
int ordinal;
if (names.TryGetValue(name, out ordinal))
{
Debug.Assert(ordinal >= 0);
Debug.Assert(ordinal < n);
var constraintClause = this.BindTypeParameterConstraints(name, clause.Constraints, diagnostics);
if (results[ordinal] == null)
{
results[ordinal] = constraintClause;
}
else
{
// "A constraint clause has already been specified for type parameter '{0}'. ..."
diagnostics.Add(ErrorCode.ERR_DuplicateConstraintClause, clause.Name.Location, name);
}
}
else
{
// Unrecognized type parameter. Don't bother binding the constraints
// (the ": I<U>" in "where U : I<U>") since that will lead to additional
// errors ("type or namespace 'U' could not be found") if the type
// parameter is referenced in the constraints.
// "'{1}' does not define type parameter '{0}'"
diagnostics.Add(ErrorCode.ERR_TyVarNotFoundInConstraint, clause.Name.Location, name, containingSymbol.ConstructedFrom());
}
}
return results.AsImmutableOrNull();
}
/// <summary>
/// Bind and return a single type parameter constraint clause.
/// </summary>
private TypeParameterConstraintClause BindTypeParameterConstraints(string name, SeparatedSyntaxList<TypeParameterConstraintSyntax> constraintsSyntax, DiagnosticBag diagnostics)
{
var constraints = TypeParameterConstraintKind.None;
var constraintTypes = ArrayBuilder<TypeSymbol>.GetInstance();
foreach (var syntax in constraintsSyntax)
{
switch (syntax.Kind())
{
case SyntaxKind.ClassConstraint:
constraints |= TypeParameterConstraintKind.ReferenceType;
break;
case SyntaxKind.StructConstraint:
constraints |= TypeParameterConstraintKind.ValueType;
break;
case SyntaxKind.ConstructorConstraint:
constraints |= TypeParameterConstraintKind.Constructor;
break;
case SyntaxKind.TypeConstraint:
{
var typeSyntax = (TypeConstraintSyntax)syntax;
var type = this.BindType(typeSyntax.Type, diagnostics);
// Only valid constraint types are included in ConstraintTypes
// since, in general, it may be difficult to support all invalid types.
// In the future, we may want to include some invalid types
// though so the public binding API has the most information.
if (!IsValidConstraintType(typeSyntax, type, diagnostics))
{
continue;
}
if (constraintTypes.Contains(type))
{
// "Duplicate constraint '{0}' for type parameter '{1}'"
Error(diagnostics, ErrorCode.ERR_DuplicateBound, syntax, type, name);
continue;
}
if (type.TypeKind == TypeKind.Class)
{
// If there is already a struct or class constraint (class constraint could be
// 'class' or explicit type), report an error and drop this class. If we don't
// drop this additional class, we may end up with conflicting class constraints.
if (constraintTypes.Count > 0)
{
// "The class type constraint '{0}' must come before any other constraints"
Error(diagnostics, ErrorCode.ERR_ClassBoundNotFirst, syntax, type);
continue;
}
if ((constraints & (TypeParameterConstraintKind.ReferenceType | TypeParameterConstraintKind.ValueType)) != 0)
{
// "'{0}': cannot specify both a constraint class and the 'class' or 'struct' constraint"
Error(diagnostics, ErrorCode.ERR_RefValBoundWithClass, syntax, type);
continue;
}
}
constraintTypes.Add(type);
}
break;
default:
throw ExceptionUtilities.UnexpectedValue(syntax.Kind());
}
}
return new TypeParameterConstraintClause(constraints, constraintTypes.ToImmutableAndFree());
}
/// <summary>
/// Returns true if the type is a valid constraint type.
/// Otherwise returns false and generates a diagnostic.
/// </summary>
private static bool IsValidConstraintType(TypeConstraintSyntax syntax, TypeSymbol type, DiagnosticBag diagnostics)
{
switch (type.SpecialType)
{
case SpecialType.System_Object:
case SpecialType.System_ValueType:
case SpecialType.System_Enum:
case SpecialType.System_Delegate:
case SpecialType.System_MulticastDelegate:
case SpecialType.System_Array:
// "Constraint cannot be special class '{0}'"
Error(diagnostics, ErrorCode.ERR_SpecialTypeAsBound, syntax, type);
return false;
}
switch (type.TypeKind)
{
case TypeKind.Error:
case TypeKind.TypeParameter:
return true;
case TypeKind.Interface:
break;
case TypeKind.Dynamic:
// "Constraint cannot be the dynamic type"
Error(diagnostics, ErrorCode.ERR_DynamicTypeAsBound, syntax);
return false;
case TypeKind.Class:
if (type.IsSealed)
{
goto case TypeKind.Struct;
}
else if (type.IsStatic)
{
// "'{0}': static classes cannot be used as constraints"
Error(diagnostics, ErrorCode.ERR_ConstraintIsStaticClass, syntax, type);
return false;
}
break;
case TypeKind.Delegate:
case TypeKind.Enum:
case TypeKind.Struct:
// "'{0}' is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter."
Error(diagnostics, ErrorCode.ERR_BadBoundType, syntax, type);
return false;
case TypeKind.Array:
case TypeKind.Pointer:
// CS0706 already reported by parser.
return false;
case TypeKind.Submission:
// script class is synthesized, never used as a constraint
default:
throw ExceptionUtilities.UnexpectedValue(type.TypeKind);
}
if (type.ContainsDynamic())
{
// "Constraint cannot be a dynamic type '{0}'"
Error(diagnostics, ErrorCode.ERR_ConstructedDynamicTypeAsBound, syntax, type);
return false;
}
return true;
}
}
}