Example #1
0
        public override TypeNode InferTypeOfBinaryExpression(TypeNode t1, TypeNode t2, BinaryExpression binaryExpression)
        {
            //
            // For addition or subtraction operations involving a set (as either operand)
            // the result will be a set of the same type. Later, in the Checker, we'll
            // verify that the combination of operands is valid. Here, we can quickly infer
            // the type of the result.
            //
            switch (binaryExpression.NodeType)
            {
                case NodeType.Add:
                case NodeType.Sub:
                    if (t1 is Set)
                        return t1;
                    else if (t2 is Set)
                        return t2;
                    break;

                case (NodeType)ZingNodeType.In:
                    return SystemTypes.Boolean;

                default:
                    break;
            }
            return base.InferTypeOfBinaryExpression(t1, t2, binaryExpression);
        }
Example #2
0
        /**
         * <summary>IsExposedType compares the given type to itself. If this filter
         * contains a '.' designating it as for a nested class it will skip
         * non-nested classes. Classes with no declaring types will not be compared to 
         * filters with a '.'
         * </summary>
         */
        public bool? IsExposedType(TypeNode type)
        {
            bool? typeIsExposed = null;

            //check if the type was nested
            if (type.DeclaringType == null)
            {
                if (type.Name.Name == name)
                    typeIsExposed = exposed;
            }
                //if we are nested then check if this filter is for a nested class
                //check that nothing used is null also. 
                //if the name attribute is not there in the config name can be null here.
            else if (name != null && name.Contains("."))
            {
                //Get a stack of declaring type names
                Stack < string > parentNames = new Stack < string >();
                parentNames.Push(type.Name.Name); //start with this one

                TypeNode parent = type.DeclaringType;
                while (parent != null)
                {
                    parentNames.Push(parent.Name.Name);
                    parent = parent.DeclaringType;
                }

                //put them back in the correct order and check the name
                if (name.Equals(String.Join(".", parentNames.ToArray())))
                    typeIsExposed = exposed;
            }

            return typeIsExposed;
        }
 public CollectOldExpressions(Module module, Method method, ContractNodes contractNodes, Dictionary<TypeNode, Local> closureLocals, 
     int localCounterStart, Class initialClosureClass)
     : this(module, method, contractNodes, closureLocals, localCounterStart)
 {
     this.topLevelClosureClass = initialClosureClass;
     this.currentClosureClass = initialClosureClass;
 }
Example #4
0
 private CurrentState(TypeNode type, Dictionary<string, object> typeSuppressed, Method method)
 {
     this.Type = type;
     this.typeSuppressed = typeSuppressed;
     this.Method = method;
     this.methodSuppressed = null;
 }
Example #5
0
 public override string GetTypeName(TypeNode type) {
     using (TextWriter writer = new StringWriter()) {
         writer.Write("T:");
         WriteType(type, writer);
         return (writer.ToString());
     }
 }
Example #6
0
 public static Namespace GetNamespace(TypeNode type) {
     if (type.DeclaringType != null) {
         return (GetNamespace(type.DeclaringType));
     } else {
         return (new Namespace(type.Namespace));
     }
 }
Example #7
0
    /// <summary>
    /// Returns a list of CfgBlock that are handlers of the current block, handling an exception
    /// of the given type, or a subtype thereof.
    /// </summary>
    /// <param name="exception">Type of exception thrown. It is assumed that any actual subtype could be thrown</param>
    /// <returns>All handlers that could apply directly to this exception.
    ///  In addition, if the method might not handle it, then the ExceptionExit block is
    /// part of this list.</returns>
    public System.Collections.IEnumerable/*CfgBlock*/ HandlersMatching(TypeNode exception) {
      ArrayList handlers = new ArrayList();

      CfgBlock currentBlock = this;

      while (currentBlock != null) {
        CfgBlock handler = this.cfg.ExceptionHandler(currentBlock);

        if (handler == null || handler.Statements == null || handler.Statements.Count < 1) break;

        Catch stat = handler.Statements[0] as Catch;

        if (stat != null) {
          if (exception.IsAssignableTo(stat.Type)) {
            // handles exceptions completely
            handlers.Add(handler);
            break;
          }
          if (stat.Type.IsAssignableTo(exception)) {
            // handles part of it
            handlers.Add(handler);
          }
          currentBlock = handler;
        }
        else {
          // must be the Unwind block
          handlers.Add(handler);
          break;
        }
      }
      return handlers;
    }
Example #8
0
        //
        // We add an implicit conversion from "object" to any of our heap-allocated
        // types.
        //
        // We also permit implicit conversions between "int" and "byte".
        //
        // TODO: We may need to construct a more elaborate expression here for the
        // conversion to permit the runtime to make the appropriate checks.
        //
        public override bool ImplicitCoercionFromTo(Expression source, TypeNode t1, TypeNode t2)
        {
            if (t1 == SystemTypes.Object)
            {
                if (t2 is Chan || t2 is Set || t2 is ZArray || t2 is Class)
                    return true;
                else
                    return false;
            }

            if (t2 == SystemTypes.Object)
            {
                if (t1 is Chan || t1 is Set || t1 is ZArray || t1 is Class)
                    return true;
                else
                    return false;
            }

            if (t1 == SystemTypes.Int32 && t2 == SystemTypes.UInt8)
                return true;

            if (t1 == SystemTypes.UInt8 && t2 == SystemTypes.Int32)
                return true;

            return base.ImplicitCoercionFromTo(source, t1, t2);
        }
Example #9
0
 public override Expression CoerceAnonymousNestedFunction(AnonymousNestedFunction func, TypeNode targetType, bool explicitCoercion, TypeViewer typeViewer) {
   if (func is AnonymousNestedDelegate && !(targetType is DelegateNode)) {
     this.HandleError(func, Error.AnonMethToNonDel, this.GetTypeName(targetType));
     return null;
   }
   return base.CoerceAnonymousNestedFunction(func, targetType, explicitCoercion, typeViewer);
 }
Example #10
0
 /// <param name="module">The module into which the duplicate IR will be grafted.</param>
 /// <param name="type">The type into which the duplicate Member will be grafted. Ignored if entire type, or larger unit is duplicated.</param>
 public Duplicator(Module/*!*/ module, TypeNode type)
 {
     this.TargetModule = module;
     this.TargetType = this.OriginalTargetType = type;
     this.DuplicateFor = new TrivialHashtable();
     this.TypesToBeDuplicated = new TrivialHashtable();
     //^ base();
 }
Example #11
0
        /// <inheritdoc />
        public override string GetTypeName(TypeNode type)
        {
            StringBuilder sb = new StringBuilder("T:");

            WriteType(type, sb);

            return sb.ToString();
        }
Example #12
0
 public CurrentState(AssemblyNode assembly) {
   this.Assembly = assembly;
   this.Type = null;
   this.Method = null;
   this.assemblySuppressed = null;
   this.typeSuppressed = null;
   this.methodSuppressed = null;
 }
        public override void VisitTypeNode(TypeNode typeNode)
        {
            if (typeNode == null) return;

            ScrubAttributeList(typeNode.Attributes);

            this.VisitMemberList(typeNode.Members);
        }
        public DuplicatorForContractsAndClosures(Module module, Method sourceMethod, Method targetMethod,
            ContractNodes contractNodes, bool mapParameters)
            : base(module, targetMethod.DeclaringType)
        {
            this.sourceMethod = sourceMethod;
            this.targetMethod = targetMethod;

            this.RemoveNameForLocals = true;

            Duplicator dup = this;

            if (mapParameters)
            {
                if (sourceMethod.ThisParameter != null)
                {
                    if (targetMethod.ThisParameter != null)
                    {
                        dup.DuplicateFor[sourceMethod.ThisParameter.UniqueKey] = targetMethod.ThisParameter;
                    }
                    else
                    {
                        // target is a static wrapper. But duplicator cannot handle This -> Parameter conversion
                        // so we handle it explicitly here in this visitor.
                        replaceThisWithParameter = targetMethod.Parameters[0];
                    }
                }

                if (sourceMethod.Parameters != null && targetMethod.Parameters != null
                    && sourceMethod.Parameters.Count == targetMethod.Parameters.Count)
                {
                    for (int i = 0, n = sourceMethod.Parameters.Count; i < n; i++)
                    {
                        dup.DuplicateFor[sourceMethod.Parameters[i].UniqueKey] = targetMethod.Parameters[i];
                    }
                }
            }

            var originalType = HelperMethods.IsContractTypeForSomeOtherType(sourceMethod.DeclaringType, contractNodes);
            if (originalType != null)
            {
                var contractType = this.contractClass = sourceMethod.DeclaringType;
                while (contractType.Template != null)
                {
                    contractType = contractType.Template;
                }
                    
                while (originalType.Template != null)
                {
                    originalType = originalType.Template;
                }

                // forward ContractType<A,B> -> originalType<A',B'>
                this.contractClassToForward = contractType;
                this.targetTypeToForwardTo = originalType;

                //dup.DuplicateFor[contractType.UniqueKey] = originalType;
            }
        }
Example #15
0
        //Find out if any are exposed incase this class is not exposed
        public bool HasExposedMembers(TypeNode type)
        {
            foreach (Member member in type.Members)
                foreach (MemberFilter memberFilter in memberFilters)
                    if (memberFilter.IsExposedMember(member) == true)
                        return true;

            return false;
        }
        public static bool IsRuntimeIgnored(Node node, ContractNodes contractNodes, TypeNode referencingType, bool skipQuantifiers)
        {
            CodeInspector ifrv = new CodeInspector(
                ContractNodes.RuntimeIgnoredAttributeName, contractNodes, referencingType, skipQuantifiers);
            
            ifrv.Visit(node);

            return ifrv.foundAttribute;
        }
 private CurrentState(Method method, CurrentState oldState)
 {
     this.Assembly = oldState.Assembly;
     this.assemblySuppressed = oldState.assemblySuppressed;
     this.Type = oldState.Type;
     this.typeSuppressed = oldState.typeSuppressed;
     this.Method = method;
     this.methodSuppressed = null;
 }
        public ScrubContractClass(ExtractorVisitor parent, Class contractClass, TypeNode originalType)
        {
            Contract.Requires(TypeNode.IsCompleteTemplate(contractClass));
            Contract.Requires(TypeNode.IsCompleteTemplate(originalType));

            this.parent = parent;
            this.contractClass = contractClass;
            this.abstractClass = originalType;
        }
 public CurrentState(TypeNode type, CurrentState oldState)
 {
     this.Type = type;
     this.Method = null;
     this.typeSuppressed = null;
     this.methodSuppressed = null;
     this.Assembly = oldState.Assembly;
     this.assemblySuppressed = oldState.assemblySuppressed;
 }
        /// <summary>
        /// actualReturn type is null if Task is not generic, otherwise ,the Task result type.
        /// </summary>
        public static bool Contains(Node node, ContractNodes contractNodes, Method currentMethod,
            TypeNode actualReturnType)
        {
            var v = new AsyncReturnValueQuery(contractNodes, currentMethod, actualReturnType);

            v.Visit(node);

            return v.foundReturnValueTaskResult;
        }
        // we are satistied with the default namespace expose test, so don't override it

        public override bool IsExposedType(TypeNode type) {
            if (type == null) throw new ArgumentNullException("type");
            // expose any visible types allowed by the base filter
            if (type.IsVisibleOutsideAssembly) {
                return (base.IsExposedType(type));
            } else {
                return (false);
            }
            // return(type.IsVisibleOutsideAssembly);
        }
        public override Method VisitMethod(Method method)
        {
            Method re = base.VisitMethod(method);

            System.Compiler.TypeNode sigTmp = re.ReturnType;
            if(sigTmp.TypeCode!=TypeCode.Empty)
                signatureType = sigTmp;

            return re;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="type">the current type being extended</param>
        /// <param name="extensionMethodTemplate">A reference to the extension method. For generic methods, this is a reference to the 
        /// non-specialized method, e.g. System.Linq.Enumerable.Select``2.
        /// </param>
        /// <param name="specialization">When the current type implements or inherits from a specialization of a generic type,
        /// this parameter has a TypeNode for the type used as apecialization of the generic type's first template param. 
        /// </param>
        private void AddExtensionMethod(XmlWriter writer, TypeNode type, Method extensionMethodTemplate, TypeNode specialization)
        {
            // If this is a specialization of a generic method, construct a Method object that describes the specialization
            Method extensionMethodTemplate2 = extensionMethodTemplate;
            if (extensionMethodTemplate2.IsGeneric && (specialization != null))
            {
                // the specialization type is the first of the method's template arguments
                TypeNodeList templateArgs = new TypeNodeList();
                templateArgs.Add(specialization);
                // add any additional template arguments
                for (int i = 1; i < extensionMethodTemplate.TemplateParameters.Count; i++)
                    templateArgs.Add(extensionMethodTemplate.TemplateParameters[i]);
                extensionMethodTemplate2 = extensionMethodTemplate.GetTemplateInstance(type, templateArgs);
            }
            TypeNode extensionMethodTemplateReturnType = extensionMethodTemplate2.ReturnType;
            ParameterList extensionMethodTemplateParameters = extensionMethodTemplate2.Parameters;

            ParameterList extensionMethodParameters = new ParameterList();
            for (int i = 1; i < extensionMethodTemplateParameters.Count; i++)
            {
                Parameter extensionMethodParameter = extensionMethodTemplateParameters[i];
                extensionMethodParameters.Add(extensionMethodParameter);
            }
            Method extensionMethod = new Method(extensionMethodTemplate.DeclaringType, new AttributeList(), extensionMethodTemplate.Name, extensionMethodParameters, extensionMethodTemplate.ReturnType, null);
            extensionMethod.Flags = extensionMethodTemplate.Flags & ~MethodFlags.Static;

            // for generic methods, set the template args and params so the template data is included in the id and the method data
            if (extensionMethodTemplate2.IsGeneric)
            {
                extensionMethod.IsGeneric = true;
                if (specialization != null)
                {
                    // set the template args for the specialized generic method
                    extensionMethod.TemplateArguments = extensionMethodTemplate2.TemplateArguments;
                }
                else
                {
                    // set the generic template params for the non-specialized generic method
                    extensionMethod.TemplateParameters = extensionMethodTemplate2.TemplateParameters;
                }
            }

            // Get the id
            string extensionMethodTemplateId = reflector.ApiNamer.GetMemberName(extensionMethodTemplate);

            // write the element node
            writer.WriteStartElement("element");
            writer.WriteAttributeString("api", extensionMethodTemplateId);
            writer.WriteAttributeString("source", "extension");
            isExtensionMethod = true;
            reflector.WriteMember(extensionMethod);
            isExtensionMethod = false;
            writer.WriteEndElement();
        }
Example #24
0
        public override Expression ImplicitCoercion(Expression source, TypeNode targetType, TypeViewer typeViewer)
        {
            // LJW: added third parameter "typeViewer" so we override the correct thing
            if (source == null || source.Type == null || targetType == null)
                return null;

            if (source.Type is EnumNode && targetType == SystemTypes.Int32)
            {
                return source;
            }

            if (source.Type == SystemTypes.Object)
            {
                if (targetType is Chan || targetType is Set || targetType is ZArray || targetType is Class)
                    return source;
                else
                {
                    this.HandleError(source, System.Compiler.Error.NoImplicitCoercion, "object", targetType.FullName);
                    return null;
                }
            }

            if (targetType == SystemTypes.Object)
            {
                if (!(source.Type is Chan || source.Type is Set || source.Type is ZArray || source.Type is Class))
                {
                    this.HandleError(source, System.Compiler.Error.NoImplicitCoercion, source.Type.FullName, "object");
                    return null;
                }
            }

            if (source.Type == SystemTypes.Int32 && targetType == SystemTypes.UInt8)
            {
                BinaryExpression binExpr = new BinaryExpression(source, new MemberBinding(null, SystemTypes.UInt8),
                    NodeType.Castclass, source.SourceContext);

                binExpr.Type = SystemTypes.UInt8;

                return binExpr;
            }

            if (source.Type == SystemTypes.UInt8 && targetType == SystemTypes.Int32)
            {
                BinaryExpression binExpr = new BinaryExpression(source, new MemberBinding(null, SystemTypes.Int32),
                    NodeType.Castclass, source.SourceContext);

                binExpr.Type = SystemTypes.Int32;

                return binExpr;
            }

            return base.ImplicitCoercion(source, targetType, typeViewer);
        }
Example #25
0
 //Find out if any are exposed incase this class is not exposed
 public bool HasExposedMembers(TypeNode type)
 {
     foreach (NamespaceFilter namespaceFilter in namespaceFilters)
     {
         bool? result = namespaceFilter.IsExposedType(type);
         if (result != null)
         {
             return namespaceFilter.HasExposedMembers(type);
         }
     }
     return false;
 }
Example #26
0
        //=====================================================================

        /// <summary>
        /// This is used to find out if the given type is required
        /// </summary>
        /// <param name="type">The type to check</param>
        /// <returns>True if the type is required, false if not</returns>
        public bool IsRequiredType(TypeNode type)
        {
            foreach(NamespaceFilter namespaceFilter in namespaceFilters)
            {
                bool? result = namespaceFilter.IsRequiredType(type);

                if(result != null)
                    return result.Value;
            }

            return false;
        }
        private void TryAddTypeToMembersToDuplicate(TypeNode type)
        {
            TypeNode template = type;
            while (template.Template != null)
            {
                template = template.Template;
            }

            if (!this.MembersToDuplicate.Contains(template))
            {
                this.MembersToDuplicate.Add(template);
            }
        }
Example #28
0
 private static FieldFlags MapTypeVisibilityToFieldVisibility(TypeNode type){
   TypeFlags flags = type.Flags & TypeFlags.VisibilityMask;
   switch (flags){
     case TypeFlags.Public:
     case TypeFlags.NestedPublic: return FieldFlags.Public;
     case TypeFlags.NotPublic:
     case TypeFlags.NestedAssembly: return FieldFlags.Assembly;
     case TypeFlags.NestedFamANDAssem: return FieldFlags.FamANDAssem;
     case TypeFlags.NestedFamily: return FieldFlags.Family;
     case TypeFlags.NestedFamORAssem: return FieldFlags.FieldAccessMask;
     default: return FieldFlags.Private;
   }
 }
        private CodeInspector(Identifier attributeToFind, ContractNodes contractNodes, TypeNode referencingType, bool skipQuantifiers)
        {
            this.foundAttribute = false;
            
            this.attributeToFind = attributeToFind;
            this.contractNodes = contractNodes;
            this.skipQuantifiers = skipQuantifiers;
            this.referencingType = new Stack<TypeNode>();

            if (referencingType != null)
            {
                this.referencingType.Push(referencingType);
            }
        }
		public static bool IsVoid (TypeNode type) 
		{
			TypeModifier mtype = type as TypeModifier;
			if (mtype != null) 
			{
				return IsVoid(mtype.ModifiedType);
			}

			if (type != null && type.Equals(Cci.SystemTypes.Void)) 
			{
				return true;
			}
			return false;
		}