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

« back to all changes in this revision

Viewing changes to contrib/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) 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.Text;
22
 
 
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
 
                void ISupportsInterning.PrepareForInterning(IInterningProvider provider)
150
 
                {
151
 
                        if (!IsFrozen) {
152
 
                                name = provider.Intern(name);
153
 
                                type = provider.Intern(type);
154
 
                                attributes = provider.InternList(attributes);
155
 
                                defaultValue = provider.Intern(defaultValue);
156
 
                        }
157
 
                }
158
 
                
159
 
                int ISupportsInterning.GetHashCodeForInterning()
160
 
                {
161
 
                        return type.GetHashCode() ^ name.GetHashCode()
162
 
                                ^ (attributes != null ? attributes.GetHashCode() : 0)
163
 
                                ^ (defaultValue != null ? defaultValue.GetHashCode() : 0)
164
 
                                ^ region.GetHashCode() ^ flags;
165
 
                }
166
 
                
167
 
                bool ISupportsInterning.EqualsForInterning(ISupportsInterning other)
168
 
                {
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;
172
 
                }
173
 
                
174
 
                public override string ToString()
175
 
                {
176
 
                        StringBuilder b = new StringBuilder();
177
 
                        if (IsRef)
178
 
                                b.Append("ref ");
179
 
                        if (IsOut)
180
 
                                b.Append("out ");
181
 
                        if (IsParams)
182
 
                                b.Append("params ");
183
 
                        b.Append(name);
184
 
                        b.Append(':');
185
 
                        b.Append(type.ToString());
186
 
                        if (defaultValue != null) {
187
 
                                b.Append(" = ");
188
 
                                b.Append(defaultValue.ToString());
189
 
                        }
190
 
                        return b.ToString();
191
 
                }
192
 
                
193
 
                public IParameter CreateResolvedParameter(ITypeResolveContext context)
194
 
                {
195
 
                        Freeze();
196
 
                        if (defaultValue != null) {
197
 
                                return new ResolvedParameterWithDefaultValue(defaultValue, context) {
198
 
                                        Type = type.Resolve(context),
199
 
                                        Name = name,
200
 
                                        Region = region,
201
 
                                        Attributes = attributes.CreateResolvedAttributes(context),
202
 
                                        IsRef = this.IsRef,
203
 
                                        IsOut = this.IsOut,
204
 
                                        IsParams = this.IsParams
205
 
                                };
206
 
                        } else {
207
 
                                return new DefaultParameter(type.Resolve(context), name, region,
208
 
                                                            attributes.CreateResolvedAttributes(context), IsRef, IsOut, IsParams);
209
 
                        }
210
 
                }
211
 
                
212
 
                sealed class ResolvedParameterWithDefaultValue : IParameter
213
 
                {
214
 
                        readonly IConstantValue defaultValue;
215
 
                        readonly ITypeResolveContext context;
216
 
                        
217
 
                        public ResolvedParameterWithDefaultValue(IConstantValue defaultValue, ITypeResolveContext context)
218
 
                        {
219
 
                                this.defaultValue = defaultValue;
220
 
                                this.context = context;
221
 
                        }
222
 
                        
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; } }
232
 
                        
233
 
                        ResolveResult resolvedDefaultValue;
234
 
                        
235
 
                        public object ConstantValue {
236
 
                                get {
237
 
                                        ResolveResult rr = LazyInit.VolatileRead(ref this.resolvedDefaultValue);
238
 
                                        if (rr != null) {
239
 
                                                return rr.ConstantValue;
240
 
                                        } else {
241
 
                                                rr = defaultValue.Resolve(context);
242
 
                                                return LazyInit.GetOrSet(ref this.resolvedDefaultValue, rr).ConstantValue;
243
 
                                        }
244
 
                                }
245
 
                        }
246
 
                        
247
 
                        public override string ToString()
248
 
                        {
249
 
                                return DefaultParameter.ToString(this);
250
 
                        }
251
 
                }
252
 
        }
253
 
}