Пример #1
0
        public bool TryGetParameterReference(CSharpParameter semanticParameter, ushort index, out IParameterTypeInformation cciParameter)
        {
            cciParameter = null;

            #region Check input
            if (semanticParameter == null)
            {
                return(false);
            }
            #endregion
            #region Set up our working cci parameter
            Microsoft.Cci.MutableCodeModel.ParameterTypeInformation workingParameter = null;
            cciParameter = workingParameter = new Microsoft.Cci.MutableCodeModel.ParameterTypeInformation();
            #endregion
            #region Get our parameter type
            ITypeReference paramType;
            if (!TryGetTypeReference(semanticParameter.Type, out paramType))
            {
                goto ReturnFalse;
            }
            workingParameter.Type = paramType;
            #endregion
            #region Get our index
            workingParameter.Index = index;
            #endregion
            #region Get our reference status
            workingParameter.IsByReference = semanticParameter.IsOut || semanticParameter.IsRef;
            #endregion
            #region ReturnTrue:
            //ReturnTrue:
            return(cciParameter != Dummy.ParameterTypeInformation);

            #endregion
            #region ReturnFalse:
ReturnFalse:
            if (semanticParameter.Name != null)
            {
                ContractsPackageAccessor.Current.Logger.WriteToLog("Failed to build parameter reference for: " + semanticParameter.Name.Text);
            }
            return(false);

            #endregion
        }
    public bool TryGetPropertyAccessorReferences(ISymbol semanticProperty, out IMethodReference getter, out IMethodReference setter) {
      Contract.Requires(semanticProperty == null || semanticProperty.Kind == SymbolKind.Property);

      getter = setter = null;

      #region Check input
      if (semanticProperty == null) {
        return false;
      }
      #endregion
      #region Check cache
      Tuple<IMethodReference, IMethodReference> cacheOutput;
      if (_semanticPropertiesToCCIAccessorMethods.TryGetValue(semanticProperty, out cacheOutput)) {
        Contract.Assume(cacheOutput != null, "Non-null only dictionary");
        getter = cacheOutput.Item1;
        setter = cacheOutput.Item2;
        return (getter != null && getter != Dummy.MethodReference) || (setter != null && setter != Dummy.MethodReference);
      }
      #endregion
      #region Get calling conventions
      var callingConventions = CSharpToCCIHelper.GetCallingConventionFor(semanticProperty);
      #endregion
      #region get containing type
      ITypeReference containingType;
      if (!TryGetTypeReference(semanticProperty.ContainingType, out containingType))
        goto ReturnFalse;
      #endregion
      #region Get return type
      ITypeReference returnType;
      if (!TryGetTypeReference(semanticProperty.ReturnType(), out returnType))
        goto ReturnFalse;
      #endregion
      #region Get the property's name
      string propertyName;
      if (semanticProperty.IsIndexer())
        propertyName = "Item";
      else
      {
        if (semanticProperty.Name == null) goto ReturnFalse;
        propertyName = semanticProperty.Name;
      }
      #endregion
      #region Get the parameters
      List<IParameterTypeInformation> getterParams;
      if (!semanticProperty.Parameters().IsDefault) {
        if (!TryGetParametersList(semanticProperty.Parameters(), out getterParams))
          goto ReturnFalse;
      } else
        getterParams = new List<IParameterTypeInformation>();

      List<IParameterTypeInformation> setterParams;
      if (!semanticProperty.Parameters().IsDefault) {
        if (!TryGetParametersList(semanticProperty.Parameters(), out setterParams, 1))
          goto ReturnFalse;
        #region Append the "value" param
        var valParam = new Microsoft.Cci.MutableCodeModel.ParameterTypeInformation() {
          Type = returnType,
          Index = 0,
          IsByReference = false
        };
        setterParams.Insert(0, valParam);
        #endregion
      } else
        setterParams = new List<IParameterTypeInformation>();
      #endregion
      #region Build the getter
      getter = new Microsoft.Cci.MutableCodeModel.MethodReference() {
        InternFactory = Host.InternFactory,
        CallingConvention = callingConventions,
        ContainingType = containingType,
        Type = returnType,
        Name = Host.NameTable.GetNameFor("get_" + propertyName),
        GenericParameterCount = 0,
        Parameters = getterParams
      };
      #endregion
      #region Build the setter
      setter = new Microsoft.Cci.MutableCodeModel.MethodReference() {
        InternFactory = Host.InternFactory,
        CallingConvention = callingConventions,
        ContainingType = containingType,
        Type = Host.PlatformType.SystemVoid,
        Name = Host.NameTable.GetNameFor("set_" + propertyName),
        GenericParameterCount = 0,
        Parameters = setterParams
      };
      #endregion
      #region Get the assembly reference (this also ensures the assembly gets loaded properly)
      IAssemblyReference assemblyReference;
      TryGetAssemblyReference(semanticProperty.ContainingAssembly, out assemblyReference);
      #endregion
      #region ReturnTrue:
    //ReturnTrue:
      _semanticPropertiesToCCIAccessorMethods[semanticProperty] = new Tuple<IMethodReference, IMethodReference>(getter, setter);
      return true;
      #endregion
      #region ReturnFalse:
    ReturnFalse:
      if (semanticProperty.Name != null)
      {
        ContractsPackageAccessor.Current.Logger.WriteToLog("Failed to build accessor references for: " + semanticProperty.Name);
      }
      _semanticPropertiesToCCIAccessorMethods[semanticProperty] = new Tuple<IMethodReference, IMethodReference>(Dummy.MethodReference, Dummy.MethodReference);
      return false;
      #endregion
    }
    public bool TryGetParameterReference(IParameterSymbol semanticParameter, ushort index, out IParameterTypeInformation cciParameter) {

      cciParameter = null;

      #region Check input
      if (semanticParameter == null) {
        return false;
      }
      #endregion
      #region Set up our working cci parameter
      Microsoft.Cci.MutableCodeModel.ParameterTypeInformation workingParameter = null;
      cciParameter = workingParameter = new Microsoft.Cci.MutableCodeModel.ParameterTypeInformation();
      #endregion
      #region Get our parameter type
      ITypeReference paramType;
      if (!TryGetTypeReference(semanticParameter.Type, out paramType))
        goto ReturnFalse;
      workingParameter.Type = paramType;
      #endregion
      #region Get our index
      workingParameter.Index = index;
      #endregion
      #region Get our reference status
      workingParameter.IsByReference = semanticParameter.RefKind == RefKind.Out || semanticParameter.RefKind == RefKind.Ref;
      #endregion
      #region ReturnTrue:
    //ReturnTrue:
      return cciParameter != Dummy.ParameterTypeInformation;
      #endregion
      #region ReturnFalse:
    ReturnFalse:
      if (semanticParameter.Name != null)
      {
        ContractsPackageAccessor.Current.Logger.WriteToLog("Failed to build parameter reference for: " + semanticParameter.Name);
      }
      return false;
      #endregion
    }
Пример #4
0
        public bool TryGetPropertyAccessorReferences(CSharpMember semanticProperty, out IMethodReference getter, out IMethodReference setter)
        {
            Contract.Requires(semanticProperty == null || semanticProperty.IsProperty || semanticProperty.IsIndexer);

            getter = setter = null;

            #region Check input
            if (semanticProperty == null)
            {
                return(false);
            }
            #endregion
            #region Check cache
            Tuple <IMethodReference, IMethodReference> cacheOutput;
            if (_semanticPropertiesToCCIAccessorMethods.TryGetValue(semanticProperty, out cacheOutput))
            {
                Contract.Assume(cacheOutput != null, "Non-null only dictionary");
                getter = cacheOutput.Item1;
                setter = cacheOutput.Item2;
                return((getter != null && getter != Dummy.MethodReference) || (setter != null && setter != Dummy.MethodReference));
            }
            #endregion
            #region Get calling conventions
            var callingConventions = CSharpToCCIHelper.GetCallingConventionFor(semanticProperty);
            #endregion
            #region get containing type
            ITypeReference containingType;
            if (!TryGetTypeReference(semanticProperty.ContainingType, out containingType))
            {
                goto ReturnFalse;
            }
            #endregion
            #region Get return type
            ITypeReference returnType;
            if (!TryGetTypeReference(semanticProperty.ReturnType, out returnType))
            {
                goto ReturnFalse;
            }
            #endregion
            #region Get the property's name
            string propertyName;
            if (semanticProperty.IsIndexer)
            {
                propertyName = "Item";
            }
            else
            {
                if (semanticProperty.Name == null)
                {
                    goto ReturnFalse;
                }
                propertyName = semanticProperty.Name.Text;
            }
            #endregion
            #region Get the parameters
            List <IParameterTypeInformation> getterParams;
            if (semanticProperty.Parameters != null)
            {
                if (!TryGetParametersList(semanticProperty.Parameters, out getterParams))
                {
                    goto ReturnFalse;
                }
            }
            else
            {
                getterParams = new List <IParameterTypeInformation>();
            }

            List <IParameterTypeInformation> setterParams;
            if (semanticProperty.Parameters != null)
            {
                if (!TryGetParametersList(semanticProperty.Parameters, out setterParams, 1))
                {
                    goto ReturnFalse;
                }
                #region Append the "value" param
                var valParam = new Microsoft.Cci.MutableCodeModel.ParameterTypeInformation()
                {
                    Type          = returnType,
                    Index         = 0,
                    IsByReference = false
                };
                setterParams.Insert(0, valParam);
                #endregion
            }
            else
            {
                setterParams = new List <IParameterTypeInformation>();
            }
            #endregion
            #region Build the getter
            getter = new Microsoft.Cci.MutableCodeModel.MethodReference()
            {
                InternFactory     = Host.InternFactory,
                CallingConvention = callingConventions,
                ContainingType    = containingType,
                Type = returnType,
                Name = Host.NameTable.GetNameFor("get_" + propertyName),
                GenericParameterCount = 0,
                Parameters            = getterParams
            };
            #endregion
            #region Build the setter
            setter = new Microsoft.Cci.MutableCodeModel.MethodReference()
            {
                InternFactory     = Host.InternFactory,
                CallingConvention = callingConventions,
                ContainingType    = containingType,
                Type = Host.PlatformType.SystemVoid,
                Name = Host.NameTable.GetNameFor("set_" + propertyName),
                GenericParameterCount = 0,
                Parameters            = setterParams
            };
            #endregion
            #region Get the assembly reference (this also ensures the assembly gets loaded properly)
            IAssemblyReference assemblyReference;
            TryGetAssemblyReference(semanticProperty.Assembly, out assemblyReference);
            #endregion
            #region ReturnTrue:
            //ReturnTrue:
            _semanticPropertiesToCCIAccessorMethods[semanticProperty] = new Tuple <IMethodReference, IMethodReference>(getter, setter);
            return(true);

            #endregion
            #region ReturnFalse:
ReturnFalse:
            if (semanticProperty.Name != null)
            {
                ContractsPackageAccessor.Current.Logger.WriteToLog("Failed to build accessor references for: " + semanticProperty.Name.Text);
            }
            _semanticPropertiesToCCIAccessorMethods[semanticProperty] = new Tuple <IMethodReference, IMethodReference>(Dummy.MethodReference, Dummy.MethodReference);
            return(false);

            #endregion
        }