Inheritance: AbstractCompilerComponent
Ejemplo n.º 1
0
        private int MoreSpecific(IType t1, IType t2)
        {
            // Dive into array types and ref types
            if (t1.IsArray && t2.IsArray || t1.IsByRef && t2.IsByRef)
            {
                return(MoreSpecific(t1.ElementType, t2.ElementType));
            }

            // A more concrete type is more specfic
            int result = GenericsServices.GetTypeConcreteness(t1) - GenericsServices.GetTypeConcreteness(t2);

            if (result != 0)
            {
                return(result);
            }

            // With equal concreteness, the more generic type is more specific.
            //First search for open args, then for all args
            result = GenericsServices.GetTypeGenerity(t1) - GenericsServices.GetTypeGenerity(t2);
            if (result != 0)
            {
                return(result);
            }
            result = GenericsServices.GetTypeGenericDepth(t1) - GenericsServices.GetTypeGenericDepth(t2);
            if (result != 0)
            {
                return(result);
            }

            // If both types have the same genrity, the deeper-nested type is more specific
            return(GetLogicalTypeDepth(t1) - GetLogicalTypeDepth(t2));
        }
Ejemplo n.º 2
0
        public IType GetGenericEnumerableItemType(IType iteratorType)
        {
            // Arrays implicitly implement IEnumerable[of element type]
            if (iteratorType is ArrayType)
            {
                return(iteratorType.ElementType);
            }

            // If type is not an array, try to find IEnumerable[of some type] in its interfaces
            IType itemType = null;

            foreach (IType type in GenericsServices.FindConstructedTypes(iteratorType, IEnumerableGenericType))
            {
                IType candidateItemType = type.ConstructedInfo.GenericArguments[0];

                if (itemType != null)
                {
                    itemType = GetMostGenericType(itemType, candidateItemType);
                }
                else
                {
                    itemType = candidateItemType;
                }
            }

            return(itemType);
        }
Ejemplo n.º 3
0
 public static CallableSignature GetOverriddenSignature(IMethod baseMethod, IMethod impl)
 {
     if (baseMethod.GenericInfo != null && GenericsServices.AreOfSameGenerity(baseMethod, impl))
     {
         return(baseMethod.GenericInfo.ConstructMethod(impl.GenericInfo.GenericParameters).CallableType.GetSignature());
     }
     return(baseMethod.CallableType.GetSignature());
 }
Ejemplo n.º 4
0
        private int BetterCandidate(Candidate c1, Candidate c2)
        {
            if (c1 == c2)
            {
                return(0);
            }

            int result = Math.Sign(TotalScore(c1) - TotalScore(c2));

            if (result != 0)
            {
                return(result);
            }

            // Prefer methods declared on deeper types
            result =
                c1.Method.DeclaringType.GetTypeDepth() -
                c2.Method.DeclaringType.GetTypeDepth();

            if (result != 0)
            {
                return(result);
            }

            // Prefer methods with less generic parameters
            result =
                GenericsServices.GetMethodGenerity(c2.Method) -
                GenericsServices.GetMethodGenerity(c1.Method);

            if (result != 0)
            {
                return(result);
            }

            // --- Tie breaking mode! ---

            // Non-expanded methods are better than expanded ones
            if (!c1.Expanded && c2.Expanded)
            {
                return(1);
            }
            if (c1.Expanded && !c2.Expanded)
            {
                return(-1);
            }

            // An expanded method with more fixed parameters is better
            result = c1.Parameters.Length - c2.Parameters.Length;
            if (result != 0)
            {
                return(result);
            }

            // As a last means of breaking this desperate tie, we select the
            // "more specific" candidate, if one exists
            return(MoreSpecific(c1, c2));
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Checks if a specified entity is not a generic definition.
 /// </summary>
 public bool NotGenericDefinition(IEntity entity)
 {
     if (!(GenericsServices.IsGenericType(entity) || GenericsServices.IsGenericMethod(entity)))
     {
         Errors.Add(CompilerErrorFactory.NotAGenericDefinition(ConstructionNode, entity.FullName));
         return(true);
     }
     return(false);
 }
Ejemplo n.º 6
0
        public static bool CheckOverrideSignature(IMethod impl, IMethod baseMethod)
        {
            if (!GenericsServices.AreOfSameGenerity(impl, baseMethod))
            {
                return(false);
            }

            CallableSignature baseSignature = GetOverriddenSignature(baseMethod, impl);

            return(CheckOverrideSignature(impl.GetParameters(), baseSignature.Parameters));
        }
Ejemplo n.º 7
0
 private bool IsFullyConstructed()
 {
     foreach (IType arg in GenericArguments)
     {
         if (GenericsServices.IsOpenGenericType(arg))
         {
             return(false);
         }
     }
     return(true);
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Gets the generic parameters associated with a generic type or generic method definition.
 /// </summary>
 /// <returns>An array of IGenericParameter objects, or null if the specified entity isn't a generic definition.</returns>
 private static IGenericParameter[] GetGenericParameters(IEntity definition)
 {
     if (GenericsServices.IsGenericType(definition))
     {
         return(((IType)definition).GenericInfo.GenericParameters);
     }
     if (GenericsServices.IsGenericMethod(definition))
     {
         return(((IMethod)definition).GenericInfo.GenericParameters);
     }
     return(null);
 }
Ejemplo n.º 9
0
        private int MoreSpecific(IType t1, IType t2)
        {
            // Dive into array types and ref types
            if (t1.IsArray && t2.IsArray || t1.IsByRef && t2.IsByRef)
            {
                return(MoreSpecific(t1.ElementType, t2.ElementType));
            }

            // The less-generic type is more specific
            int result = GenericsServices.GetTypeGenerity(t2) - GenericsServices.GetTypeGenerity(t1);

            if (result != 0)
            {
                return(result);
            }

            // If both types have the same genrity, the deeper-nested type is more specific
            return(GetLogicalTypeDepth(t1) - GetLogicalTypeDepth(t2));
        }
Ejemplo n.º 10
0
        public TypeSystemServices(CompilerContext context)
        {
            if (null == context)
            {
                throw new ArgumentNullException("context");
            }

            _context = context;
            _anonymousCallablesManager = new AnonymousCallablesManager(this);
            _genericsServices          = new GenericsServices(context);

            CodeBuilder = new BooCodeBuilder(this);

            Cache(typeof(Builtins.duck), DuckType = new DuckTypeImpl(this));
            Cache(IQuackFuType             = new ExternalType(this, typeof(IQuackFu)));
            Cache(VoidType                 = new VoidTypeImpl(this));
            Cache(ObjectType               = new ExternalType(this, Types.Object));
            Cache(RegexType                = new ExternalType(this, Types.Regex));
            Cache(ValueTypeType            = new ExternalType(this, typeof(ValueType)));
            Cache(EnumType                 = new ExternalType(this, typeof(Enum)));
            Cache(ArrayType                = new ExternalType(this, Types.Array));
            Cache(TypeType                 = new ExternalType(this, Types.Type));
            Cache(StringType               = new ExternalType(this, Types.String));
            Cache(BoolType                 = new ExternalType(this, Types.Bool));
            Cache(SByteType                = new ExternalType(this, Types.SByte));
            Cache(CharType                 = new ExternalType(this, Types.Char));
            Cache(ShortType                = new ExternalType(this, Types.Short));
            Cache(IntType                  = new ExternalType(this, Types.Int));
            Cache(LongType                 = new ExternalType(this, Types.Long));
            Cache(ByteType                 = new ExternalType(this, Types.Byte));
            Cache(UShortType               = new ExternalType(this, Types.UShort));
            Cache(UIntType                 = new ExternalType(this, Types.UInt));
            Cache(ULongType                = new ExternalType(this, Types.ULong));
            Cache(SingleType               = new ExternalType(this, Types.Single));
            Cache(DoubleType               = new ExternalType(this, Types.Double));
            Cache(DecimalType              = new ExternalType(this, Types.Decimal));
            Cache(TimeSpanType             = new ExternalType(this, Types.TimeSpan));
            Cache(DateTimeType             = new ExternalType(this, Types.DateTime));
            Cache(RuntimeServicesType      = new ExternalType(this, Types.RuntimeServices));
            Cache(BuiltinsType             = new ExternalType(this, Types.Builtins));
            Cache(ListType                 = new ExternalType(this, Types.List));
            Cache(HashType                 = new ExternalType(this, Types.Hash));
            Cache(ICallableType            = new ExternalType(this, Types.ICallable));
            Cache(IEnumerableType          = new ExternalType(this, Types.IEnumerable));
            Cache(IEnumeratorType          = new ExternalType(this, typeof(IEnumerator)));
            Cache(ICollectionType          = new ExternalType(this, Types.ICollection));
            Cache(IListType                = new ExternalType(this, Types.IList));
            Cache(IDictionaryType          = new ExternalType(this, Types.IDictionary));
            Cache(ApplicationExceptionType = new ExternalType(this, Types.ApplicationException));
            Cache(ExceptionType            = new ExternalType(this, Types.Exception));
            Cache(IntPtrType               = new ExternalType(this, Types.IntPtr));
            Cache(UIntPtrType              = new ExternalType(this, Types.UIntPtr));
            Cache(MulticastDelegateType    = new ExternalType(this, Types.MulticastDelegate));
            Cache(DelegateType             = new ExternalType(this, Types.Delegate));
            Cache(SystemAttribute          = new ExternalType(this, typeof(System.Attribute)));
            Cache(IEnumerableGenericType   = new ExternalType(this, typeof(System.Collections.Generic.IEnumerable <>)));
            Cache(IEnumeratorGenericType   = new ExternalType(this, typeof(System.Collections.Generic.IEnumerator <>)));

            ObjectArrayType = GetArrayType(ObjectType, 1);

            PreparePrimitives();
            PrepareBuiltinFunctions();
        }
Ejemplo n.º 11
0
        public TypeSystemServices(CompilerContext context)
        {
            if (null == context) throw new ArgumentNullException("context");

            _context = context;
            _anonymousCallablesManager = new AnonymousCallablesManager(this);
            _genericsServices = new GenericsServices(context);

            CodeBuilder = new BooCodeBuilder(this);

            Cache(typeof(Builtins.duck), DuckType = new DuckTypeImpl(this));
            Cache(IQuackFuType = new ExternalType(this, typeof(IQuackFu)));
            Cache(VoidType = new VoidTypeImpl(this));
            Cache(ObjectType = new ExternalType(this, Types.Object));
            Cache(RegexType = new ExternalType(this, Types.Regex));
            Cache(ValueTypeType = new ExternalType(this, typeof(ValueType)));
            Cache(EnumType = new ExternalType(this, typeof(Enum)));
            Cache(ArrayType = new ExternalType(this, Types.Array));
            Cache(TypeType = new ExternalType(this, Types.Type));
            Cache(StringType = new ExternalType(this, Types.String));
            Cache(BoolType = new ExternalType(this, Types.Bool));
            Cache(SByteType = new ExternalType(this, Types.SByte));
            Cache(CharType = new ExternalType(this, Types.Char));
            Cache(ShortType = new ExternalType(this, Types.Short));
            Cache(IntType = new ExternalType(this, Types.Int));
            Cache(LongType = new ExternalType(this, Types.Long));
            Cache(ByteType = new ExternalType(this, Types.Byte));
            Cache(UShortType = new ExternalType(this, Types.UShort));
            Cache(UIntType = new ExternalType(this, Types.UInt));
            Cache(ULongType = new ExternalType(this, Types.ULong));
            Cache(SingleType = new ExternalType(this, Types.Single));
            Cache(DoubleType = new ExternalType(this, Types.Double));
            Cache(DecimalType = new ExternalType(this, Types.Decimal));
            Cache(TimeSpanType = new ExternalType(this, Types.TimeSpan));
            Cache(DateTimeType = new ExternalType(this, Types.DateTime));
            Cache(RuntimeServicesType = new ExternalType(this, Types.RuntimeServices));
            Cache(BuiltinsType = new ExternalType(this, Types.Builtins));
            Cache(ListType = new ExternalType(this, Types.List));
            Cache(HashType = new ExternalType(this, Types.Hash));
            Cache(ICallableType = new ExternalType(this, Types.ICallable));
            Cache(IEnumerableType = new ExternalType(this, Types.IEnumerable));
            Cache(IEnumeratorType = new ExternalType(this, typeof(IEnumerator)));
            Cache(ICollectionType = new ExternalType(this, Types.ICollection));
            Cache(IListType = new ExternalType(this, Types.IList));
            Cache(IDictionaryType = new ExternalType(this, Types.IDictionary));
            Cache(IntPtrType = new ExternalType(this, Types.IntPtr));
            Cache(UIntPtrType = new ExternalType(this, Types.UIntPtr));
            Cache(MulticastDelegateType = new ExternalType(this, Types.MulticastDelegate));
            Cache(DelegateType = new ExternalType(this, Types.Delegate));
            Cache(SystemAttribute = new ExternalType(this, typeof(System.Attribute)));
            Cache(ConditionalAttribute = new ExternalType(this, typeof(System.Diagnostics.ConditionalAttribute)));
            Cache(IEnumerableGenericType = new ExternalType(this, typeof(System.Collections.Generic.IEnumerable<>)));
            Cache(IEnumeratorGenericType = new ExternalType(this, typeof(System.Collections.Generic.IEnumerator<>)));

            ObjectArrayType = GetArrayType(ObjectType, 1);

            PreparePrimitives();
            PrepareBuiltinFunctions();
        }