1
ļ»æ// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
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:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
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.
20
using System.Collections.Generic;
21
using System.Collections.ObjectModel;
22
using System.Diagnostics;
24
using ICSharpCode.NRefactory.CSharp.Resolver;
25
using ICSharpCode.NRefactory.Semantics;
26
using ICSharpCode.NRefactory.TypeSystem;
27
using ICSharpCode.NRefactory.TypeSystem.Implementation;
28
using ICSharpCode.NRefactory.Utils;
30
namespace ICSharpCode.NRefactory.CSharp.TypeSystem
33
public sealed class CSharpAttribute : IUnresolvedAttribute
35
ITypeReference attributeType;
37
IList<IConstantValue> positionalArguments;
38
IList<KeyValuePair<string, IConstantValue>> namedCtorArguments;
39
IList<KeyValuePair<string, IConstantValue>> namedArguments;
41
public CSharpAttribute(ITypeReference attributeType, DomRegion region,
42
IList<IConstantValue> positionalArguments,
43
IList<KeyValuePair<string, IConstantValue>> namedCtorArguments,
44
IList<KeyValuePair<string, IConstantValue>> namedArguments)
46
if (attributeType == null)
47
throw new ArgumentNullException("attributeType");
48
this.attributeType = attributeType;
50
this.positionalArguments = positionalArguments ?? EmptyList<IConstantValue>.Instance;
51
this.namedCtorArguments = namedCtorArguments ?? EmptyList<KeyValuePair<string, IConstantValue>>.Instance;
52
this.namedArguments = namedArguments ?? EmptyList<KeyValuePair<string, IConstantValue>>.Instance;
55
public DomRegion Region {
56
get { return region; }
59
public ITypeReference AttributeType {
60
get { return attributeType; }
63
public IAttribute CreateResolvedAttribute(ITypeResolveContext context)
65
return new CSharpResolvedAttribute((CSharpTypeResolveContext)context, this);
68
sealed class CSharpResolvedAttribute : IAttribute
70
readonly CSharpTypeResolveContext context;
71
readonly CSharpAttribute unresolved;
72
readonly IType attributeType;
74
IList<KeyValuePair<IMember, ResolveResult>> namedArguments;
76
public CSharpResolvedAttribute(CSharpTypeResolveContext context, CSharpAttribute unresolved)
78
this.context = context;
79
this.unresolved = unresolved;
80
// Pretty much any access to the attribute checks the type first, so
81
// we don't need to use lazy-loading for that.
82
this.attributeType = unresolved.AttributeType.Resolve(context);
85
DomRegion IAttribute.Region {
86
get { return unresolved.Region; }
89
IType IAttribute.AttributeType {
90
get { return attributeType; }
93
ResolveResult ctorInvocation;
95
InvocationResolveResult GetCtorInvocation()
97
ResolveResult rr = LazyInit.VolatileRead(ref this.ctorInvocation);
99
return rr as InvocationResolveResult;
101
CSharpResolver resolver = new CSharpResolver(context);
102
int totalArgumentCount = unresolved.positionalArguments.Count + unresolved.namedCtorArguments.Count;
103
ResolveResult[] arguments = new ResolveResult[totalArgumentCount];
104
string[] argumentNames = new string[totalArgumentCount];
106
while (i < unresolved.positionalArguments.Count) {
107
IConstantValue cv = unresolved.positionalArguments[i];
108
arguments[i] = cv.Resolve(context);
111
foreach (var pair in unresolved.namedCtorArguments) {
112
argumentNames[i] = pair.Key;
113
arguments[i] = pair.Value.Resolve(context);
116
rr = resolver.ResolveObjectCreation(attributeType, arguments, argumentNames);
117
return LazyInit.GetOrSet(ref this.ctorInvocation, rr) as InvocationResolveResult;
121
IMethod IAttribute.Constructor {
123
var invocation = GetCtorInvocation();
124
if (invocation != null)
125
return invocation.Member as IMethod;
131
IList<ResolveResult> positionalArguments;
133
IList<ResolveResult> IAttribute.PositionalArguments {
135
var result = LazyInit.VolatileRead(ref this.positionalArguments);
136
if (result != null) {
139
var invocation = GetCtorInvocation();
140
if (invocation != null)
141
result = invocation.GetArgumentsForCall();
143
result = EmptyList<ResolveResult>.Instance;
144
return LazyInit.GetOrSet(ref this.positionalArguments, result);
149
IList<KeyValuePair<IMember, ResolveResult>> IAttribute.NamedArguments {
151
var namedArgs = LazyInit.VolatileRead(ref this.namedArguments);
152
if (namedArgs != null) {
155
namedArgs = new List<KeyValuePair<IMember, ResolveResult>>();
156
foreach (var pair in unresolved.namedArguments) {
157
IMember member = attributeType.GetMembers(m => (m.EntityType == EntityType.Field || m.EntityType == EntityType.Property) && m.Name == pair.Key).FirstOrDefault();
158
if (member != null) {
159
ResolveResult val = pair.Value.Resolve(context);
160
namedArgs.Add(new KeyValuePair<IMember, ResolveResult>(member, val));
163
return LazyInit.GetOrSet(ref this.namedArguments, namedArgs);
b'\\ No newline at end of file'