1
ļ»æ// Copyright (c) 2010-2013 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
int ISupportsInterning.GetHashCodeForInterning()
152
int hashCode = 1919191 ^ (flags & ~1);
154
hashCode += type.GetHashCode();
156
hashCode += name.GetHashCode();
157
hashCode += attributes != null ? attributes.Count : 0;
162
bool ISupportsInterning.EqualsForInterning(ISupportsInterning other)
164
// compare everything except for the IsFrozen flag
165
DefaultUnresolvedParameter p = other as DefaultUnresolvedParameter;
166
return p != null && type == p.type && name == p.name && ListEquals(attributes, p.attributes)
167
&& defaultValue == p.defaultValue && region == p.region && (flags & ~1) == (p.flags & ~1);
170
static bool ListEquals(IList<IUnresolvedAttribute> list1, IList<IUnresolvedAttribute> list2)
172
return (list1 ?? EmptyList<IUnresolvedAttribute>.Instance).SequenceEqual(list2 ?? EmptyList<IUnresolvedAttribute>.Instance);
175
public override string ToString()
177
StringBuilder b = new StringBuilder();
186
b.Append(type.ToString());
187
if (defaultValue != null) {
189
b.Append(defaultValue.ToString());
194
static bool IsOptionalAttribute (IType attributeType)
196
return attributeType.Name == "OptionalAttribute" && attributeType.Namespace == "System.Runtime.InteropServices";
199
public IParameter CreateResolvedParameter(ITypeResolveContext context)
202
if (defaultValue != null) {
203
return new ResolvedParameterWithDefaultValue(defaultValue, context) {
204
Type = type.Resolve(context),
207
Attributes = attributes.CreateResolvedAttributes(context),
210
IsParams = this.IsParams
213
var resolvedAttributes = attributes.CreateResolvedAttributes (context);
214
bool isOptional = resolvedAttributes != null && resolvedAttributes.Any (a => IsOptionalAttribute (a.AttributeType));
215
return new DefaultParameter (type.Resolve (context), name, region,
216
resolvedAttributes, IsRef, IsOut, IsParams, isOptional);
220
sealed class ResolvedParameterWithDefaultValue : IParameter
222
readonly IConstantValue defaultValue;
223
readonly ITypeResolveContext context;
225
public ResolvedParameterWithDefaultValue(IConstantValue defaultValue, ITypeResolveContext context)
227
this.defaultValue = defaultValue;
228
this.context = context;
231
public IType Type { get; internal set; }
232
public string Name { get; internal set; }
233
public DomRegion Region { get; internal set; }
234
public IList<IAttribute> Attributes { get; internal set; }
235
public bool IsRef { get; internal set; }
236
public bool IsOut { get; internal set; }
237
public bool IsParams { get; internal set; }
238
public bool IsOptional { get { return true; } }
239
bool IVariable.IsConst { get { return false; } }
241
ResolveResult resolvedDefaultValue;
243
public object ConstantValue {
245
ResolveResult rr = LazyInit.VolatileRead(ref this.resolvedDefaultValue);
247
rr = defaultValue.Resolve(context);
248
LazyInit.GetOrSet(ref this.resolvedDefaultValue, rr);
250
if (rr is ConversionResolveResult) {
251
var crr = (ConversionResolveResult)rr;
252
if (crr.Conversion.IsNullableConversion)
253
return crr.Input.ConstantValue;
255
return rr.ConstantValue;
260
public override string ToString()
262
return DefaultParameter.ToString(this);