~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.NRefactory.Semantics;
using ICSharpCode.NRefactory.Utils;

namespace ICSharpCode.NRefactory.TypeSystem.Implementation
{
	/// <summary>
	/// Default implementation for IUnresolvedParameter.
	/// </summary>
	[Serializable]
	public sealed class DefaultUnresolvedParameter : IUnresolvedParameter, IFreezable, ISupportsInterning
	{
		string name = string.Empty;
		ITypeReference type = SpecialType.UnknownType;
		IList<IUnresolvedAttribute> attributes;
		IConstantValue defaultValue;
		DomRegion region;
		byte flags;
		
		public DefaultUnresolvedParameter()
		{
		}
		
		public DefaultUnresolvedParameter(ITypeReference type, string name)
		{
			if (type == null)
				throw new ArgumentNullException("type");
			if (name == null)
				throw new ArgumentNullException("name");
			this.type = type;
			this.name = name;
		}
		
		void FreezeInternal()
		{
			attributes = FreezableHelper.FreezeListAndElements(attributes);
			FreezableHelper.Freeze(defaultValue);
		}
		
		public string Name {
			get { return name; }
			set {
				if (value == null)
					throw new ArgumentNullException("value");
				FreezableHelper.ThrowIfFrozen(this);
				name = value;
			}
		}
		
		public ITypeReference Type {
			get { return type; }
			set {
				if (value == null)
					throw new ArgumentNullException("value");
				FreezableHelper.ThrowIfFrozen(this);
				type = value;
			}
		}
		
		public IList<IUnresolvedAttribute> Attributes {
			get {
				if (attributes == null)
					attributes = new List<IUnresolvedAttribute>();
				return attributes;
			}
		}
		
		public IConstantValue DefaultValue {
			get { return defaultValue; }
			set {
				FreezableHelper.ThrowIfFrozen(this);
				defaultValue = value;
			}
		}
		
		public DomRegion Region {
			get { return region; }
			set {
				FreezableHelper.ThrowIfFrozen(this);
				region = value;
			}
		}
		
		bool HasFlag(byte flag)
		{
			return (this.flags & flag) != 0;
		}
		void SetFlag(byte flag, bool value)
		{
			FreezableHelper.ThrowIfFrozen(this);
			if (value)
				this.flags |= flag;
			else
				this.flags &= unchecked((byte)~flag);
		}
		
		public bool IsFrozen {
			get { return HasFlag(1); }
		}
		
		public void Freeze()
		{
			if (!this.IsFrozen) {
				FreezeInternal();
				this.flags |= 1;
			}
		}
		
		public bool IsRef {
			get { return HasFlag(2); }
			set { SetFlag(2, value); }
		}
		
		public bool IsOut {
			get { return HasFlag(4); }
			set { SetFlag(4, value); }
		}
		
		public bool IsParams {
			get { return HasFlag(8); }
			set { SetFlag(8, value); }
		}
		
		public bool IsOptional {
			get { return this.DefaultValue != null; }
		}
		
		int ISupportsInterning.GetHashCodeForInterning()
		{
			unchecked {
				int hashCode = 1919191 ^ (flags & ~1);
				hashCode *= 31;
				hashCode += type.GetHashCode();
				hashCode *= 31;
				hashCode += name.GetHashCode();
				hashCode += attributes != null ? attributes.Count : 0;
				return hashCode;
			}
		}
		
		bool ISupportsInterning.EqualsForInterning(ISupportsInterning other)
		{
			// compare everything except for the IsFrozen flag
			DefaultUnresolvedParameter p = other as DefaultUnresolvedParameter;
			return p != null && type == p.type && name == p.name && ListEquals(attributes, p.attributes)
				&& defaultValue == p.defaultValue && region == p.region && (flags & ~1) == (p.flags & ~1);
		}
		
		static bool ListEquals(IList<IUnresolvedAttribute> list1, IList<IUnresolvedAttribute> list2)
		{
			return (list1 ?? EmptyList<IUnresolvedAttribute>.Instance).SequenceEqual(list2 ?? EmptyList<IUnresolvedAttribute>.Instance);
		}
		
		public override string ToString()
		{
			StringBuilder b = new StringBuilder();
			if (IsRef)
				b.Append("ref ");
			if (IsOut)
				b.Append("out ");
			if (IsParams)
				b.Append("params ");
			b.Append(name);
			b.Append(':');
			b.Append(type.ToString());
			if (defaultValue != null) {
				b.Append(" = ");
				b.Append(defaultValue.ToString());
			}
			return b.ToString();
		}

		static bool IsOptionalAttribute (IType attributeType)
		{
			return attributeType.Name == "OptionalAttribute" && attributeType.Namespace == "System.Runtime.InteropServices";
		}
		
		public IParameter CreateResolvedParameter(ITypeResolveContext context)
		{
			Freeze();
			if (defaultValue != null) {
				return new ResolvedParameterWithDefaultValue(defaultValue, context) {
					Type = type.Resolve(context),
					Name = name,
					Region = region,
					Attributes = attributes.CreateResolvedAttributes(context),
					IsRef = this.IsRef,
					IsOut = this.IsOut,
					IsParams = this.IsParams
				};
			} else {
				var resolvedAttributes = attributes.CreateResolvedAttributes (context);
				bool isOptional = resolvedAttributes != null && resolvedAttributes.Any (a => IsOptionalAttribute (a.AttributeType));
				return new DefaultParameter (type.Resolve (context), name, region,
				                            resolvedAttributes, IsRef, IsOut, IsParams, isOptional);
			}
		}
		
		sealed class ResolvedParameterWithDefaultValue : IParameter
		{
			readonly IConstantValue defaultValue;
			readonly ITypeResolveContext context;
			
			public ResolvedParameterWithDefaultValue(IConstantValue defaultValue, ITypeResolveContext context)
			{
				this.defaultValue = defaultValue;
				this.context = context;
			}
			
			public IType Type { get; internal set; }
			public string Name { get; internal set; }
			public DomRegion Region { get; internal set; }
			public IList<IAttribute> Attributes { get; internal set; }
			public bool IsRef { get; internal set; }
			public bool IsOut { get; internal set; }
			public bool IsParams { get; internal set; }
			public bool IsOptional { get { return true; } }
			bool IVariable.IsConst { get { return false; } }
			
			ResolveResult resolvedDefaultValue;
			
			public object ConstantValue {
				get {
					ResolveResult rr = LazyInit.VolatileRead(ref this.resolvedDefaultValue);
					if (rr == null) {
						rr = defaultValue.Resolve(context);
						LazyInit.GetOrSet(ref this.resolvedDefaultValue, rr);
					}
					if (rr is ConversionResolveResult) {
						var crr = (ConversionResolveResult)rr;
						if (crr.Conversion.IsNullableConversion)
							return crr.Input.ConstantValue;
					}
					return rr.ConstantValue;

				}
			}
			
			public override string ToString()
			{
				return DefaultParameter.ToString(this);
			}
		}
	}
}