public MethodDefinition(string name, MethodAttributes attributes, TypeReference returnType)
     : base(name, returnType)
 {
     this.attributes = (ushort)attributes;
     this.HasThis = !this.IsStatic;
     this.token = new MetadataToken(TokenType.Method);
 }
 public OptionalModifierType(TypeReference modifierType, TypeReference type)
     : base(type)
 {
     Mixin.CheckModifier(modifierType, type);
     this.modifier_type = modifierType;
     this.etype = MD.ElementType.CModOpt;
 }
        internal ParameterReference(string name, TypeReference parameterType)
        {
            if (parameterType == null)
                throw new ArgumentNullException("parameterType");

            this.name = name ?? string.Empty;
            this.parameter_type = parameterType;
        }
        public MethodReference(string name, TypeReference returnType, TypeReference declaringType)
            : this(name, returnType)
        {
            if (declaringType == null)
                throw new ArgumentNullException("declaringType");

            this.DeclaringType = declaringType;
        }
        protected EventReference(string name, TypeReference eventType)
            : base(name)
        {
            if (eventType == null)
                throw new ArgumentNullException("eventType");

            event_type = eventType;
        }
        public CallSite(TypeReference returnType)
            : this()
        {
            if (returnType == null)
                throw new ArgumentNullException("returnType");

            this.signature.ReturnType = returnType;
        }
        internal PropertyReference(string name, TypeReference propertyType)
            : base(name)
        {
            if (propertyType == null)
                throw new ArgumentNullException("propertyType");

            property_type = propertyType;
        }
        public FieldReference(string name, TypeReference fieldType, TypeReference declaringType)
            : this(name, fieldType)
        {
            if (declaringType == null)
                throw new ArgumentNullException("declaringType");

            this.DeclaringType = declaringType;
        }
        public FieldReference(string name, TypeReference fieldType)
            : base(name)
        {
            if (fieldType == null)
                throw new ArgumentNullException("fieldType");

            this.field_type = fieldType;
            this.token = new MetadataToken(TokenType.MemberRef);
        }
        public MethodReference(string name, TypeReference returnType)
            : base(name)
        {
            if (returnType == null)
                throw new ArgumentNullException("returnType");

            this.return_type = new MethodReturnType(this);
            this.return_type.ReturnType = returnType;
            this.token = new MetadataToken(TokenType.MemberRef);
        }
 public void AddTypeReference(TypeReference type)
 {
     TypeReferences[type.token.RID - 1] = type;
 }
        public ArrayType(TypeReference type, int rank)
            : this(type)
        {
            Mixin.CheckType(type);

            if (rank == 1)
                return;

            dimensions = new Collection<ArrayDimension>(rank);
            for (int i = 0; i < rank; i++)
                dimensions.Add(new ArrayDimension());
            this.etype = MD.ElementType.Array;
        }
 public PinnedType(TypeReference type)
     : base(type)
 {
     Mixin.CheckType(type);
     this.etype = MD.ElementType.Pinned;
 }
        TypeReference ImportTypeSpecification(TypeReference type, ImportGenericContext context)
        {
            switch (type.etype)
            {
                case ElementType.SzArray:
                    var vector = (ArrayType)type;
                    return new ArrayType(ImportType(vector.ElementType, context));
                case ElementType.Ptr:
                    var pointer = (PointerType)type;
                    return new PointerType(ImportType(pointer.ElementType, context));
                case ElementType.ByRef:
                    var byref = (ByReferenceType)type;
                    return new ByReferenceType(ImportType(byref.ElementType, context));
                case ElementType.Pinned:
                    var pinned = (PinnedType)type;
                    return new PinnedType(ImportType(pinned.ElementType, context));
                case ElementType.Sentinel:
                    var sentinel = (SentinelType)type;
                    return new SentinelType(ImportType(sentinel.ElementType, context));
                case ElementType.CModOpt:
                    var modopt = (OptionalModifierType)type;
                    return new OptionalModifierType(
                        ImportType(modopt.ModifierType, context),
                        ImportType(modopt.ElementType, context));
                case ElementType.CModReqD:
                    var modreq = (RequiredModifierType)type;
                    return new RequiredModifierType(
                        ImportType(modreq.ModifierType, context),
                        ImportType(modreq.ElementType, context));
                case ElementType.Array:
                    var array = (ArrayType)type;
                    var imported_array = new ArrayType(ImportType(array.ElementType, context));
                    if (array.IsVector)
                        return imported_array;

                    var dimensions = array.Dimensions;
                    var imported_dimensions = imported_array.Dimensions;

                    imported_dimensions.Clear();

                    for (int i = 0; i < dimensions.Count; i++)
                    {
                        var dimension = dimensions[i];

                        imported_dimensions.Add(new ArrayDimension(dimension.LowerBound, dimension.UpperBound));
                    }

                    return imported_array;
                case ElementType.GenericInst:
                    var instance = (GenericInstanceType)type;
                    var element_type = ImportType(instance.ElementType, context);
                    var imported_instance = new GenericInstanceType(element_type);

                    var arguments = instance.GenericArguments;
                    var imported_arguments = imported_instance.GenericArguments;

                    for (int i = 0; i < arguments.Count; i++)
                        imported_arguments.Add(ImportType(arguments[i], context));

                    return imported_instance;
                case ElementType.Var:
                    var var_parameter = (GenericParameter)type;
                    return context.TypeParameter(type.DeclaringType.FullName, var_parameter.Position);
                case ElementType.MVar:
                    var mvar_parameter = (GenericParameter)type;
                    return context.MethodParameter(mvar_parameter.DeclaringMethod.Name, mvar_parameter.Position);
            }

            throw new NotSupportedException(type.etype.ToString());
        }
 public ArrayType(TypeReference type)
     : base(type)
 {
     Mixin.CheckType(type);
     this.etype = MD.ElementType.Array;
 }
        public TypeReference ImportType(Type type, ImportGenericContext context, ImportGenericKind import_kind)
        {
            if (IsTypeSpecification(type) || ImportOpenGenericType(type, import_kind))
                return ImportTypeSpecification(type, context);

            var reference = new TypeReference(
                string.Empty,
                type.Name,
                module,
                ImportScope(type.Assembly),
                type.IsValueType);

            reference.etype = ImportElementType(type);

            if (IsNestedType(type))
                reference.DeclaringType = ImportType(type.DeclaringType, context, import_kind);
            else
                reference.Namespace = type.Namespace ?? string.Empty;

            if (type.IsGenericType)
                ImportGenericParameters(reference, type.GetGenericArguments());

            return reference;
        }
        public TypeReference ImportType(TypeReference type, ImportGenericContext context)
        {
            if (type.IsTypeSpecification())
                return ImportTypeSpecification(type, context);

            var reference = new TypeReference(
                type.Namespace,
                type.Name,
                module,
                ImportScope(type.Scope),
                type.IsValueType);

            MetadataSystem.TryProcessPrimitiveTypeReference(reference);

            if (type.IsNested)
                reference.DeclaringType = ImportType(type.DeclaringType, context);

            if (type.HasGenericParameters)
                ImportGenericParameters(reference, type);

            return reference;
        }
        static bool AreSame(TypeReference a, TypeReference b)
        {
            if (ReferenceEquals(a, b))
                return true;

            if (a == null || b == null)
                return false;

            if (a.etype != b.etype)
                return false;

            if (a.IsGenericParameter)
                return AreSame((GenericParameter)a, (GenericParameter)b);

            if (a.IsTypeSpecification())
                return AreSame((TypeSpecification)a, (TypeSpecification)b);

            if (a.Name != b.Name || a.Namespace != b.Namespace)
                return false;

            //TODO: check scope

            return AreSame(a.DeclaringType, b.DeclaringType);
        }
        static TypeDefinition GetType(ModuleDefinition module, TypeReference reference)
        {
            var type = GetTypeDefinition(module, reference);
            if (type != null)
                return type;

            if (!module.HasExportedTypes)
                return null;

            var exported_types = module.ExportedTypes;

            for (int i = 0; i < exported_types.Count; i++)
            {
                var exported_type = exported_types[i];
                if (exported_type.Name != reference.Name)
                    continue;

                if (exported_type.Namespace != reference.Namespace)
                    continue;

                return exported_type.Resolve();
            }

            return null;
        }
 public SentinelType(TypeReference type)
     : base(type)
 {
     Mixin.CheckType(type);
     this.etype = MD.ElementType.Sentinel;
 }
 public static void CheckModifier(TypeReference modifierType, TypeReference type)
 {
     if (modifierType == null)
         throw new ArgumentNullException("modifierType");
     if (type == null)
         throw new ArgumentNullException("type");
 }
 public FieldDefinition(string name, FieldAttributes attributes, TypeReference fieldType)
     : base(name, fieldType)
 {
     this.attributes = (ushort)attributes;
 }
 internal ParameterDefinition(TypeReference parameterType, IMethodSignature method)
     : this(string.Empty, ParameterAttributes.None, parameterType)
 {
     this.method = method;
 }
 public ParameterDefinition(string name, ParameterAttributes attributes, TypeReference parameterType)
     : base(name, parameterType)
 {
     this.attributes = (ushort)attributes;
     this.token = new MetadataToken(TokenType.Param);
 }
 public ParameterDefinition(TypeReference parameterType)
     : this(string.Empty, ParameterAttributes.None, parameterType)
 {
 }
 public SecurityAttribute(TypeReference attributeType)
 {
     this.attribute_type = attributeType;
 }
 public RequiredModifierType(TypeReference modifierType, TypeReference type)
     : base(type)
 {
     Mixin.CheckModifier(modifierType, type);
     this.modifier_type = modifierType;
     this.etype = MD.ElementType.CModReqD;
 }
        static bool TryGetPrimitiveData(TypeReference type, out Row<ElementType, bool> primitive_data)
        {
            if (primitive_value_types == null)
                InitializePrimitives();

            return primitive_value_types.TryGetValue(type.Name, out primitive_data);
        }
        public static void TryProcessPrimitiveTypeReference(TypeReference type)
        {
            if (type.Namespace != "System")
                return;

            var scope = type.scope;
            if (scope == null || scope.MetadataScopeType != MetadataScopeType.AssemblyNameReference)
                return;

            Row<ElementType, bool> primitive_data;
            if (!TryGetPrimitiveData(type, out primitive_data))
                return;

            type.etype = primitive_data.Col1;
            type.IsValueType = primitive_data.Col2;
        }
        static TypeDefinition GetTypeDefinition(ModuleDefinition module, TypeReference type)
        {
            if (!type.IsNested)
                return module.GetType(type.Namespace, type.Name);

            var declaring_type = type.DeclaringType.Resolve();
            if (declaring_type == null)
                return null;

            return declaring_type.GetNestedType(type.TypeFullName());
        }