private static TypeSig Resolve(TypeSig type, TypeSig typeContext)
        {
            var args = new GenericArguments();

            if (typeContext.IsGenericInstanceType)
            {
                args.PushTypeArgs(((GenericInstSig)typeContext).GenericArguments);
            }
            return(args.Resolve(type));
        }
        private static MethodSig Resolve(MethodBaseSig method, TypeSig typeContext)
        {
            var args = new GenericArguments();

            if (typeContext.IsGenericInstanceType)
            {
                args.PushTypeArgs(((GenericInstSig)typeContext).GenericArguments);
            }

            var retSig = new MethodSig(method.CallingConvention);

            retSig.RetType = args.Resolve(method.RetType);
            foreach (var param in method.Params)
            {
                retSig.Params.Add(args.Resolve(param));
            }
            return(retSig);
        }
        private static ASTType?InferVarPush(ILASTExpression expr)
        {
            var method = (IMethod)expr.Operand;

            if (method.MethodSig.RetType.ElementType == ElementType.Void)
            {
                return(null);
            }

            var genArgs = new GenericArguments();

            if (method is MethodSpec)
            {
                genArgs.PushMethodArgs(((MethodSpec)method).GenericInstMethodSig.GenericArguments);
            }
            if (method.DeclaringType.TryGetGenericInstSig() != null)
            {
                genArgs.PushTypeArgs(method.DeclaringType.TryGetGenericInstSig().GenericArguments);
            }


            return(TypeInference.ToASTType(genArgs.ResolveType(method.MethodSig.RetType)));
        }
        private static IEnumerable <TypeSig> BaseTypes(TypeSig type)
        {
            TypeDef typeDef = type.ToTypeDefOrRef().ResolveTypeDefThrow();

            if (typeDef.BaseType == null)
            {
                yield break;
            }

            TypeSig baseType = type;

            do
            {
                var args = new GenericArguments();
                if (baseType.IsGenericInstanceType)
                {
                    args.PushTypeArgs(((GenericInstSig)baseType).GenericArguments);
                }
                baseType = args.Resolve(typeDef.BaseType.ToTypeSig());
                yield return(baseType);

                typeDef = typeDef.BaseType.ResolveTypeDef();
            } while (typeDef != null && typeDef.BaseType != null);
        }
 /// <summary>
 /// Pushes the type generic arguments into resolver stack.
 /// </summary>
 /// <param name="genericArgs">The generic arguments.</param>
 public void PushTypeGenericArguments(IList <TypeSig> genericArgs)
 {
     genericArguments.PushTypeArgs(genericArgs);
 }
		void Write(CustomAttribute ca) {
			if (ca == null) {
				helper.Error("The custom attribute is null");
				return;
			}

			// Check whether it's raw first. If it is, we don't care whether the ctor is
			// invalid. Just use the raw data.
			if (ca.IsRawBlob) {
				if ((ca.ConstructorArguments != null && ca.ConstructorArguments.Count > 0) || (ca.NamedArguments != null && ca.NamedArguments.Count > 0))
					helper.Error("Raw custom attribute contains arguments and/or named arguments");
				writer.Write(ca.RawData);
				return;
			}

			if (ca.Constructor == null) {
				helper.Error("Custom attribute ctor is null");
				return;
			}

			var methodSig = GetMethodSig(ca.Constructor);
			if (methodSig == null) {
				helper.Error("Custom attribute ctor's method signature is invalid");
				return;
			}

			if (ca.ConstructorArguments.Count != methodSig.Params.Count)
				helper.Error("Custom attribute arguments count != method sig arguments count");
			if (methodSig.ParamsAfterSentinel != null && methodSig.ParamsAfterSentinel.Count > 0)
				helper.Error("Custom attribute ctor has parameters after the sentinel");
			if (ca.NamedArguments.Count > ushort.MaxValue)
				helper.Error("Custom attribute has too many named arguments");

			// A generic custom attribute isn't allowed by most .NET languages (eg. C#) but
			// the CLR probably supports it.
			var mrCtor = ca.Constructor as MemberRef;
			if (mrCtor != null) {
				var owner = mrCtor.Class as TypeSpec;
				if (owner != null) {
					var gis = owner.TypeSig as GenericInstSig;
					if (gis != null) {
						genericArguments = new GenericArguments();
						genericArguments.PushTypeArgs(gis.GenericArguments);
					}
				}
			}

			writer.Write((ushort)1);

			int numArgs = Math.Min(methodSig.Params.Count, ca.ConstructorArguments.Count);
			for (int i = 0; i < numArgs; i++)
				WriteValue(FixTypeSig(methodSig.Params[i]), ca.ConstructorArguments[i]);

			int numNamedArgs = Math.Min((int)ushort.MaxValue, ca.NamedArguments.Count);
			writer.Write((ushort)numNamedArgs);
			for (int i = 0; i < numNamedArgs; i++)
				Write(ca.NamedArguments[i]);
		}
        void Write(CustomAttribute ca)
        {
            if (ca is null)
            {
                helper.Error("The custom attribute is null");
                return;
            }

            // Check whether it's raw first. If it is, we don't care whether the ctor is
            // invalid. Just use the raw data.
            if (ca.IsRawBlob)
            {
                if ((ca.ConstructorArguments is not null && ca.ConstructorArguments.Count > 0) || (ca.NamedArguments is not null && ca.NamedArguments.Count > 0))
                {
                    helper.Error("Raw custom attribute contains arguments and/or named arguments");
                }
                writer.WriteBytes(ca.RawData);
                return;
            }

            if (ca.Constructor is null)
            {
                helper.Error("Custom attribute ctor is null");
                return;
            }

            var methodSig = GetMethodSig(ca.Constructor);

            if (methodSig is null)
            {
                helper.Error("Custom attribute ctor's method signature is invalid");
                return;
            }

            if (ca.ConstructorArguments.Count != methodSig.Params.Count)
            {
                helper.Error("Custom attribute arguments count != method sig arguments count");
            }
            if (methodSig.ParamsAfterSentinel is not null && methodSig.ParamsAfterSentinel.Count > 0)
            {
                helper.Error("Custom attribute ctor has parameters after the sentinel");
            }
            if (ca.NamedArguments.Count > ushort.MaxValue)
            {
                helper.Error("Custom attribute has too many named arguments");
            }

            if (ca.Constructor is MemberRef mrCtor && mrCtor.Class is TypeSpec owner && owner.TypeSig is GenericInstSig gis)
            {
                genericArguments = new GenericArguments();
                genericArguments.PushTypeArgs(gis.GenericArguments);
            }

            writer.WriteUInt16((ushort)1);

            int numArgs = Math.Min(methodSig.Params.Count, ca.ConstructorArguments.Count);

            for (int i = 0; i < numArgs; i++)
            {
                WriteValue(FixTypeSig(methodSig.Params[i]), ca.ConstructorArguments[i]);
            }

            int numNamedArgs = Math.Min((int)ushort.MaxValue, ca.NamedArguments.Count);

            writer.WriteUInt16((ushort)numNamedArgs);
            for (int i = 0; i < numNamedArgs; i++)
            {
                Write(ca.NamedArguments[i]);
            }
        }
        void Write(CustomAttribute ca)
        {
            if (ca == null)
            {
                helper.Error("The custom attribute is null");
                return;
            }

            // Check whether it's raw first. If it is, we don't care whether the ctor is
            // invalid. Just use the raw data.
            if (ca.IsRawBlob)
            {
                if ((ca.ConstructorArguments != null && ca.ConstructorArguments.Count > 0) || (ca.NamedArguments != null && ca.NamedArguments.Count > 0))
                {
                    helper.Error("Raw custom attribute contains arguments and/or named arguments");
                }
                writer.Write(ca.RawData);
                return;
            }

            if (ca.Constructor == null)
            {
                helper.Error("Custom attribute ctor is null");
                return;
            }

            var methodSig = GetMethodSig(ca.Constructor);

            if (methodSig == null)
            {
                helper.Error("Custom attribute ctor's method signature is invalid");
                return;
            }

            if (ca.ConstructorArguments.Count != methodSig.Params.Count)
            {
                helper.Error("Custom attribute arguments count != method sig arguments count");
            }
            if (methodSig.ParamsAfterSentinel != null && methodSig.ParamsAfterSentinel.Count > 0)
            {
                helper.Error("Custom attribute ctor has parameters after the sentinel");
            }
            if (ca.NamedArguments.Count > ushort.MaxValue)
            {
                helper.Error("Custom attribute has too many named arguments");
            }

            // A generic custom attribute isn't allowed by most .NET languages (eg. C#) but
            // the CLR probably supports it.
            var mrCtor = ca.Constructor as MemberRef;

            if (mrCtor != null)
            {
                var owner = mrCtor.Class as TypeSpec;
                if (owner != null)
                {
                    var gis = owner.TypeSig as GenericInstSig;
                    if (gis != null)
                    {
                        genericArguments = new GenericArguments();
                        genericArguments.PushTypeArgs(gis.GenericArguments);
                    }
                }
            }

            writer.Write((ushort)1);

            int numArgs = Math.Min(methodSig.Params.Count, ca.ConstructorArguments.Count);

            for (int i = 0; i < numArgs; i++)
            {
                WriteValue(FixTypeSig(methodSig.Params[i]), ca.ConstructorArguments[i]);
            }

            int numNamedArgs = Math.Min((int)ushort.MaxValue, ca.NamedArguments.Count);

            writer.Write((ushort)numNamedArgs);
            for (int i = 0; i < numNamedArgs; i++)
            {
                Write(ca.NamedArguments[i]);
            }
        }