| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- //
- // MethodReference.cs
- //
- // Author:
- // Jb Evain (jbevain@gmail.com)
- //
- // Copyright (c) 2008 - 2011 Jb Evain
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.Text;
- using Mono.Collections.Generic;
- namespace Mono.Cecil
- {
- public class MethodReference : MemberReference, IMethodSignature, IGenericParameterProvider, IGenericContext
- {
- int hashCode = -1;
- static int instance_id;
- internal ParameterDefinitionCollection parameters;
- MethodReturnType return_type;
- bool has_this;
- bool explicit_this;
- MethodCallingConvention calling_convention;
- internal Collection<GenericParameter> generic_parameters;
- public virtual bool HasThis
- {
- get { return has_this; }
- set { has_this = value; }
- }
- public virtual bool ExplicitThis
- {
- get { return explicit_this; }
- set { explicit_this = value; }
- }
- public virtual MethodCallingConvention CallingConvention
- {
- get { return calling_convention; }
- set { calling_convention = value; }
- }
- public virtual bool HasParameters
- {
- get { return !Mixin.IsNullOrEmpty(parameters); }
- }
- public virtual Collection<ParameterDefinition> Parameters
- {
- get
- {
- if (parameters == null)
- parameters = new ParameterDefinitionCollection(this);
- return parameters;
- }
- }
- IGenericParameterProvider IGenericContext.Type
- {
- get
- {
- var declaring_type = this.DeclaringType;
- var instance = declaring_type as GenericInstanceType;
- if (instance != null)
- return instance.ElementType;
- return declaring_type;
- }
- }
- IGenericParameterProvider IGenericContext.Method
- {
- get { return this; }
- }
- GenericParameterType IGenericParameterProvider.GenericParameterType
- {
- get { return GenericParameterType.Method; }
- }
- public virtual bool HasGenericParameters
- {
- get { return !Mixin.IsNullOrEmpty(generic_parameters); }
- }
- public virtual Collection<GenericParameter> GenericParameters
- {
- get
- {
- if (generic_parameters != null)
- return generic_parameters;
- return generic_parameters = new GenericParameterCollection(this);
- }
- }
- public TypeReference ReturnType
- {
- get
- {
- var return_type = MethodReturnType;
- return return_type != null ? return_type.ReturnType : null;
- }
- set
- {
- var return_type = MethodReturnType;
- if (return_type != null)
- return_type.ReturnType = value;
- }
- }
- public virtual MethodReturnType MethodReturnType
- {
- get { return return_type; }
- set { return_type = value; }
- }
- public override string FullName
- {
- get
- {
- var builder = new StringBuilder();
- builder.Append(ReturnType.FullName)
- .Append(" ")
- .Append(MemberFullName());
- Mixin.MethodSignatureFullName(this, builder);
- return builder.ToString();
- }
- }
- public override int GetHashCode()
- {
- if (hashCode == -1)
- hashCode = System.Threading.Interlocked.Add(ref instance_id, 1);
- return hashCode;
- }
- public virtual bool IsGenericInstance
- {
- get { return false; }
- }
- internal override bool ContainsGenericParameter
- {
- get
- {
- if (this.ReturnType.ContainsGenericParameter || base.ContainsGenericParameter)
- return true;
- var parameters = this.Parameters;
- for (int i = 0; i < parameters.Count; i++)
- if (parameters[i].ParameterType.ContainsGenericParameter)
- return true;
- return false;
- }
- }
- internal MethodReference()
- {
- this.return_type = new MethodReturnType(this);
- this.token = new MetadataToken(TokenType.MemberRef);
- }
- public MethodReference(string name, TypeReference returnType)
- : base(name)
- {
- if (returnType == null)
- throw new ArgumentNullException("returnType");
- this.return_type = new MethodReturnType(this);
- this.return_type.ReturnType = returnType;
- this.token = new MetadataToken(TokenType.MemberRef);
- }
- public MethodReference(string name, TypeReference returnType, TypeReference declaringType)
- : this(name, returnType)
- {
- if (declaringType == null)
- throw new ArgumentNullException("declaringType");
- this.DeclaringType = declaringType;
- }
- public virtual MethodReference GetElementMethod()
- {
- return this;
- }
- public virtual MethodDefinition Resolve()
- {
- var module = this.Module;
- if (module == null)
- throw new NotSupportedException();
- return module.Resolve(this);
- }
- }
- static partial class Mixin
- {
- public static bool IsVarArg(IMethodSignature self)
- {
- return (self.CallingConvention & MethodCallingConvention.VarArg) != 0;
- }
- public static int GetSentinelPosition(IMethodSignature self)
- {
- if (!self.HasParameters)
- return -1;
- var parameters = self.Parameters;
- for (int i = 0; i < parameters.Count; i++)
- if (parameters[i].ParameterType.IsSentinel)
- return i;
- return -1;
- }
- }
- }
|