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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultUnresolvedTypeParameter.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.Globalization;
 
22
 
 
23
using ICSharpCode.NRefactory.Utils;
 
24
 
 
25
namespace ICSharpCode.NRefactory.TypeSystem.Implementation
 
26
{
 
27
        /// <summary>
 
28
        /// Default implementation of <see cref="IUnresolvedTypeParameter"/>.
 
29
        /// </summary>
 
30
        [Serializable]
 
31
        public class DefaultUnresolvedTypeParameter : IUnresolvedTypeParameter, IFreezable
 
32
        {
 
33
                readonly int index;
 
34
                IList<IUnresolvedAttribute> attributes;
 
35
                IList<ITypeReference> constraints;
 
36
                string name;
 
37
                DomRegion region;
 
38
                
 
39
                EntityType ownerType;
 
40
                VarianceModifier variance;
 
41
                BitVector16 flags;
 
42
                const ushort FlagFrozen                       = 0x0001;
 
43
                const ushort FlagReferenceTypeConstraint      = 0x0002;
 
44
                const ushort FlagValueTypeConstraint          = 0x0004;
 
45
                const ushort FlagDefaultConstructorConstraint = 0x0008;
 
46
                
 
47
                public void Freeze()
 
48
                {
 
49
                        if (!flags[FlagFrozen]) {
 
50
                                FreezeInternal();
 
51
                                flags[FlagFrozen] = true;
 
52
                        }
 
53
                }
 
54
                
 
55
                protected virtual void FreezeInternal()
 
56
                {
 
57
                        attributes = FreezableHelper.FreezeListAndElements(attributes);
 
58
                }
 
59
                
 
60
                public DefaultUnresolvedTypeParameter(EntityType ownerType, int index, string name = null)
 
61
                {
 
62
                        this.ownerType = ownerType;
 
63
                        this.index = index;
 
64
                        this.name = name ?? ((ownerType == EntityType.Method ? "!!" : "!") + index.ToString(CultureInfo.InvariantCulture));
 
65
                }
 
66
                
 
67
                public EntityType OwnerType {
 
68
                        get { return ownerType; }
 
69
                }
 
70
                
 
71
                public int Index {
 
72
                        get { return index; }
 
73
                }
 
74
                
 
75
                public bool IsFrozen {
 
76
                        get { return flags[FlagFrozen]; }
 
77
                }
 
78
                
 
79
                public string Name {
 
80
                        get { return name; }
 
81
                        set {
 
82
                                FreezableHelper.ThrowIfFrozen(this);
 
83
                                name = value;
 
84
                        }
 
85
                }
 
86
                
 
87
                string INamedElement.FullName {
 
88
                        get { return name; }
 
89
                }
 
90
                
 
91
                string INamedElement.Namespace {
 
92
                        get { return string.Empty; }
 
93
                }
 
94
                
 
95
                string INamedElement.ReflectionName {
 
96
                        get {
 
97
                                if (ownerType == EntityType.Method)
 
98
                                        return "``" + index.ToString(CultureInfo.InvariantCulture);
 
99
                                else
 
100
                                        return "`" + index.ToString(CultureInfo.InvariantCulture);
 
101
                        }
 
102
                }
 
103
                
 
104
                public IList<IUnresolvedAttribute> Attributes {
 
105
                        get {
 
106
                                if (attributes == null)
 
107
                                        attributes = new List<IUnresolvedAttribute>();
 
108
                                return attributes;
 
109
                        }
 
110
                }
 
111
                
 
112
                public IList<ITypeReference> Constraints {
 
113
                        get {
 
114
                                if (constraints == null)
 
115
                                        constraints = new List<ITypeReference>();
 
116
                                return constraints;
 
117
                        }
 
118
                }
 
119
                
 
120
                public VarianceModifier Variance {
 
121
                        get { return variance; }
 
122
                        set {
 
123
                                FreezableHelper.ThrowIfFrozen(this);
 
124
                                variance = value;
 
125
                        }
 
126
                }
 
127
                
 
128
                public DomRegion Region {
 
129
                        get { return region; }
 
130
                        set {
 
131
                                FreezableHelper.ThrowIfFrozen(this);
 
132
                                region = value;
 
133
                        }
 
134
                }
 
135
                
 
136
                public bool HasDefaultConstructorConstraint {
 
137
                        get { return flags[FlagDefaultConstructorConstraint]; }
 
138
                        set {
 
139
                                FreezableHelper.ThrowIfFrozen(this);
 
140
                                flags[FlagDefaultConstructorConstraint] = value;
 
141
                        }
 
142
                }
 
143
                
 
144
                public bool HasReferenceTypeConstraint {
 
145
                        get { return flags[FlagReferenceTypeConstraint]; }
 
146
                        set {
 
147
                                FreezableHelper.ThrowIfFrozen(this);
 
148
                                flags[FlagReferenceTypeConstraint] = value;
 
149
                        }
 
150
                }
 
151
                
 
152
                public bool HasValueTypeConstraint {
 
153
                        get { return flags[FlagValueTypeConstraint]; }
 
154
                        set {
 
155
                                FreezableHelper.ThrowIfFrozen(this);
 
156
                                flags[FlagValueTypeConstraint] = value;
 
157
                        }
 
158
                }
 
159
                
 
160
                /// <summary>
 
161
                /// Uses the specified interning provider to intern
 
162
                /// strings and lists in this entity.
 
163
                /// This method does not test arbitrary objects to see if they implement ISupportsInterning;
 
164
                /// instead we assume that those are interned immediately when they are created (before they are added to this entity).
 
165
                /// </summary>
 
166
                public virtual void ApplyInterningProvider(InterningProvider provider)
 
167
                {
 
168
                        if (provider == null)
 
169
                                throw new ArgumentNullException("provider");
 
170
                        FreezableHelper.ThrowIfFrozen(this);
 
171
                        name = provider.Intern(name);
 
172
                        attributes = provider.InternList(attributes);
 
173
                        constraints = provider.InternList(constraints);
 
174
                }
 
175
                
 
176
                public virtual ITypeParameter CreateResolvedTypeParameter(ITypeResolveContext context)
 
177
                {
 
178
                        IEntity owner = null;
 
179
                        if (this.OwnerType == EntityType.Method) {
 
180
                                owner = context.CurrentMember as IMethod;
 
181
                        } else if (this.OwnerType == EntityType.TypeDefinition) {
 
182
                                owner = context.CurrentTypeDefinition;
 
183
                        }
 
184
                        if (owner == null)
 
185
                                throw new InvalidOperationException("Could not determine the type parameter's owner.");
 
186
                        return new DefaultTypeParameter(
 
187
                                owner, index, name, variance,
 
188
                                this.Attributes.CreateResolvedAttributes(context), this.Region,
 
189
                                this.HasValueTypeConstraint, this.HasReferenceTypeConstraint, this.HasDefaultConstructorConstraint, this.Constraints.Resolve(context)
 
190
                        );
 
191
                }
 
192
        }
 
193
}