Ejemplo n.º 1
0
        internal IMethod WrapAccessor(ref IMethod cachingField, IMethod accessorDefinition)
        {
            if (accessorDefinition == null)
            {
                return(null);
            }
            var result = LazyInit.VolatileRead(ref cachingField);

            if (result != null)
            {
                return(result);
            }
            else
            {
                return(LazyInit.GetOrSet(ref cachingField, new SpecializedMethod(accessorDefinition, substitution)));
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Creates a type parameter reference.
 /// For common type parameter references, this method may return a shared instance.
 /// </summary>
 public static TypeParameterReference Create(SymbolKind ownerType, int index)
 {
     if (index >= 0 && index < 8 && (ownerType == SymbolKind.TypeDefinition || ownerType == SymbolKind.Method))
     {
         TypeParameterReference[] arr    = (ownerType == SymbolKind.TypeDefinition) ? classTypeParameterReferences : methodTypeParameterReferences;
         TypeParameterReference   result = LazyInit.VolatileRead(ref arr[index]);
         if (result == null)
         {
             result = LazyInit.GetOrSet(ref arr[index], new TypeParameterReference(ownerType, index));
         }
         return(result);
     }
     else
     {
         return(new TypeParameterReference(ownerType, index));
     }
 }
Ejemplo n.º 3
0
        Dictionary <FullNameAndTypeParameterCount, DefaultResolvedTypeDefinition> GetTypes()
        {
            var dict = this.typeDict;

            if (dict != null)
            {
                LazyInit.ReadBarrier();
                return(dict);
            }
            else
            {
                var comparer = FullNameAndTypeParameterCountComparer.Ordinal;
                dict = projectContent.TopLevelTypeDefinitions
                       .GroupBy(t => new FullNameAndTypeParameterCount(t.Namespace, t.Name, t.TypeParameters.Count), comparer)
                       .ToDictionary(g => g.Key, g => new DefaultResolvedTypeDefinition(context, g.ToArray()), comparer);
                return(LazyInit.GetOrSet(ref this.typeDict, dict));
            }
        }
Ejemplo n.º 4
0
        private void DecodeSignature()
        {
            var   methodDef      = module.metadata.GetMethodDefinition(handle);
            var   genericContext = new GenericContext(DeclaringType.TypeParameters, this.TypeParameters);
            IType returnType;

            IParameter[] parameters;
            try {
                var nullableContext = methodDef.GetCustomAttributes().GetNullableContext(module.metadata) ?? DeclaringTypeDefinition.NullableContext;
                var signature       = methodDef.DecodeSignature(module.TypeProvider, genericContext);
                (returnType, parameters) = DecodeSignature(module, this, signature, methodDef.GetParameters(), nullableContext);
            } catch (BadImageFormatException) {
                returnType = SpecialType.UnknownType;
                parameters = Empty <IParameter> .Array;
            }
            LazyInit.GetOrSet(ref this.returnType, returnType);
            LazyInit.GetOrSet(ref this.parameters, parameters);
        }
Ejemplo n.º 5
0
        Dictionary <string, INamespace> GetChildNamespaces()
        {
            var result = LazyInit.VolatileRead(ref this.childNamespaces);

            if (result != null)
            {
                return(result);
            }
            else
            {
                result = new Dictionary <string, INamespace>(compilation.NameComparer);
                foreach (var g in namespaces.SelectMany(ns => ns.ChildNamespaces).GroupBy(ns => ns.Name, compilation.NameComparer))
                {
                    result.Add(g.Key, new MergedNamespace(this, g.ToArray()));
                }
                return(LazyInit.GetOrSet(ref this.childNamespaces, result));
            }
        }
Ejemplo n.º 6
0
        internal IMethod GetAccessor(ref IMethod accessorField, IUnresolvedMethod unresolvedAccessor)
        {
            if (unresolvedAccessor == null)
            {
                return(null);
            }
            IMethod result = accessorField;

            if (result != null)
            {
                LazyInit.ReadBarrier();
                return(result);
            }
            else
            {
                return(LazyInit.GetOrSet(ref accessorField, (IMethod)unresolvedAccessor.CreateResolved(context)));
            }
        }
Ejemplo n.º 7
0
        internal IMethod WrapAccessor(ref IMethod cachingField, IMethod accessorDefinition)
        {
            if (accessorDefinition == null)
            {
                return(null);
            }
            var result = LazyInit.VolatileRead(ref cachingField);

            if (result != null)
            {
                return(result);
            }
            else
            {
                var sm = accessorDefinition.Specialize(substitution);
                //sm.AccessorOwner = this;
                return(LazyInit.GetOrSet(ref cachingField, sm));
            }
        }
Ejemplo n.º 8
0
        Dictionary <FullNameAndTypeParameterCount, ITypeDefinition> GetTypes()
        {
            var dict = LazyInit.VolatileRead(ref this.typeDict);

            if (dict != null)
            {
                return(dict);
            }
            else
            {
                // Always use the ordinal comparer for the main dictionary so that partial classes
                // get merged correctly.
                // The compilation's comparer will be used for the per-namespace dictionaries.
                var comparer = FullNameAndTypeParameterCountComparer.Ordinal;
                dict = projectContent.TopLevelTypeDefinitions
                       .GroupBy(t => new FullNameAndTypeParameterCount(t.Namespace, t.Name, t.TypeParameters.Count), comparer)
                       .ToDictionary(g => g.Key, g => CreateResolvedTypeDefinition(g.ToArray()), comparer);
                return(LazyInit.GetOrSet(ref this.typeDict, dict));
            }
        }
Ejemplo n.º 9
0
        private IType DecodeTypeAndVolatileFlag()
        {
            var   metadata = module.metadata;
            var   fieldDef = metadata.GetFieldDefinition(handle);
            IType ty;

            try {
                ty = fieldDef.DecodeSignature(module.TypeProvider, new GenericContext(DeclaringType?.TypeParameters));
                if (ty is ModifiedType mod && mod.Modifier.Name == "IsVolatile" && mod.Modifier.Namespace == "System.Runtime.CompilerServices")
                {
                    Volatile.Write(ref this.isVolatile, true);
                    ty = mod.ElementType;
                }
                ty = ApplyAttributeTypeVisitor.ApplyAttributesToType(ty, Compilation,
                                                                     fieldDef.GetCustomAttributes(), metadata, module.TypeSystemOptions);
            } catch (BadImageFormatException) {
                ty = SpecialType.UnknownType;
            }
            return(LazyInit.GetOrSet(ref this.type, ty));
        }
Ejemplo n.º 10
0
        public object GetConstantValue(bool throwOnInvalidMetadata)
        {
            object val = LazyInit.VolatileRead(ref this.constantValue);

            if (val != null)
            {
                return(val);
            }
            try
            {
                var metadata = module.metadata;
                var fieldDef = metadata.GetFieldDefinition(handle);
                if (IsDecimalConstant && DecimalConstantHelper.AllowsDecimalConstants(module))
                {
                    val = DecimalConstantHelper.GetDecimalConstantValue(module, fieldDef.GetCustomAttributes());
                }
                else
                {
                    var constantHandle = fieldDef.GetDefaultValue();
                    if (constantHandle.IsNil)
                    {
                        return(null);
                    }
                    var constant   = metadata.GetConstant(constantHandle);
                    var blobReader = metadata.GetBlobReader(constant.Value);
                    try
                    {
                        val = blobReader.ReadConstant(constant.TypeCode);
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        throw new BadImageFormatException($"Constant with invalid typecode: {constant.TypeCode}");
                    }
                }
                return(LazyInit.GetOrSet(ref this.constantValue, val));
            }
            catch (BadImageFormatException) when(!throwOnInvalidMetadata)
            {
                return(null);
            }
        }
        internal IUnresolvedTypeDefinition GetTypeDefByToken(Mono.Cecil.MetadataToken token)
        {
            if (token.TokenType != Mono.Cecil.TokenType.TypeDef)
            {
                throw new ArgumentException("Token must be typedef-token.");
            }
            var lookup = LazyInit.VolatileRead(ref allTypesByMetadata);

            if (lookup == null)
            {
                lookup = LazyInit.GetOrSet(ref allTypesByMetadata, BuildMetadataLookup());
            }
            if (token.RID < lookup.Length)
            {
                return(lookup[token.RID]);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 12
0
        private void DecodeSignature()
        {
            var   methodDef      = module.metadata.GetMethodDefinition(handle);
            var   genericContext = new GenericContext(DeclaringType.TypeParameters, this.TypeParameters);
            IType returnType;

            IParameter[] parameters;
            ModifiedType mod;

            try {
                var nullableContext = methodDef.GetCustomAttributes().GetNullableContext(module.metadata) ?? DeclaringTypeDefinition.NullableContext;
                var signature       = methodDef.DecodeSignature(module.TypeProvider, genericContext);
                (returnType, parameters, mod) = DecodeSignature(module, this, signature, methodDef.GetParameters(), nullableContext, module.OptionsForEntity(this));
            } catch (BadImageFormatException) {
                returnType = SpecialType.UnknownType;
                parameters = Empty <IParameter> .Array;
                mod        = null;
            }
            this.isInitOnly = mod is { Modifier : { Name : "IsExternalInit", Namespace : "System.Runtime.CompilerServices" } };
            LazyInit.GetOrSet(ref this.returnType, returnType);
            LazyInit.GetOrSet(ref this.parameters, parameters);
        }
Ejemplo n.º 13
0
        private void DecodeSignature()
        {
            var   propertyDef    = module.metadata.GetPropertyDefinition(propertyHandle);
            var   genericContext = new GenericContext(DeclaringType.TypeParameters);
            IType returnType;

            IParameter[] parameters;
            try {
                var signature = propertyDef.DecodeSignature(module.TypeProvider, genericContext);
                var accessors = propertyDef.GetAccessors();
                ParameterHandleCollection?parameterHandles;
                Nullability nullableContext;
                if (!accessors.Getter.IsNil)
                {
                    var getter = module.metadata.GetMethodDefinition(accessors.Getter);
                    parameterHandles = getter.GetParameters();
                    nullableContext  = getter.GetCustomAttributes().GetNullableContext(module.metadata)
                                       ?? DeclaringTypeDefinition?.NullableContext ?? Nullability.Oblivious;
                }
                else if (!accessors.Setter.IsNil)
                {
                    var setter = module.metadata.GetMethodDefinition(accessors.Setter);
                    parameterHandles = setter.GetParameters();
                    nullableContext  = setter.GetCustomAttributes().GetNullableContext(module.metadata)
                                       ?? DeclaringTypeDefinition?.NullableContext ?? Nullability.Oblivious;
                }
                else
                {
                    parameterHandles = null;
                    nullableContext  = DeclaringTypeDefinition?.NullableContext ?? Nullability.Oblivious;
                }
                (returnType, parameters) = MetadataMethod.DecodeSignature(module, this, signature, parameterHandles, nullableContext);
            } catch (BadImageFormatException) {
                returnType = SpecialType.UnknownType;
                parameters = Empty <IParameter> .Array;
            }
            LazyInit.GetOrSet(ref this.returnType, returnType);
            LazyInit.GetOrSet(ref this.parameters, parameters);
        }
Ejemplo n.º 14
0
        MemberList GetMemberList()
        {
            var result = LazyInit.VolatileRead(ref this.memberList);

            if (result != null)
            {
                return(result);
            }
            List <IUnresolvedMember>   unresolvedMembers = new List <IUnresolvedMember>();
            List <ITypeResolveContext> contextPerMember  = new List <ITypeResolveContext>();
            bool addDefaultConstructorIfRequired         = false;

            foreach (IUnresolvedTypeDefinition part in parts)
            {
                ITypeResolveContext parentContextForPart = part.CreateResolveContext(parentContext);
                ITypeResolveContext contextForPart       = parentContextForPart.WithCurrentTypeDefinition(this);
                foreach (var member in part.Members)
                {
                    IUnresolvedMethod method = member as IUnresolvedMethod;

                    unresolvedMembers.Add(member);
                    contextPerMember.Add(contextForPart);
                }

                addDefaultConstructorIfRequired |= part.AddDefaultConstructorIfRequired;
            }
            if (addDefaultConstructorIfRequired)
            {
                TypeKind kind = this.Kind;
                if (kind == TypeKind.Class && !this.IsStatic && !unresolvedMembers.Any(m => m.SymbolKind == SymbolKind.Constructor && !m.IsStatic) ||
                    kind == TypeKind.Enum || kind == TypeKind.Struct)
                {
                    contextPerMember.Add(parts[0].CreateResolveContext(parentContext).WithCurrentTypeDefinition(this));
                    unresolvedMembers.Add(MethodCore.CreateDefaultConstructor(parts[0]));
                }
            }
            result = new MemberList(contextPerMember, unresolvedMembers);
            return(LazyInit.GetOrSet(ref this.memberList, result));
        }
Ejemplo n.º 15
0
        IList <IAttribute> GetAttributes(ref IList <IAttribute> field, bool assemblyAttributes)
        {
            IList <IAttribute> result = LazyInit.VolatileRead(ref field);

            if (result != null)
            {
                return(result);
            }
            else
            {
                result = new List <IAttribute>();
                foreach (var unresolvedFile in projectContent.Files.OfType <CSharpUnresolvedFile>())
                {
                    var attributes = assemblyAttributes ? unresolvedFile.AssemblyAttributes : unresolvedFile.ModuleAttributes;
                    var context    = new CSharpTypeResolveContext(this, unresolvedFile.RootUsingScope.Resolve(compilation));
                    foreach (var unresolvedAttr in attributes)
                    {
                        result.Add(unresolvedAttr.CreateResolvedAttribute(context));
                    }
                }
                return(LazyInit.GetOrSet(ref field, result));
            }
        }
Ejemplo n.º 16
0
        private void DecodeSignature()
        {
            var propertyDef    = module.metadata.GetPropertyDefinition(propertyHandle);
            var genericContext = new GenericContext(DeclaringType.TypeParameters);
            var signature      = propertyDef.DecodeSignature(module.TypeProvider, genericContext);
            var accessors      = propertyDef.GetAccessors();
            ParameterHandleCollection?parameterHandles;

            if (!accessors.Getter.IsNil)
            {
                parameterHandles = module.metadata.GetMethodDefinition(accessors.Getter).GetParameters();
            }
            else if (!accessors.Setter.IsNil)
            {
                parameterHandles = module.metadata.GetMethodDefinition(accessors.Setter).GetParameters();
            }
            else
            {
                parameterHandles = null;
            }
            var(returnType, parameters) = MetadataMethod.DecodeSignature(module, this, signature, parameterHandles);
            LazyInit.GetOrSet(ref this.returnType, returnType);
            LazyInit.GetOrSet(ref this.parameters, parameters);
        }
Ejemplo n.º 17
0
        MemberList GetMemberList()
        {
            var result = LazyInit.VolatileRead(ref this.memberList);

            if (result != null)
            {
                return(result);
            }
            List <IUnresolvedMember>   unresolvedMembers  = new List <IUnresolvedMember>();
            List <ITypeResolveContext> contextPerMember   = new List <ITypeResolveContext>();
            List <PartialMethodInfo>   partialMethodInfos = null;
            bool addDefaultConstructorIfRequired          = false;

            foreach (IUnresolvedTypeDefinition part in parts)
            {
                ITypeResolveContext parentContextForPart = part.CreateResolveContext(parentContext);
                ITypeResolveContext contextForPart       = parentContextForPart.WithCurrentTypeDefinition(this);
                foreach (var member in part.Members)
                {
                    IUnresolvedMethod method = member as IUnresolvedMethod;
                    if (method != null && method.IsPartial)
                    {
                        // Merge partial method declaration and implementation
                        if (partialMethodInfos == null)
                        {
                            partialMethodInfos = new List <PartialMethodInfo>();
                        }
                        PartialMethodInfo newInfo      = new PartialMethodInfo(method, contextForPart);
                        PartialMethodInfo existingInfo = null;
                        foreach (var info in partialMethodInfos)
                        {
                            if (newInfo.IsSameSignature(info, Compilation.NameComparer))
                            {
                                existingInfo = info;
                                break;
                            }
                        }
                        if (existingInfo != null)
                        {
                            // Add the unresolved method to the PartialMethodInfo:
                            existingInfo.AddPart(method, contextForPart);
                        }
                        else
                        {
                            partialMethodInfos.Add(newInfo);
                        }
                    }
                    else
                    {
                        unresolvedMembers.Add(member);
                        contextPerMember.Add(contextForPart);
                    }
                }

                addDefaultConstructorIfRequired |= part.AddDefaultConstructorIfRequired;
            }
            if (addDefaultConstructorIfRequired)
            {
                TypeKind kind = this.Kind;
                if (kind == TypeKind.Class && !this.IsStatic && !unresolvedMembers.Any(m => m.SymbolKind == SymbolKind.Constructor && !m.IsStatic) ||
                    kind == TypeKind.Enum || kind == TypeKind.Struct)
                {
                    contextPerMember.Add(parts[0].CreateResolveContext(parentContext).WithCurrentTypeDefinition(this));
                    unresolvedMembers.Add(DefaultUnresolvedMethod.CreateDefaultConstructor(parts[0]));
                }
            }
            result = new MemberList(contextPerMember, unresolvedMembers, partialMethodInfos);
            return(LazyInit.GetOrSet(ref this.memberList, result));
        }