Пример #1
0
        /// <summary>
        /// Replaces any MemberBindings of the form (this,x) in the methodContract with a method call where the method
        /// being called is P where x is a private field of the source type that has been marked as [ContractPublicPropertyName("P")].
        /// </summary>
        /// <param name="targetType"></param>
        /// <param name="sourceType"></param>
        /// <param name="methodContract"></param>
        internal static void ReplacePrivateFieldsThatHavePublicProperties(TypeNode targetType,
                                                                          TypeNode sourceType,
                                                                          MethodContract methodContract,
                                                                          ContractNodes contractNodes
                                                                          )
        {
            Contract.Requires(sourceType != null);

            Dictionary <Field, Method> field2Getter = new Dictionary <Field, Method>();

            for (int i = 0, n = /*sourceType.Members == null ? 0 : */ sourceType.Members.Count; i < n; i++)
            {
                Field f = sourceType.Members[i] as Field;
                if (f == null)
                {
                    continue;
                }
                string propertyName = HelperMethods.GetStringFromAttribute(f, ContractNodes.SpecPublicAttributeName);
                if (propertyName != null)
                {
                    Property p = sourceType.GetProperty(Identifier.For(propertyName));
                    if (p != null)
                    {
                        field2Getter.Add(f, p.Getter);
                    }
                }
            }
            if (0 < field2Getter.Count)
            {
                SubstitutePropertyGetterForField s = new SubstitutePropertyGetterForField(field2Getter);
                s.Visit(methodContract);
            }
            return;
        }
Пример #2
0
 public static PropertyNode GetPropertyFromParent(Identifier id, TypeNode typeNode)
 {
     if (typeNode == null)
     {
         return null;
     }
     return typeNode.GetProperty(id) ?? GetPropertyFromParent(id, typeNode.BaseType);
 }
Пример #3
0
 public static Expression BindPseudoMember(Expression qualifier, Identifier identifier)
 {
     if (qualifier != null)
     {
         SourceContext fullSourceContext = identifier.SourceContext;
         if (qualifier.SourceContext.Document != null)
         {
             fullSourceContext.StartPos = qualifier.SourceContext.StartPos;
         }
         TypeNode qualifierType = qualifier.Type;
         if (identifier.UniqueIdKey == Cci.Runtime.IsConsistentId.UniqueIdKey)
         {
             if (qualifier is Base)
             {
                 if (qualifierType != null)
                 {
                     qualifierType = TypeNode.StripModifiers(qualifierType);
                     return(new MethodCall(new MemberBinding(null, SystemTypes.Guard.GetMethod(Identifier.For("FrameIsExposable"), SystemTypes.Object, SystemTypes.Type)),
                                           new ExpressionList(qualifier, new UnaryExpression(new Literal(qualifierType, SystemTypes.Type), NodeType.Typeof, OptionalModifier.For(SystemTypes.NonNullType, SystemTypes.Type))), NodeType.Call, SystemTypes.Boolean, fullSourceContext));
                 }
             }
             else
             {
                 return(new MethodCall(
                            new MemberBinding(null, SystemTypes.Guard.GetMethod(Cci.Runtime.IsConsistentId,
                                                                                OptionalModifier.For(SystemTypes.NonNullType, SystemTypes.Object))),
                            new ExpressionList(qualifier), NodeType.Call, SystemTypes.Boolean, fullSourceContext));
             }
         }
         if (identifier.UniqueIdKey == Cci.Runtime.IsPeerConsistentId.UniqueIdKey)
         {
             return(new MethodCall(
                        new MemberBinding(null, SystemTypes.Guard.GetMethod(Cci.Runtime.IsPeerConsistentId,
                                                                            OptionalModifier.For(SystemTypes.NonNullType, SystemTypes.Object))),
                        new ExpressionList(qualifier), NodeType.Call, SystemTypes.Boolean, fullSourceContext));
         }
         if (qualifierType != null)
         {
             qualifierType = TypeNode.StripModifiers(qualifierType);
             if (identifier.UniqueIdKey == Cci.Runtime.IsVirtualConsistentId.UniqueIdKey)
             {
                 if (qualifier is This || qualifier is ImplicitThis)
                 {
                     SourceContext sc = identifier.SourceContext;
                     identifier = Cci.Runtime.IsExposableId;
                     identifier.SourceContext = sc;
                 }
             }
             TypeNode guard = SystemTypes.Guard;
             if (guard != null)
             {
                 Property property = guard.GetProperty(identifier);
                 if (property != null && property.IsPublic && !property.IsStatic)
                 {
                     Method method = guard.GetMethod(Identifier.For("Frame" + identifier.Name), SystemTypes.Object, SystemTypes.Type);
                     if (method != null && method.IsPublic && method.IsStatic)
                     {
                         return
                             (new MethodCall(
                                  new MemberBinding(null, method),
                                  new ExpressionList(qualifier, new UnaryExpression(new Literal(qualifierType, SystemTypes.Type), NodeType.Typeof, OptionalModifier.For(SystemTypes.NonNullType, SystemTypes.Type))),
                                  NodeType.Call,
                                  method.ReturnType,
                                  fullSourceContext));
                     }
                 }
             }
         }
         {
             Method method = SystemTypes.Guard.GetMethod(identifier, SystemTypes.Object);
             if (method != null && method.IsPublic && method.IsStatic && method.ReturnType != SystemTypes.Void)
             {
                 return(new MethodCall(new MemberBinding(null, method), new ExpressionList(qualifier), NodeType.Call, method.ReturnType, fullSourceContext));
             }
         }
     }
     return(null);
 }