private LowLevelDictionary<string, QHandle> CreateCaseInsensitiveTypeDictionary()
        {
            //
            // Collect all of the *non-nested* types and type-forwards.
            //
            //   The keys are full typenames in lower-cased form.
            //   The value is a tuple containing either a TypeDefinitionHandle or TypeForwarderHandle and the associated Reader
            //      for that handle.
            //
            // We do not store nested types here. The container type is resolved and chosen first, then the nested type chosen from
            // that. If we chose the wrong container type and fail the match as a result, that's too bad. (The desktop CLR has the
            // same issue.)
            //

            LowLevelDictionary<string, QHandle> dict = new LowLevelDictionary<string, QHandle>();

            foreach (QScopeDefinition scope in AllScopes)
            {
                MetadataReader reader = scope.Reader;
                ScopeDefinition scopeDefinition = scope.ScopeDefinition;
                IEnumerable<NamespaceDefinitionHandle> topLevelNamespaceHandles = new NamespaceDefinitionHandle[] { scopeDefinition.RootNamespaceDefinition };
                IEnumerable<NamespaceDefinitionHandle> allNamespaceHandles = reader.GetTransitiveNamespaces(topLevelNamespaceHandles);
                foreach (NamespaceDefinitionHandle namespaceHandle in allNamespaceHandles)
                {
                    string ns = namespaceHandle.ToNamespaceName(reader);
                    if (ns.Length != 0)
                        ns = ns + ".";
                    ns = ns.ToLower();

                    NamespaceDefinition namespaceDefinition = namespaceHandle.GetNamespaceDefinition(reader);
                    foreach (TypeDefinitionHandle typeDefinitionHandle in namespaceDefinition.TypeDefinitions)
                    {
                        string fullName = ns + typeDefinitionHandle.GetTypeDefinition(reader).Name.GetString(reader).ToLower();
                        QHandle existingValue;
                        if (!dict.TryGetValue(fullName, out existingValue))
                        {
                            dict.Add(fullName, new QHandle(reader, typeDefinitionHandle));
                        }
                    }

                    foreach (TypeForwarderHandle typeForwarderHandle in namespaceDefinition.TypeForwarders)
                    {
                        string fullName = ns + typeForwarderHandle.GetTypeForwarder(reader).Name.GetString(reader).ToLower();
                        QHandle existingValue;
                        if (!dict.TryGetValue(fullName, out existingValue))
                        {
                            dict.Add(fullName, new QHandle(reader, typeForwarderHandle));
                        }
                    }
                }
            }

            return dict;
        }
Example #2
0
        //
        // Returns the native layout info reader
        //
        internal static unsafe NativeReader GetNativeLayoutInfoReader(TypeManagerHandle moduleHandle)
        {
            Debug.Assert(!moduleHandle.IsNull);

            t_moduleNativeReaders ??= new LowLevelDictionary <TypeManagerHandle, NativeReader>();

            NativeReader result;

            if (t_moduleNativeReaders.TryGetValue(moduleHandle, out result))
            {
                return(result);
            }

            byte *pBlob;
            uint  cbBlob;

            if (RuntimeAugments.FindBlob(moduleHandle, (int)ReflectionMapBlob.NativeLayoutInfo, new IntPtr(&pBlob), new IntPtr(&cbBlob)))
            {
                result = new NativeReader(pBlob, cbBlob);
            }

            t_moduleNativeReaders.Add(moduleHandle, result);
            return(result);
        }
Example #3
0
        private static IDictionary GetEnvironmentVariablesCore(EnvironmentVariableTarget target)
        {
            if (target == EnvironmentVariableTarget.Process)
            {
                return(GetEnvironmentVariablesCore());
            }
            else
            {
                RegistryKey baseKey;
                string      keyName;
                if (target == EnvironmentVariableTarget.Machine)
                {
                    baseKey = Registry.LocalMachine;
                    keyName = @"System\CurrentControlSet\Control\Session Manager\Environment";
                }
                else
                {
                    Debug.Assert(target == EnvironmentVariableTarget.User);
                    baseKey = Registry.CurrentUser;
                    keyName = @"Environment";
                }

                using (RegistryKey environmentKey = baseKey.OpenSubKey(keyName, writable: false))
                {
                    var table = new LowLevelDictionary <string, string>();
                    if (environmentKey != null)
                    {
                        foreach (string name in environmentKey.GetValueNames())
                        {
                            table.Add(name, environmentKey.GetValue(name, "").ToString());
                        }
                    }
                    return(table);
                }
            }
        }
Example #4
0
        private static IDictionary GetEnvironmentVariablesCore(EnvironmentVariableTarget target)
        {
            if (target == EnvironmentVariableTarget.Process)
            {
                return GetEnvironmentVariablesCore();
            }
            else
            {
                RegistryKey baseKey;
                string keyName;
                if (target == EnvironmentVariableTarget.Machine)
                {
                    baseKey = Registry.LocalMachine;
                    keyName = @"System\CurrentControlSet\Control\Session Manager\Environment";
                }
                else
                {
                    Debug.Assert(target == EnvironmentVariableTarget.User);
                    baseKey = Registry.CurrentUser;
                    keyName = @"Environment";
                }

                using (RegistryKey environmentKey = baseKey.OpenSubKey(keyName, writable: false))
                {
                    var table = new LowLevelDictionary<string, string>();
                    if (environmentKey != null)
                    {
                        foreach (string name in environmentKey.GetValueNames())
                        {
                            table.Add(name, environmentKey.GetValue(name, "").ToString());
                        }
                    }
                    return table;
                }
            }
        }
Example #5
0
        private static LowLevelDictionary <string, TypeFlags> InitPrimitiveTypesDictionary()
        {
            LowLevelDictionary <string, TypeFlags> result = new LowLevelDictionary <string, TypeFlags>();

            result.Add("Void", TypeFlags.Void);
            result.Add("Boolean", TypeFlags.Boolean);
            result.Add("Char", TypeFlags.Char);
            result.Add("SByte", TypeFlags.SByte);
            result.Add("Byte", TypeFlags.Byte);
            result.Add("Int16", TypeFlags.Int16);
            result.Add("UInt16", TypeFlags.UInt16);
            result.Add("Int32", TypeFlags.Int32);
            result.Add("UInt32", TypeFlags.UInt32);
            result.Add("Int64", TypeFlags.Int64);
            result.Add("UInt64", TypeFlags.UInt64);
            result.Add("IntPtr", TypeFlags.IntPtr);
            result.Add("UIntPtr", TypeFlags.UIntPtr);
            result.Add("Single", TypeFlags.Single);
            result.Add("Double", TypeFlags.Double);
            return(result);
        }
Example #6
0
 internal ModuleMap(ModuleInfo[] modules)
 {
     Modules = modules;
     HandleToModuleIndex = new LowLevelDictionary<IntPtr, int>();
     for (int moduleIndex = 0; moduleIndex < Modules.Length; moduleIndex++)
     {
         HandleToModuleIndex.Add(Modules[moduleIndex].Handle, moduleIndex);
     }
 }
Example #7
0
            public static unsafe IntPtr Get(RuntimeTypeHandle constraintType, RuntimeTypeHandle constrainedMethodType, int constrainedMethodSlot, bool directConstrainedCall = false)
            {
                lock (s_nonGenericConstrainedCallDescs)
                {
                    // Get list of constrained call descs associated with a given type
                    LowLevelList <IntPtr> associatedCallDescs;
                    if (!s_nonGenericConstrainedCallDescs.TryGetValue(constraintType, out associatedCallDescs))
                    {
                        associatedCallDescs = new LowLevelList <IntPtr>();
                        s_nonGenericConstrainedCallDescs.Add(constraintType, associatedCallDescs);
                    }

                    // Perform linear scan of associated call descs to see if one matches
                    for (int i = 0; i < associatedCallDescs.Count; i++)
                    {
                        NonGenericConstrainedCallDesc *callDesc = (NonGenericConstrainedCallDesc *)associatedCallDescs[i];

                        Debug.Assert(constraintType.Equals(callDesc->_constraintType));

                        if (callDesc->_constrainedMethodSlot != constrainedMethodSlot)
                        {
                            continue;
                        }

                        if (!callDesc->_constrainedMethodType.Equals(constrainedMethodType))
                        {
                            continue;
                        }

                        // Found matching entry.
                        return(associatedCallDescs[i]);
                    }

                    // Did not find match, allocate a new one and add it to the lookup list
                    IntPtr newCallDescPtr = MemoryHelpers.AllocateMemory(sizeof(NonGenericConstrainedCallDesc));
                    NonGenericConstrainedCallDesc *newCallDesc = (NonGenericConstrainedCallDesc *)newCallDescPtr;
                    newCallDesc->_exactTarget = IntPtr.Zero;
                    if (directConstrainedCall)
                    {
                        newCallDesc->_lookupFunc = RuntimeAugments.GetUniversalTransitionThunk();
                    }
                    else
                    {
                        if (RuntimeAugments.IsValueType(constraintType))
                        {
                            newCallDesc->_lookupFunc = s_resolveCallOnValueTypeFuncPtr;
                        }
                        else
                        {
                            newCallDesc->_lookupFunc = s_resolveCallOnReferenceTypeFuncPtr;
                        }
                    }


                    newCallDesc->_constraintType        = constraintType;
                    newCallDesc->_constrainedMethodSlot = constrainedMethodSlot;
                    newCallDesc->_constrainedMethodType = constrainedMethodType;

                    associatedCallDescs.Add(newCallDescPtr);

                    return(newCallDescPtr);
                }
            }
        //
        // Main iterator.
        //
        private IEnumerable <CustomAttributeData> GetMatchingCustomAttributesIterator(E element, Func <Type, bool> rawPassesFilter, bool inherit)
        {
            Func <Type, bool> passesFilter =
                delegate(Type attributeType)
            {
                // Windows prohibits instantiating WinRT custom attributes. Filter them from the search as the desktop CLR does.
                TypeAttributes typeAttributes = attributeType.Attributes;
                if (0 != (typeAttributes & TypeAttributes.WindowsRuntime))
                {
                    return(false);
                }
                return(rawPassesFilter(attributeType));
            };

            LowLevelList <CustomAttributeData> immediateResults = new LowLevelList <CustomAttributeData>();

            foreach (CustomAttributeData cad in GetDeclaredCustomAttributes(element))
            {
                if (passesFilter(cad.AttributeType))
                {
                    yield return(cad);

                    immediateResults.Add(cad);
                }
            }
            if (inherit)
            {
                // Because the "inherit" parameter defaults to "true", we probably get here for a lot of elements that
                // don't actually have any inheritance chains. Try to avoid doing any unnecessary setup for the inheritance walk
                // unless we have to.
                element = GetParent(element);
                if (element != null)
                {
                    // This dictionary serves two purposes:
                    //   - Let us know which attribute types we've encountered at lower levels so we can block them from appearing twice in the results
                    //     if appropriate.
                    //
                    //   - Cache the results of retrieving the usage attribute.
                    //
                    LowLevelDictionary <TypeUnificationKey, AttributeUsageAttribute> encounteredTypes = new LowLevelDictionary <TypeUnificationKey, AttributeUsageAttribute>(11);

                    for (int i = 0; i < immediateResults.Count; i++)
                    {
                        Type attributeType = immediateResults[i].AttributeType;
                        AttributeUsageAttribute usage;
                        TypeUnificationKey      attributeTypeKey = new TypeUnificationKey(attributeType);
                        if (!encounteredTypes.TryGetValue(attributeTypeKey, out usage))
                        {
                            encounteredTypes.Add(attributeTypeKey, null);
                        }
                    }

                    do
                    {
                        foreach (CustomAttributeData cad in GetDeclaredCustomAttributes(element))
                        {
                            Type attributeType = cad.AttributeType;
                            if (!passesFilter(attributeType))
                            {
                                continue;
                            }
                            AttributeUsageAttribute usage;
                            TypeUnificationKey      attributeTypeKey = new TypeUnificationKey(attributeType);
                            if (!encounteredTypes.TryGetValue(attributeTypeKey, out usage))
                            {
                                // Type was not encountered before. Only include it if it is inheritable.
                                usage = GetAttributeUsage(attributeType);
                                encounteredTypes.Add(attributeTypeKey, usage);
                                if (usage.Inherited)
                                {
                                    yield return(cad);
                                }
                            }
                            else
                            {
                                if (usage == null)
                                {
                                    usage = GetAttributeUsage(attributeType);
                                }
                                encounteredTypes[attributeTypeKey] = usage;
                                // Type was encountered at a lower level. Only include it if its inheritable AND allowMultiple.
                                if (usage.Inherited && usage.AllowMultiple)
                                {
                                    yield return(cad);
                                }
                            }
                        }
                    }while ((element = GetParent(element)) != null);
                }
            }
        }
Example #9
0
            private static LowLevelDictionary<string, char> GenerateLookupTable()
            {
                // e[0] is unicode char, e[1] is '-', e[2+] is entity string

                LowLevelDictionary<string, char> lookupTable = new LowLevelDictionary<string, char>(StringComparer.Ordinal);
                foreach (string e in s_entitiesList)
                {
                    lookupTable.Add(e.Substring(2), e[0]);
                }

                return lookupTable;
            }
        public TypeDesc ResolveRuntimeTypeHandle(RuntimeTypeHandle rtth)
        {
            TypeDesc returnedType;

            if (_runtimeTypeHandleResolutionCache.TryGetValue(rtth, out returnedType))
            {
                return(returnedType);
            }

            if (rtth.Equals(CanonType.RuntimeTypeHandle))
            {
                returnedType = CanonType;
            }
            else if (rtth.Equals(UniversalCanonType.RuntimeTypeHandle))
            {
                returnedType = UniversalCanonType;
            }
            else if (RuntimeAugments.IsGenericTypeDefinition(rtth))
            {
                returnedType = TryGetMetadataBasedTypeFromRuntimeTypeHandle_Uncached(rtth);
                if (returnedType == null)
                {
                    unsafe
                    {
                        TypeDesc[] genericParameters = new TypeDesc[rtth.ToEETypePtr()->GenericArgumentCount];
                        for (int i = 0; i < genericParameters.Length; i++)
                        {
                            genericParameters[i] = GetSignatureVariable(i, false);
                        }

                        returnedType = new NoMetadataType(this, rtth, null, new Instantiation(genericParameters), rtth.GetHashCode());
                    }
                }
            }
            else if (RuntimeAugments.IsGenericType(rtth))
            {
                RuntimeTypeHandle   typeDefRuntimeTypeHandle;
                RuntimeTypeHandle[] genericArgRuntimeTypeHandles;
                typeDefRuntimeTypeHandle = RuntimeAugments.GetGenericInstantiation(rtth, out genericArgRuntimeTypeHandles);

                DefType       typeDef     = (DefType)ResolveRuntimeTypeHandle(typeDefRuntimeTypeHandle);
                Instantiation genericArgs = ResolveRuntimeTypeHandles(genericArgRuntimeTypeHandles);
                returnedType = ResolveGenericInstantiation(typeDef, genericArgs);
            }
            else if (RuntimeAugments.IsArrayType(rtth))
            {
                RuntimeTypeHandle elementTypeHandle = RuntimeAugments.GetRelatedParameterTypeHandle(rtth);
                TypeDesc          elementType       = ResolveRuntimeTypeHandle(elementTypeHandle);
                unsafe
                {
                    if (rtth.ToEETypePtr()->IsSzArray)
                    {
                        returnedType = GetArrayType(elementType);
                    }
                    else
                    {
                        returnedType = GetArrayType(elementType, rtth.ToEETypePtr()->ArrayRank);
                    }
                }
            }
            else if (RuntimeAugments.IsUnmanagedPointerType(rtth))
            {
                RuntimeTypeHandle targetTypeHandle = RuntimeAugments.GetRelatedParameterTypeHandle(rtth);
                TypeDesc          targetType       = ResolveRuntimeTypeHandle(targetTypeHandle);
                returnedType = GetPointerType(targetType);
            }
            else if (RuntimeAugments.IsByRefType(rtth))
            {
                RuntimeTypeHandle targetTypeHandle = RuntimeAugments.GetRelatedParameterTypeHandle(rtth);
                TypeDesc          targetType       = ResolveRuntimeTypeHandle(targetTypeHandle);
                returnedType = GetByRefType(targetType);
            }
            else
            {
                returnedType = TryGetMetadataBasedTypeFromRuntimeTypeHandle_Uncached(rtth);
                if (returnedType == null)
                {
                    returnedType = new NoMetadataType(this, rtth, null, Instantiation.Empty, rtth.GetHashCode());
                }
            }

            // We either retrieved an existing DefType that is already registered with the runtime
            // or one that is not associated with an MethodTable yet. If it's not associated, associate it.
            if (returnedType.RuntimeTypeHandle.IsNull())
            {
                TypeBuilderState state = returnedType.GetTypeBuilderStateIfExist();
                bool             skipStoringRuntimeTypeHandle = false;

                // If we've already attempted to lookup and failed to retrieve this type handle, we
                // may have already decided to create a new one. In that case, do not attempt to abort
                // that creation process as it may have already begun the process of type creation
                if (state != null && state.AttemptedAndFailedToRetrieveTypeHandle)
                {
                    skipStoringRuntimeTypeHandle = true;
                }

                if (!skipStoringRuntimeTypeHandle)
                {
                    returnedType.SetRuntimeTypeHandleUnsafe(rtth);
                }
            }

            _runtimeTypeHandleResolutionCache.Add(rtth, returnedType);

            return(returnedType.WithDebugName());
        }
Example #11
0
 private static LowLevelDictionary<string, TypeFlags> InitPrimitiveTypesDictionary()
 {
     LowLevelDictionary<string, TypeFlags> result = new LowLevelDictionary<string, TypeFlags>();
     result.Add("Void", TypeFlags.Void);
     result.Add("Boolean", TypeFlags.Boolean);
     result.Add("Char", TypeFlags.Char);
     result.Add("SByte", TypeFlags.SByte);
     result.Add("Byte", TypeFlags.Byte);
     result.Add("Int16", TypeFlags.Int16);
     result.Add("UInt16", TypeFlags.UInt16);
     result.Add("Int32", TypeFlags.Int32);
     result.Add("UInt32", TypeFlags.UInt32);
     result.Add("Int64", TypeFlags.Int64);
     result.Add("UInt64", TypeFlags.UInt64);
     result.Add("IntPtr", TypeFlags.IntPtr);
     result.Add("UIntPtr", TypeFlags.UIntPtr);
     result.Add("Single", TypeFlags.Single);
     result.Add("Double", TypeFlags.Double);
     return result;
 }