상속: MemberTracker, IMembersList
예제 #1
0
        /// <summary>
        /// As a fallback, so if the type does exist in any assembly. This would happen if a new type was added
        /// that was not in the hardcoded list of types.
        /// This code is not accurate because:
        /// 1. We dont deal with generic types (TypeCollision).
        /// 2. Previous calls to GetCustomMemberNames (eg. "from foo import *" in Python) would not have included this type.
        /// 3. This does not deal with new namespaces added to the assembly
        /// </summary>
        private MemberTracker CheckForUnlistedType(string nameString)
        {
            Assert.NotNull(nameString);

            string fullTypeName = GetFullChildName(nameString);

            foreach (Assembly assem in _packageAssemblies)
            {
                Type type = assem.GetType(fullTypeName);
                if (type == null || type.IsNested())
                {
                    continue;
                }

                bool publishType = type.IsPublic() || _topPackage.DomainManager.Configuration.PrivateBinding;
                if (!publishType)
                {
                    continue;
                }

                // We dont use TypeCollision.UpdateTypeEntity here because we do not handle generic type names
                return(TypeTracker.GetTypeTracker(type));
            }

            return(null);
        }
예제 #2
0
        internal void AddTypeName(string typeName, Assembly assem)
        {
            // lock is held when this is called
            Assert.NotNull(typeName, assem);
            Debug.Assert(typeName.IndexOf(Type.Delimiter) == -1); // This is the simple name, not the full name

            if (!_typeNames.ContainsKey(assem))
            {
                _typeNames[assem] = new TypeNames(assem, _fullName);
            }
            _typeNames[assem].AddTypeName(typeName);

            string normalizedTypeName = ReflectionUtils.GetNormalizedTypeName(typeName);

            if (_dict.ContainsKey(normalizedTypeName))
            {
                // A similarly named type, namespace, or module already exists.
                Type newType = LoadType(assem, GetFullChildName(typeName));

                object      existingValue      = _dict[normalizedTypeName];
                TypeTracker existingTypeEntity = existingValue as TypeTracker;
                if (existingTypeEntity == null)
                {
                    // Replace the existing namespace or module with the new type
                    Debug.Assert(existingValue is NamespaceTracker);
                    _dict[normalizedTypeName] = MemberTracker.FromMemberInfo(newType);
                }
                else
                {
                    // Unify the new type with the existing type
                    _dict[normalizedTypeName] = TypeGroup.UpdateTypeEntity(existingTypeEntity, ReflectionCache.GetTypeTracker(newType));
                }
                return;
            }
        }
예제 #3
0
            internal MemberTracker UpdateTypeEntity(TypeTracker existingTypeEntity, string normalizedTypeName)
            {
                Debug.Assert(normalizedTypeName.IndexOf('.') == -1); // This is the simple name, not the full name
                Debug.Assert(ReflectionUtils.GetNormalizedTypeName(normalizedTypeName) == normalizedTypeName);

                // Look for a non-generic type
                if (_simpleTypeNames.Contains(normalizedTypeName))
                {
                    Type newType = LoadType(_assembly, GetFullChildName(normalizedTypeName));
                    if (newType != null)
                    {
                        existingTypeEntity = TypeGroup.UpdateTypeEntity(existingTypeEntity, TypeTracker.GetTypeTracker(newType));
                    }
                }

                // Look for generic types
                if (_genericTypeNames.ContainsKey(normalizedTypeName))
                {
                    List <string> actualNames = _genericTypeNames[normalizedTypeName];
                    foreach (string actualName in actualNames)
                    {
                        Type newType = LoadType(_assembly, GetFullChildName(actualName));
                        if (newType != null)
                        {
                            existingTypeEntity = TypeGroup.UpdateTypeEntity(existingTypeEntity, TypeTracker.GetTypeTracker(newType));
                        }
                    }
                }

                return(existingTypeEntity);
            }
예제 #4
0
 public static RubyClass ToClass(RubyContext/*!*/ context, TypeTracker/*!*/ self)
 {
     if (self.Type.IsInterface()) {
         RubyExceptions.CreateTypeError("Cannot convert a CLR interface to a Ruby class");
     }
     return context.GetClass(self.Type);
 }
예제 #5
0
        /// <param name="existingTypeEntity">The merged list so far. Could be null</param>
        /// <param name="newType">The new type(s) to add to the merged list</param>
        /// <returns>The merged list.  Could be a TypeTracker or TypeGroup</returns>
        public static TypeTracker UpdateTypeEntity(TypeTracker existingTypeEntity, TypeTracker newType)
        {
            Debug.Assert(newType != null);
            Debug.Assert(existingTypeEntity == null || (existingTypeEntity is NestedTypeTracker) || (existingTypeEntity is TypeGroup));

            if (existingTypeEntity == null)
            {
                return(newType);
            }

            NestedTypeTracker existingType          = existingTypeEntity as NestedTypeTracker;
            TypeGroup         existingTypeCollision = existingTypeEntity as TypeGroup;

            if (existingType != null)
            {
                int existingArity = GetGenericArity(existingType.Type);
                int newArity      = GetGenericArity(newType.Type);

                if (existingArity == newArity)
                {
                    return(newType);
                }

                return(new TypeGroup(existingType.Type, existingArity, newType.Type, newArity));
            }

            return(new TypeGroup(newType.Type, existingTypeCollision));
        }
예제 #6
0
파일: TypeGroup.cs 프로젝트: xia7410/dlr
 public TypeTracker GetTypeForArity(int arity)
 {
     if (!_typesByArity.TryGetValue(arity, out Type typeWithMatchingArity))
     {
         return(null);
     }
     return(TypeTracker.GetTypeTracker(typeWithMatchingArity));
 }
예제 #7
0
        private static Type GetTargetType(object target)
        {
            TypeTracker tt = target as TypeTracker;

            if (tt != null)
            {
                return(tt.Type);
            }
            return(target as Type);
        }
        private void MakeTypeBody(Type type, MemberGroup members)
        {
            TypeTracker typeTracker = (TypeTracker)members[0];

            for (int i = 1; i < members.Count; i++)
            {
                typeTracker = TypeGroup.UpdateTypeEntity(typeTracker, (TypeTracker)members[i]);
            }

            AddToBody(Rule.MakeReturn(Binder, typeTracker.GetValue(Binder, type)));
        }
예제 #9
0
        private void MakeTypeBody(GetMemberInfo getMemInfo, Type instanceType, MemberGroup members)
        {
            TypeTracker typeTracker = (TypeTracker)members[0];

            for (int i = 1; i < members.Count; i++)
            {
                typeTracker = TypeGroup.UpdateTypeEntity(typeTracker, (TypeTracker)members[i]);
            }

            getMemInfo.Body.FinishCondition(typeTracker.GetValue(getMemInfo.ResolutionFactory, this, instanceType));
        }
예제 #10
0
        public DynamicMetaObject GetMemberNames(DynamicMetaObject target)
        {
            BindingRestrictions restrictions = BindingRestrictions.GetTypeRestriction(target.Expression, target.LimitType);

            HashSet <string> names = new HashSet <string>();

            TypeTracker.GetMemberNames(target.LimitType, names);

            return(new DynamicMetaObject(
                       AstUtils.Constant(names.ToArray()),
                       restrictions
                       ));
        }
예제 #11
0
        /// <param name="existingTypeEntity">The merged list so far. Could be null</param>
        /// <param name="newType">The new type(s) to add to the merged list</param>
        /// <returns>The merged list.  Could be a TypeTracker or TypeGroup</returns>
        public static TypeTracker UpdateTypeEntity(
            TypeTracker existingTypeEntity,
            TypeTracker newType)
        {
            Debug.Assert(newType != null);
            Debug.Assert(existingTypeEntity == null || (existingTypeEntity is NestedTypeTracker) || (existingTypeEntity is TypeGroup));

            if (existingTypeEntity == null)
            {
                return(newType);
            }

            NestedTypeTracker existingType          = existingTypeEntity as NestedTypeTracker;
            TypeGroup         existingTypeCollision = existingTypeEntity as TypeGroup;

#if DEBUG
            string existingEntityNormalizedName = (existingType != null) ? ReflectionUtils.GetNormalizedTypeName(existingType.Type)
                                                                         : existingTypeCollision.NormalizedName;
            string newEntityNormalizedName = ReflectionUtils.GetNormalizedTypeName(newType.Type);
            Debug.Assert(existingEntityNormalizedName == newEntityNormalizedName);
#endif

            if (existingType != null)
            {
                if (GetGenericArity(existingType.Type) == GetGenericArity(newType.Type))
                {
                    return(newType);
                }

                return(new TypeGroup(existingType.Type, newType.Type));
            }

            // copy the dictionary and return a new collision
            Dictionary <int, Type> copy = new Dictionary <int, Type>(existingTypeCollision._typesByArity);
            return(new TypeGroup(newType.Type, copy));
        }
예제 #12
0
파일: RubyClass.cs 프로젝트: kashano/main
        // friend: RubyContext
        // tracker: non-null => show members declared on the tracker
        internal RubyClass(RubyContext/*!*/ context, string name, Type type, object singletonClassOf,
            Action<RubyModule> methodsInitializer, Action<RubyModule> constantsInitializer, Delegate/*!*/[] factories, RubyClass superClass, 
            RubyModule/*!*/[] expandedMixins, TypeTracker tracker, RubyStruct.Info structInfo,
            bool isRubyClass, bool isSingletonClass, ModuleRestrictions restrictions)
            : base(context, name, methodsInitializer, constantsInitializer, expandedMixins,
                type != typeof(object) ? null : context.Namespaces, tracker, restrictions) {

            Debug.Assert(context.Namespaces != null, "Namespaces should be initialized");
            Debug.Assert(superClass != null || structInfo == null, "BasicObject is not a struct");
            Debug.Assert(!isRubyClass || tracker == null, "Ruby class cannot have a tracker");
            Debug.Assert(singletonClassOf != null || !isSingletonClass, "Singleton classes don't have a type");
            Debug.Assert(superClass != this);

            _underlyingSystemType = type;
            _superClass = superClass;
            _isSingletonClass = isSingletonClass;
            _isRubyClass = isRubyClass;
            _singletonClassOf = singletonClassOf;
            _factories = factories ?? Utils.EmptyDelegates;

            if (superClass != null) {
                _level = superClass.Level + 1;
                _structInfo = structInfo ?? superClass._structInfo;
            } else {
                _level = 0;
            }
        }
예제 #13
0
        // friend: RubyContext
        // tracker: non-null => show members declared on the tracker
        internal RubyClass(RubyContext/*!*/ context, string name, Type type, object singletonClassOf,
            Action<RubyModule> methodsInitializer, Action<RubyModule> constantsInitializer, Delegate/*!*/[] factories, RubyClass superClass, 
            RubyModule/*!*/[] expandedMixins, TypeTracker tracker, RubyStruct.Info structInfo,
            bool isRubyClass, bool isSingletonClass, ModuleRestrictions restrictions)
            : base(context, name, methodsInitializer, constantsInitializer, expandedMixins, null, tracker, restrictions) {

            Debug.Assert((superClass == null) == (type == typeof(object)), "All classes have a superclass, except for Object");
            Debug.Assert(superClass != null || structInfo == null, "Object is not a struct");
            Debug.Assert(!isRubyClass || tracker == null, "Ruby class cannot have a tracker");
            Debug.Assert(singletonClassOf != null || !isSingletonClass, "Singleton classes don't have a type");
            Debug.Assert(superClass != this);

            _underlyingSystemType = type;
            _superClass = superClass;
            _isSingletonClass = isSingletonClass;
            _isRubyClass = isRubyClass;
            _singletonClassOf = singletonClassOf;
            _factories = factories ?? Utils.EmptyDelegates;

            if (superClass != null) {
                _level = superClass.Level + 1;
                _structInfo = structInfo ?? superClass._structInfo;
            } else {
                _level = 0;
            }

            _weakSelf = new WeakReference(this);
            Version = new VersionHandle(Interlocked.Increment(ref _globalVersion));
            Version.SetName(name);
        }
예제 #14
0
        internal RubyModule(RubyContext/*!*/ context, string name, Action<RubyModule> initializer,
            IAttributesCollection clrConstants, TypeTracker tracker) {
            Assert.NotNull(context);

            _context = context;
            _name = name;
            _state = State.Uninitialized;
            _mixins = EmptyArray;
            _initializer = initializer;
            _version = Interlocked.Increment(ref _globalVersion);
            _clrConstants = clrConstants;
            _tracker = tracker;
        }
예제 #15
0
        /// <param name="existingTypeEntity">The merged list so far. Could be null</param>
        /// <param name="newType">The new type(s) to add to the merged list</param>
        /// <returns>The merged list.  Could be a TypeTracker or TypeGroup</returns>
        public static TypeTracker UpdateTypeEntity(
            TypeTracker existingTypeEntity,
            TypeTracker newType) {

            Debug.Assert(newType != null);
            Debug.Assert(existingTypeEntity == null || (existingTypeEntity is NestedTypeTracker) || (existingTypeEntity is TypeGroup));

            if (existingTypeEntity == null) {
                return newType;
            }

            NestedTypeTracker existingType = existingTypeEntity as NestedTypeTracker;
            TypeGroup existingTypeCollision = existingTypeEntity as TypeGroup;
#if DEBUG
            string existingEntityNormalizedName = (existingType != null) ? ReflectionUtils.GetNormalizedTypeName(existingType.Type)
                                                                         : existingTypeCollision.NormalizedName;
            string newEntityNormalizedName = ReflectionUtils.GetNormalizedTypeName(newType.Type);
            Debug.Assert(existingEntityNormalizedName == newEntityNormalizedName);
#endif

            if (existingType != null) {
                if (GetGenericArity(existingType.Type) == GetGenericArity(newType.Type)) {
                    return newType;
                }

                return new TypeGroup(existingType.Type, newType.Type);
            }

            // copy the dictionary and return a new collision
            Dictionary<int, Type> copy = new Dictionary<int, Type>(existingTypeCollision._typesByArity);
            return new TypeGroup(newType.Type, copy);
        }
예제 #16
0
 private static Expression ReturnTypeTracker(TypeTracker memberTracker) {
     // all non-group types get exposed as PythonType's
     return Ast.Constant(DynamicHelpers.GetPythonTypeFromType(memberTracker.Type));
 }
예제 #17
0
 private static Type EnsureTrackerRepresentsNonGenericType(TypeTracker tracker)
 {
     // might throw an exception
     return(tracker.Type);
 }
예제 #18
0
        internal RubyModule(RubyContext/*!*/ context, string name, Action<RubyModule> methodsInitializer, Action<RubyModule> constantsInitializer,
            RubyModule/*!*/[] expandedMixins, NamespaceTracker namespaceTracker, TypeTracker typeTracker, ModuleRestrictions restrictions) {

            Assert.NotNull(context);
            Debug.Assert(namespaceTracker == null || typeTracker == null || typeTracker.Type == typeof(object));
            Debug.Assert(expandedMixins == null ||
                CollectionUtils.TrueForAll(expandedMixins, (m) => m != this && m != null && !m.IsClass && m.Context == context)
            );

            _context = context;
            _name = name;
            _methodsInitializer = methodsInitializer;
            _constantsInitializer = constantsInitializer;
            _namespaceTracker = namespaceTracker;
            _typeTracker = typeTracker;
            _mixins = expandedMixins ?? EmptyArray;
            _restrictions = restrictions;
            _weakSelf = new WeakReference(this);

            Version = new VersionHandle(Interlocked.Increment(ref _globalMethodVersion));
            Version.SetName(name);
            Id = Interlocked.Increment(ref _globalModuleId);
        }
예제 #19
0
        /// <param name="existingTypeEntity">The merged list so far. Could be null</param>
        /// <param name="newType">The new type(s) to add to the merged list</param>
        /// <returns>The merged list.  Could be a TypeTracker or TypeGroup</returns>
        public static TypeTracker UpdateTypeEntity(TypeTracker existingTypeEntity, TypeTracker newType) {
            Debug.Assert(newType != null);
            Debug.Assert(existingTypeEntity == null || (existingTypeEntity is NestedTypeTracker) || (existingTypeEntity is TypeGroup));

            if (existingTypeEntity == null) {
                return newType;
            }

            NestedTypeTracker existingType = existingTypeEntity as NestedTypeTracker;
            TypeGroup existingTypeCollision = existingTypeEntity as TypeGroup;

            if (existingType != null) {
                int existingArity = GetGenericArity(existingType.Type);
                int newArity = GetGenericArity(newType.Type);

                if (existingArity == newArity) {
                    return newType;
                }

                return new TypeGroup(existingType.Type, existingArity, newType.Type, newArity);
            }

            return new TypeGroup(newType.Type, existingTypeCollision);
        }
예제 #20
0
 private static Type EnsureTrackerRepresentsNonGenericType(TypeTracker tracker) {
     // might throw an exception
     return tracker.Type;
 }
예제 #21
0
            internal MemberTracker UpdateTypeEntity(TypeTracker existingTypeEntity, string normalizedTypeName) {
                Debug.Assert(normalizedTypeName.IndexOf(Type.Delimiter) == -1); // This is the simple name, not the full name
                Debug.Assert(ReflectionUtils.GetNormalizedTypeName(normalizedTypeName) == normalizedTypeName);

                // Look for a non-generic type
                if (_simpleTypeNames.Contains(normalizedTypeName)) {
                    Type newType = LoadType(_assembly, GetFullChildName(normalizedTypeName));
                    if (newType != null) {
                        existingTypeEntity = TypeGroup.UpdateTypeEntity(existingTypeEntity, ReflectionCache.GetTypeTracker(newType));
                    }
                }

                // Look for generic types
                if (_genericTypeNames.ContainsKey(normalizedTypeName)) {
                    List<string> actualNames = _genericTypeNames[normalizedTypeName];
                    foreach (string actualName in actualNames) {
                        Type newType = LoadType(_assembly, GetFullChildName(actualName));
                        if (newType != null) {
                            existingTypeEntity = TypeGroup.UpdateTypeEntity(existingTypeEntity, ReflectionCache.GetTypeTracker(newType));
                        }
                    }
                }

                return existingTypeEntity;
            }
예제 #22
0
        internal RubyModule(RubyContext/*!*/ context, string name, Action<RubyModule> methodsInitializer, Action<RubyModule> constantsInitializer,
            RubyModule/*!*/[] expandedMixins, NamespaceTracker namespaceTracker, TypeTracker typeTracker, ModuleRestrictions restrictions) {

            Assert.NotNull(context);
            Debug.Assert(namespaceTracker == null || typeTracker == null);
            Debug.Assert(expandedMixins == null ||
                CollectionUtils.TrueForAll(expandedMixins, (m) => m != this && m != null && !m.IsClass && m.Context == context)
            );

            _context = context;
            _name = name;
            _methodsInitializer = methodsInitializer;
            _constantsInitializer = constantsInitializer;
            _namespaceTracker = namespaceTracker;
            _typeTracker = typeTracker;
            _mixins = expandedMixins ?? EmptyArray;
            _restrictions = restrictions;
        }
예제 #23
0
 public static RubyModule/*!*/ ToModule(RubyContext/*!*/ context, TypeTracker/*!*/ self) {
     return context.GetModule(self.Type);
 }
예제 #24
0
        internal RubyModule(RubyContext/*!*/ context, string name, Action<RubyModule> methodsInitializer, Action<RubyModule> constantsInitializer,
            RubyModule/*!*/[] expandedMixins, IAttributesCollection clrConstants, TypeTracker tracker) {
            Assert.NotNull(context);
            Debug.Assert(expandedMixins == null ||
                CollectionUtils.TrueForAll(expandedMixins, (m) => m != this && m != null && !m.IsClass && m.Context == context)
            );

            _context = context;
            _name = name;
            _methodsInitializer = methodsInitializer;
            _constantsInitializer = constantsInitializer;
            _clrConstants = clrConstants;
            _tracker = tracker;
            _mixins = expandedMixins ?? EmptyArray;
        }
예제 #25
0
 public static IDictionary Get__dict__(CodeContext context, TypeTracker self) {
     return new DictProxy(DynamicHelpers.GetPythonTypeFromType(self.Type));
 }