1
ļ»æ// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
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:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
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.
20
using System.Collections.Generic;
23
using ICSharpCode.NRefactory.Semantics;
24
using ICSharpCode.NRefactory.Utils;
26
namespace ICSharpCode.NRefactory.TypeSystem.Implementation
29
/// Default implementation for IUnresolvedParameter.
32
public sealed class DefaultUnresolvedParameter : IUnresolvedParameter, IFreezable, ISupportsInterning
34
string name = string.Empty;
35
ITypeReference type = SpecialType.UnknownType;
36
IList<IUnresolvedAttribute> attributes;
37
IConstantValue defaultValue;
41
public DefaultUnresolvedParameter()
45
public DefaultUnresolvedParameter(ITypeReference type, string name)
48
throw new ArgumentNullException("type");
50
throw new ArgumentNullException("name");
57
attributes = FreezableHelper.FreezeListAndElements(attributes);
58
FreezableHelper.Freeze(defaultValue);
65
throw new ArgumentNullException("value");
66
FreezableHelper.ThrowIfFrozen(this);
71
public ITypeReference Type {
75
throw new ArgumentNullException("value");
76
FreezableHelper.ThrowIfFrozen(this);
81
public IList<IUnresolvedAttribute> Attributes {
83
if (attributes == null)
84
attributes = new List<IUnresolvedAttribute>();
89
public IConstantValue DefaultValue {
90
get { return defaultValue; }
92
FreezableHelper.ThrowIfFrozen(this);
97
public DomRegion Region {
98
get { return region; }
100
FreezableHelper.ThrowIfFrozen(this);
105
bool HasFlag(byte flag)
107
return (this.flags & flag) != 0;
109
void SetFlag(byte flag, bool value)
111
FreezableHelper.ThrowIfFrozen(this);
115
this.flags &= unchecked((byte)~flag);
118
public bool IsFrozen {
119
get { return HasFlag(1); }
124
if (!this.IsFrozen) {
131
get { return HasFlag(2); }
132
set { SetFlag(2, value); }
136
get { return HasFlag(4); }
137
set { SetFlag(4, value); }
140
public bool IsParams {
141
get { return HasFlag(8); }
142
set { SetFlag(8, value); }
145
public bool IsOptional {
146
get { return this.DefaultValue != null; }
149
void ISupportsInterning.PrepareForInterning(IInterningProvider provider)
152
name = provider.Intern(name);
153
type = provider.Intern(type);
154
attributes = provider.InternList(attributes);
155
defaultValue = provider.Intern(defaultValue);
159
int ISupportsInterning.GetHashCodeForInterning()
161
return type.GetHashCode() ^ name.GetHashCode()
162
^ (attributes != null ? attributes.GetHashCode() : 0)
163
^ (defaultValue != null ? defaultValue.GetHashCode() : 0)
164
^ region.GetHashCode() ^ flags;
167
bool ISupportsInterning.EqualsForInterning(ISupportsInterning other)
169
DefaultUnresolvedParameter p = other as DefaultUnresolvedParameter;
170
return p != null && type == p.type && attributes == p.attributes && name == p.name
171
&& defaultValue == p.defaultValue && region == p.region && flags == p.flags;
174
public override string ToString()
176
StringBuilder b = new StringBuilder();
185
b.Append(type.ToString());
186
if (defaultValue != null) {
188
b.Append(defaultValue.ToString());
193
public IParameter CreateResolvedParameter(ITypeResolveContext context)
196
if (defaultValue != null) {
197
return new ResolvedParameterWithDefaultValue(defaultValue, context) {
198
Type = type.Resolve(context),
201
Attributes = attributes.CreateResolvedAttributes(context),
204
IsParams = this.IsParams
207
return new DefaultParameter(type.Resolve(context), name, region,
208
attributes.CreateResolvedAttributes(context), IsRef, IsOut, IsParams);
212
sealed class ResolvedParameterWithDefaultValue : IParameter
214
readonly IConstantValue defaultValue;
215
readonly ITypeResolveContext context;
217
public ResolvedParameterWithDefaultValue(IConstantValue defaultValue, ITypeResolveContext context)
219
this.defaultValue = defaultValue;
220
this.context = context;
223
public IType Type { get; internal set; }
224
public string Name { get; internal set; }
225
public DomRegion Region { get; internal set; }
226
public IList<IAttribute> Attributes { get; internal set; }
227
public bool IsRef { get; internal set; }
228
public bool IsOut { get; internal set; }
229
public bool IsParams { get; internal set; }
230
public bool IsOptional { get { return true; } }
231
bool IVariable.IsConst { get { return false; } }
233
ResolveResult resolvedDefaultValue;
235
public object ConstantValue {
237
ResolveResult rr = LazyInit.VolatileRead(ref this.resolvedDefaultValue);
239
return rr.ConstantValue;
241
rr = defaultValue.Resolve(context);
242
return LazyInit.GetOrSet(ref this.resolvedDefaultValue, rr).ConstantValue;
247
public override string ToString()
249
return DefaultParameter.ToString(this);