1 | // Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team |
---|
2 | // |
---|
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this |
---|
4 | // software and associated documentation files (the "Software"), to deal in the Software |
---|
5 | // without restriction, including without limitation the rights to use, copy, modify, merge, |
---|
6 | // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
---|
7 | // to whom the Software is furnished to do so, subject to the following conditions: |
---|
8 | // |
---|
9 | // The above copyright notice and this permission notice shall be included in all copies or |
---|
10 | // substantial portions of the Software. |
---|
11 | // |
---|
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
13 | // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
14 | // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
---|
15 | // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
---|
16 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
17 | // DEALINGS IN THE SOFTWARE. |
---|
18 | |
---|
19 | using System; |
---|
20 | using System.Collections.Generic; |
---|
21 | using System.Globalization; |
---|
22 | |
---|
23 | using ICSharpCode.NRefactory.Utils; |
---|
24 | |
---|
25 | namespace ICSharpCode.NRefactory.TypeSystem.Implementation |
---|
26 | { |
---|
27 | /// <summary> |
---|
28 | /// Default implementation of <see cref="IUnresolvedTypeParameter"/>. |
---|
29 | /// </summary> |
---|
30 | [Serializable] |
---|
31 | public class DefaultUnresolvedTypeParameter : IUnresolvedTypeParameter, IFreezable |
---|
32 | { |
---|
33 | readonly int index; |
---|
34 | IList<IUnresolvedAttribute> attributes; |
---|
35 | IList<ITypeReference> constraints; |
---|
36 | string name; |
---|
37 | DomRegion region; |
---|
38 | |
---|
39 | SymbolKind ownerType; |
---|
40 | VarianceModifier variance; |
---|
41 | BitVector16 flags; |
---|
42 | const ushort FlagFrozen = 0x0001; |
---|
43 | const ushort FlagReferenceTypeConstraint = 0x0002; |
---|
44 | const ushort FlagValueTypeConstraint = 0x0004; |
---|
45 | const ushort FlagDefaultConstructorConstraint = 0x0008; |
---|
46 | |
---|
47 | public void Freeze() |
---|
48 | { |
---|
49 | if (!flags[FlagFrozen]) { |
---|
50 | FreezeInternal(); |
---|
51 | flags[FlagFrozen] = true; |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | protected virtual void FreezeInternal() |
---|
56 | { |
---|
57 | attributes = FreezableHelper.FreezeListAndElements(attributes); |
---|
58 | constraints = FreezableHelper.FreezeList(constraints); |
---|
59 | } |
---|
60 | |
---|
61 | public DefaultUnresolvedTypeParameter(SymbolKind ownerType, int index, string name = null) |
---|
62 | { |
---|
63 | this.ownerType = ownerType; |
---|
64 | this.index = index; |
---|
65 | this.name = name ?? ((ownerType == SymbolKind.Method ? "!!" : "!") + index.ToString(CultureInfo.InvariantCulture)); |
---|
66 | } |
---|
67 | |
---|
68 | public SymbolKind OwnerType { |
---|
69 | get { return ownerType; } |
---|
70 | } |
---|
71 | |
---|
72 | public int Index { |
---|
73 | get { return index; } |
---|
74 | } |
---|
75 | |
---|
76 | public bool IsFrozen { |
---|
77 | get { return flags[FlagFrozen]; } |
---|
78 | } |
---|
79 | |
---|
80 | public string Name { |
---|
81 | get { return name; } |
---|
82 | set { |
---|
83 | FreezableHelper.ThrowIfFrozen(this); |
---|
84 | name = value; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | string INamedElement.FullName { |
---|
89 | get { return name; } |
---|
90 | } |
---|
91 | |
---|
92 | string INamedElement.Namespace { |
---|
93 | get { return string.Empty; } |
---|
94 | } |
---|
95 | |
---|
96 | string INamedElement.ReflectionName { |
---|
97 | get { |
---|
98 | if (ownerType == SymbolKind.Method) |
---|
99 | return "``" + index.ToString(CultureInfo.InvariantCulture); |
---|
100 | else |
---|
101 | return "`" + index.ToString(CultureInfo.InvariantCulture); |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | public IList<IUnresolvedAttribute> Attributes { |
---|
106 | get { |
---|
107 | if (attributes == null) |
---|
108 | attributes = new List<IUnresolvedAttribute>(); |
---|
109 | return attributes; |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | public IList<ITypeReference> Constraints { |
---|
114 | get { |
---|
115 | if (constraints == null) |
---|
116 | constraints = new List<ITypeReference>(); |
---|
117 | return constraints; |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | public VarianceModifier Variance { |
---|
122 | get { return variance; } |
---|
123 | set { |
---|
124 | FreezableHelper.ThrowIfFrozen(this); |
---|
125 | variance = value; |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | public DomRegion Region { |
---|
130 | get { return region; } |
---|
131 | set { |
---|
132 | FreezableHelper.ThrowIfFrozen(this); |
---|
133 | region = value; |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | public bool HasDefaultConstructorConstraint { |
---|
138 | get { return flags[FlagDefaultConstructorConstraint]; } |
---|
139 | set { |
---|
140 | FreezableHelper.ThrowIfFrozen(this); |
---|
141 | flags[FlagDefaultConstructorConstraint] = value; |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | public bool HasReferenceTypeConstraint { |
---|
146 | get { return flags[FlagReferenceTypeConstraint]; } |
---|
147 | set { |
---|
148 | FreezableHelper.ThrowIfFrozen(this); |
---|
149 | flags[FlagReferenceTypeConstraint] = value; |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | public bool HasValueTypeConstraint { |
---|
154 | get { return flags[FlagValueTypeConstraint]; } |
---|
155 | set { |
---|
156 | FreezableHelper.ThrowIfFrozen(this); |
---|
157 | flags[FlagValueTypeConstraint] = value; |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | /// <summary> |
---|
162 | /// Uses the specified interning provider to intern |
---|
163 | /// strings and lists in this entity. |
---|
164 | /// This method does not test arbitrary objects to see if they implement ISupportsInterning; |
---|
165 | /// instead we assume that those are interned immediately when they are created (before they are added to this entity). |
---|
166 | /// </summary> |
---|
167 | public virtual void ApplyInterningProvider(InterningProvider provider) |
---|
168 | { |
---|
169 | if (provider == null) |
---|
170 | throw new ArgumentNullException("provider"); |
---|
171 | FreezableHelper.ThrowIfFrozen(this); |
---|
172 | name = provider.Intern(name); |
---|
173 | attributes = provider.InternList(attributes); |
---|
174 | constraints = provider.InternList(constraints); |
---|
175 | } |
---|
176 | |
---|
177 | public virtual ITypeParameter CreateResolvedTypeParameter(ITypeResolveContext context) |
---|
178 | { |
---|
179 | IEntity owner = null; |
---|
180 | if (this.OwnerType == SymbolKind.Method) { |
---|
181 | owner = context.CurrentMember as IMethod; |
---|
182 | } else if (this.OwnerType == SymbolKind.TypeDefinition) { |
---|
183 | owner = context.CurrentTypeDefinition; |
---|
184 | } |
---|
185 | if (owner == null) |
---|
186 | throw new InvalidOperationException("Could not determine the type parameter's owner."); |
---|
187 | return new DefaultTypeParameter( |
---|
188 | owner, index, name, variance, |
---|
189 | this.Attributes.CreateResolvedAttributes(context), this.Region, |
---|
190 | this.HasValueTypeConstraint, this.HasReferenceTypeConstraint, this.HasDefaultConstructorConstraint, this.Constraints.Resolve(context) |
---|
191 | ); |
---|
192 | } |
---|
193 | } |
---|
194 | } |
---|