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.Text; |
---|
22 | |
---|
23 | namespace ICSharpCode.NRefactory.TypeSystem.Implementation |
---|
24 | { |
---|
25 | /// <summary> |
---|
26 | /// Default implementation of <see cref="IParameter"/>. |
---|
27 | /// </summary> |
---|
28 | public sealed class DefaultParameter : IParameter |
---|
29 | { |
---|
30 | readonly IType type; |
---|
31 | readonly string name; |
---|
32 | readonly DomRegion region; |
---|
33 | readonly IList<IAttribute> attributes; |
---|
34 | readonly bool isRef, isOut, isParams, isOptional; |
---|
35 | readonly object defaultValue; |
---|
36 | readonly IParameterizedMember owner; |
---|
37 | |
---|
38 | public DefaultParameter(IType type, string name) |
---|
39 | { |
---|
40 | if (type == null) |
---|
41 | throw new ArgumentNullException("type"); |
---|
42 | if (name == null) |
---|
43 | throw new ArgumentNullException("name"); |
---|
44 | this.type = type; |
---|
45 | this.name = name; |
---|
46 | } |
---|
47 | |
---|
48 | public DefaultParameter(IType type, string name, IParameterizedMember owner = null, DomRegion region = default(DomRegion), IList<IAttribute> attributes = null, |
---|
49 | bool isRef = false, bool isOut = false, bool isParams = false, bool isOptional = false, object defaultValue = null) |
---|
50 | { |
---|
51 | if (type == null) |
---|
52 | throw new ArgumentNullException("type"); |
---|
53 | if (name == null) |
---|
54 | throw new ArgumentNullException("name"); |
---|
55 | this.type = type; |
---|
56 | this.name = name; |
---|
57 | this.owner = owner; |
---|
58 | this.region = region; |
---|
59 | this.attributes = attributes; |
---|
60 | this.isRef = isRef; |
---|
61 | this.isOut = isOut; |
---|
62 | this.isParams = isParams; |
---|
63 | this.isOptional = isOptional; |
---|
64 | this.defaultValue = defaultValue; |
---|
65 | } |
---|
66 | |
---|
67 | SymbolKind ISymbol.SymbolKind { |
---|
68 | get { return SymbolKind.Parameter; } |
---|
69 | } |
---|
70 | |
---|
71 | public IParameterizedMember Owner { |
---|
72 | get { return owner; } |
---|
73 | } |
---|
74 | |
---|
75 | public IList<IAttribute> Attributes { |
---|
76 | get { return attributes; } |
---|
77 | } |
---|
78 | |
---|
79 | public bool IsRef { |
---|
80 | get { return isRef; } |
---|
81 | } |
---|
82 | |
---|
83 | public bool IsOut { |
---|
84 | get { return isOut; } |
---|
85 | } |
---|
86 | |
---|
87 | public bool IsParams { |
---|
88 | get { return isParams; } |
---|
89 | } |
---|
90 | |
---|
91 | public bool IsOptional { |
---|
92 | get { return isOptional; } |
---|
93 | } |
---|
94 | |
---|
95 | public string Name { |
---|
96 | get { return name; } |
---|
97 | } |
---|
98 | |
---|
99 | public DomRegion Region { |
---|
100 | get { return region; } |
---|
101 | } |
---|
102 | |
---|
103 | public IType Type { |
---|
104 | get { return type; } |
---|
105 | } |
---|
106 | |
---|
107 | bool IVariable.IsConst { |
---|
108 | get { return false; } |
---|
109 | } |
---|
110 | |
---|
111 | public object ConstantValue { |
---|
112 | get { return defaultValue; } |
---|
113 | } |
---|
114 | |
---|
115 | public override string ToString() |
---|
116 | { |
---|
117 | return ToString(this); |
---|
118 | } |
---|
119 | |
---|
120 | public static string ToString(IParameter parameter) |
---|
121 | { |
---|
122 | StringBuilder b = new StringBuilder(); |
---|
123 | if (parameter.IsRef) |
---|
124 | b.Append("ref "); |
---|
125 | if (parameter.IsOut) |
---|
126 | b.Append("out "); |
---|
127 | if (parameter.IsParams) |
---|
128 | b.Append("params "); |
---|
129 | b.Append(parameter.Name); |
---|
130 | b.Append(':'); |
---|
131 | b.Append(parameter.Type.ReflectionName); |
---|
132 | if (parameter.IsOptional) { |
---|
133 | b.Append(" = "); |
---|
134 | if (parameter.ConstantValue != null) |
---|
135 | b.Append(parameter.ConstantValue.ToString()); |
---|
136 | else |
---|
137 | b.Append("null"); |
---|
138 | } |
---|
139 | return b.ToString(); |
---|
140 | } |
---|
141 | |
---|
142 | public ISymbolReference ToReference() |
---|
143 | { |
---|
144 | if (owner == null) |
---|
145 | return new ParameterReference(type.ToTypeReference(), name, region, isRef, isOut, isParams, isOptional, defaultValue); |
---|
146 | return new OwnedParameterReference(owner.ToReference(), owner.Parameters.IndexOf(this)); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | sealed class OwnedParameterReference : ISymbolReference |
---|
151 | { |
---|
152 | readonly IMemberReference memberReference; |
---|
153 | readonly int index; |
---|
154 | |
---|
155 | public OwnedParameterReference(IMemberReference member, int index) |
---|
156 | { |
---|
157 | if (member == null) |
---|
158 | throw new ArgumentNullException("member"); |
---|
159 | this.memberReference = member; |
---|
160 | this.index = index; |
---|
161 | } |
---|
162 | |
---|
163 | public ISymbol Resolve(ITypeResolveContext context) |
---|
164 | { |
---|
165 | IParameterizedMember member = memberReference.Resolve(context) as IParameterizedMember; |
---|
166 | if (member != null && index >= 0 && index < member.Parameters.Count) |
---|
167 | return member.Parameters[index]; |
---|
168 | else |
---|
169 | return null; |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | public sealed class ParameterReference : ISymbolReference |
---|
174 | { |
---|
175 | readonly ITypeReference type; |
---|
176 | readonly string name; |
---|
177 | readonly DomRegion region; |
---|
178 | readonly bool isRef, isOut, isParams, isOptional; |
---|
179 | readonly object defaultValue; |
---|
180 | |
---|
181 | public ParameterReference(ITypeReference type, string name, DomRegion region, bool isRef, bool isOut, bool isParams, bool isOptional, object defaultValue) |
---|
182 | { |
---|
183 | if (type == null) |
---|
184 | throw new ArgumentNullException("type"); |
---|
185 | if (name == null) |
---|
186 | throw new ArgumentNullException("name"); |
---|
187 | this.type = type; |
---|
188 | this.name = name; |
---|
189 | this.region = region; |
---|
190 | this.isRef = isRef; |
---|
191 | this.isOut = isOut; |
---|
192 | this.isParams = isParams; |
---|
193 | this.isOptional = isOptional; |
---|
194 | this.defaultValue = defaultValue; |
---|
195 | } |
---|
196 | |
---|
197 | public ISymbol Resolve(ITypeResolveContext context) |
---|
198 | { |
---|
199 | return new DefaultParameter(type.Resolve(context), name, region: region, isRef: isRef, isOut: isOut, isParams: isParams, isOptional: isOptional, defaultValue: defaultValue); |
---|
200 | } |
---|
201 | } |
---|
202 | } |
---|