Esempio n. 1
0
        private IReadOnlyList <ConstructorSyntaxNode> GenerateClassConstructors(ITypeBuilder type, IReadOnlyList <ConstructorSyntaxNode> constructors, CodeGenerationStore store,
                                                                                IReadOnlyList <StatementSyntaxNode> fieldInitializers,
                                                                                Dictionary <IConstructorBuilder, ConstructorSyntaxNode> constructorsDictionary, ISyntaxNode parent)
        {
            var initialConstructors = new List <ConstructorSyntaxNode>(constructors);
            var mutatedConstructors = new List <ConstructorSyntaxNode>();

            var definedConstructors = new List <IConstructorInfo>();

            store.Constructors.Add(type, definedConstructors);

            if (constructors.Count == 0)
            {
                var statementList = new List <StatementSyntaxNode>();
                initialConstructors.Add(new ConstructorSyntaxNode(parent, Array.Empty <ParameterDefinitionSyntaxNode>(), statementList));
            }

            foreach (var constructor in initialConstructors)
            {
                var newStatements = new List <StatementSyntaxNode>(fieldInitializers);
                foreach (var toAdd in constructor.Statements)
                {
                    newStatements.Add(toAdd);
                }

                var mutatedConstructor = constructor.MutateStatements(newStatements);
                mutatedConstructors.Add(mutatedConstructor);

                var methodAttributes = MethodAttributes.Public;

                var parameters = mutatedConstructor.Parameters.Select(x =>
                {
                    var tpe = store.TypeDefLookup(x.Type);
                    if (x.IsRef)
                    {
                        throw new RefTypeException("Ref types are not supported");
                    }
                    typeChecker !.AssertTypeIsNotVoid(tpe);
                    return(tpe);
                }).ToArray();

                var definedConstructor = type.DefineConstructor(methodAttributes, parameters);

                definedConstructors.Add(definedConstructor);

                store.ConstructorParameters.Add(definedConstructor, parameters);
                constructorsDictionary.Add(definedConstructor, mutatedConstructor);

                int offset = 0;

                for (int i = 0; i < mutatedConstructor.Parameters.Count; i++)
                {
                    definedConstructor.DefineParameter(i + 1 + offset, ParameterAttributes.None, mutatedConstructor.Parameters[i].Name);
                }
            }
            return(mutatedConstructors);
        }
Esempio n. 2
0
        public IConstructorBuilder DefineConstructor(MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes)
        {
            ArgumentUtility.CheckNotNull("parameterTypes", parameterTypes);

            var emittableParameterTypes = parameterTypes.Select(EmittableOperandProvider.GetEmittableType).ToArray();
            var constructorBuilder      = _typeBuilder.DefineConstructor(attributes, callingConvention, emittableParameterTypes);

            return(new ConstructorBuilderDecorator(constructorBuilder, EmittableOperandProvider));
        }