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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultUnresolvedParameter.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.Linq;
 
22
using System.Text;
 
23
using ICSharpCode.NRefactory.Semantics;
 
24
using ICSharpCode.NRefactory.Utils;
 
25
 
 
26
namespace ICSharpCode.NRefactory.TypeSystem.Implementation
 
27
{
 
28
        /// <summary>
 
29
        /// Default implementation for IUnresolvedParameter.
 
30
        /// </summary>
 
31
        [Serializable]
 
32
        public sealed class DefaultUnresolvedParameter : IUnresolvedParameter, IFreezable, ISupportsInterning
 
33
        {
 
34
                string name = string.Empty;
 
35
                ITypeReference type = SpecialType.UnknownType;
 
36
                IList<IUnresolvedAttribute> attributes;
 
37
                IConstantValue defaultValue;
 
38
                DomRegion region;
 
39
                byte flags;
 
40
                
 
41
                public DefaultUnresolvedParameter()
 
42
                {
 
43
                }
 
44
                
 
45
                public DefaultUnresolvedParameter(ITypeReference type, string name)
 
46
                {
 
47
                        if (type == null)
 
48
                                throw new ArgumentNullException("type");
 
49
                        if (name == null)
 
50
                                throw new ArgumentNullException("name");
 
51
                        this.type = type;
 
52
                        this.name = name;
 
53
                }
 
54
                
 
55
                void FreezeInternal()
 
56
                {
 
57
                        attributes = FreezableHelper.FreezeListAndElements(attributes);
 
58
                        FreezableHelper.Freeze(defaultValue);
 
59
                }
 
60
                
 
61
                public string Name {
 
62
                        get { return name; }
 
63
                        set {
 
64
                                if (value == null)
 
65
                                        throw new ArgumentNullException("value");
 
66
                                FreezableHelper.ThrowIfFrozen(this);
 
67
                                name = value;
 
68
                        }
 
69
                }
 
70
                
 
71
                public ITypeReference Type {
 
72
                        get { return type; }
 
73
                        set {
 
74
                                if (value == null)
 
75
                                        throw new ArgumentNullException("value");
 
76
                                FreezableHelper.ThrowIfFrozen(this);
 
77
                                type = value;
 
78
                        }
 
79
                }
 
80
                
 
81
                public IList<IUnresolvedAttribute> Attributes {
 
82
                        get {
 
83
                                if (attributes == null)
 
84
                                        attributes = new List<IUnresolvedAttribute>();
 
85
                                return attributes;
 
86
                        }
 
87
                }
 
88
                
 
89
                public IConstantValue DefaultValue {
 
90
                        get { return defaultValue; }
 
91
                        set {
 
92
                                FreezableHelper.ThrowIfFrozen(this);
 
93
                                defaultValue = value;
 
94
                        }
 
95
                }
 
96
                
 
97
                public DomRegion Region {
 
98
                        get { return region; }
 
99
                        set {
 
100
                                FreezableHelper.ThrowIfFrozen(this);
 
101
                                region = value;
 
102
                        }
 
103
                }
 
104
                
 
105
                bool HasFlag(byte flag)
 
106
                {
 
107
                        return (this.flags & flag) != 0;
 
108
                }
 
109
                void SetFlag(byte flag, bool value)
 
110
                {
 
111
                        FreezableHelper.ThrowIfFrozen(this);
 
112
                        if (value)
 
113
                                this.flags |= flag;
 
114
                        else
 
115
                                this.flags &= unchecked((byte)~flag);
 
116
                }
 
117
                
 
118
                public bool IsFrozen {
 
119
                        get { return HasFlag(1); }
 
120
                }
 
121
                
 
122
                public void Freeze()
 
123
                {
 
124
                        if (!this.IsFrozen) {
 
125
                                FreezeInternal();
 
126
                                this.flags |= 1;
 
127
                        }
 
128
                }
 
129
                
 
130
                public bool IsRef {
 
131
                        get { return HasFlag(2); }
 
132
                        set { SetFlag(2, value); }
 
133
                }
 
134
                
 
135
                public bool IsOut {
 
136
                        get { return HasFlag(4); }
 
137
                        set { SetFlag(4, value); }
 
138
                }
 
139
                
 
140
                public bool IsParams {
 
141
                        get { return HasFlag(8); }
 
142
                        set { SetFlag(8, value); }
 
143
                }
 
144
                
 
145
                public bool IsOptional {
 
146
                        get { return this.DefaultValue != null; }
 
147
                }
 
148
                
 
149
                int ISupportsInterning.GetHashCodeForInterning()
 
150
                {
 
151
                        unchecked {
 
152
                                int hashCode = 1919191 ^ (flags & ~1);
 
153
                                hashCode *= 31;
 
154
                                hashCode += type.GetHashCode();
 
155
                                hashCode *= 31;
 
156
                                hashCode += name.GetHashCode();
 
157
                                hashCode += attributes != null ? attributes.Count : 0;
 
158
                                return hashCode;
 
159
                        }
 
160
                }
 
161
                
 
162
                bool ISupportsInterning.EqualsForInterning(ISupportsInterning other)
 
163
                {
 
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);
 
168
                }
 
169
                
 
170
                static bool ListEquals(IList<IUnresolvedAttribute> list1, IList<IUnresolvedAttribute> list2)
 
171
                {
 
172
                        return (list1 ?? EmptyList<IUnresolvedAttribute>.Instance).SequenceEqual(list2 ?? EmptyList<IUnresolvedAttribute>.Instance);
 
173
                }
 
174
                
 
175
                public override string ToString()
 
176
                {
 
177
                        StringBuilder b = new StringBuilder();
 
178
                        if (IsRef)
 
179
                                b.Append("ref ");
 
180
                        if (IsOut)
 
181
                                b.Append("out ");
 
182
                        if (IsParams)
 
183
                                b.Append("params ");
 
184
                        b.Append(name);
 
185
                        b.Append(':');
 
186
                        b.Append(type.ToString());
 
187
                        if (defaultValue != null) {
 
188
                                b.Append(" = ");
 
189
                                b.Append(defaultValue.ToString());
 
190
                        }
 
191
                        return b.ToString();
 
192
                }
 
193
 
 
194
                static bool IsOptionalAttribute (IType attributeType)
 
195
                {
 
196
                        return attributeType.Name == "OptionalAttribute" && attributeType.Namespace == "System.Runtime.InteropServices";
 
197
                }
 
198
                
 
199
                public IParameter CreateResolvedParameter(ITypeResolveContext context)
 
200
                {
 
201
                        Freeze();
 
202
                        if (defaultValue != null) {
 
203
                                return new ResolvedParameterWithDefaultValue(defaultValue, context) {
 
204
                                        Type = type.Resolve(context),
 
205
                                        Name = name,
 
206
                                        Region = region,
 
207
                                        Attributes = attributes.CreateResolvedAttributes(context),
 
208
                                        IsRef = this.IsRef,
 
209
                                        IsOut = this.IsOut,
 
210
                                        IsParams = this.IsParams
 
211
                                };
 
212
                        } else {
 
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);
 
217
                        }
 
218
                }
 
219
                
 
220
                sealed class ResolvedParameterWithDefaultValue : IParameter
 
221
                {
 
222
                        readonly IConstantValue defaultValue;
 
223
                        readonly ITypeResolveContext context;
 
224
                        
 
225
                        public ResolvedParameterWithDefaultValue(IConstantValue defaultValue, ITypeResolveContext context)
 
226
                        {
 
227
                                this.defaultValue = defaultValue;
 
228
                                this.context = context;
 
229
                        }
 
230
                        
 
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; } }
 
240
                        
 
241
                        ResolveResult resolvedDefaultValue;
 
242
                        
 
243
                        public object ConstantValue {
 
244
                                get {
 
245
                                        ResolveResult rr = LazyInit.VolatileRead(ref this.resolvedDefaultValue);
 
246
                                        if (rr == null) {
 
247
                                                rr = defaultValue.Resolve(context);
 
248
                                                LazyInit.GetOrSet(ref this.resolvedDefaultValue, rr);
 
249
                                        }
 
250
                                        if (rr is ConversionResolveResult) {
 
251
                                                var crr = (ConversionResolveResult)rr;
 
252
                                                if (crr.Conversion.IsNullableConversion)
 
253
                                                        return crr.Input.ConstantValue;
 
254
                                        }
 
255
                                        return rr.ConstantValue;
 
256
 
 
257
                                }
 
258
                        }
 
259
                        
 
260
                        public override string ToString()
 
261
                        {
 
262
                                return DefaultParameter.ToString(this);
 
263
                        }
 
264
                }
 
265
        }
 
266
}