Ejemplo n.º 1
0
        internal override void ResolveTypes(ParserContext parser, TypeResolver typeResolver)
        {
            for (int i = 0; i < this.ArgNames.Length; ++i)
            {
                this.ArgLocalIds[i].ResolvedType = typeResolver.ResolveType(this.ArgTypes[i]);
            }

            foreach (Expression defaultArg in this.DefaultValues)
            {
                if (defaultArg != null)
                {
                    defaultArg.ResolveTypes(parser, typeResolver);
                }
            }

            ClassDefinition cd = (ClassDefinition)this.Owner;

            if (this.Modifiers.HasStatic)
            {
                // already verified that there's no base constructor invocation
            }
            else if (cd.BaseClass != null)
            {
                ConstructorDefinition baseConstructor         = cd.BaseClass.Constructor;
                ResolvedType[]        baseConstructorArgTypes = baseConstructor == null
                    ? new ResolvedType[0]
                    : baseConstructor.ResolvedArgTypes;
                Expression[] baseConstructorDefaultValues = baseConstructor == null
                    ? new Expression[0]
                    : baseConstructor.DefaultValues;
                int optionalArgCount = FunctionCall.CountOptionalArgs(baseConstructorDefaultValues);
                int maxArgCount      = baseConstructorArgTypes.Length;
                int minArgCount      = maxArgCount - optionalArgCount;
                if (this.BaseArgs.Length < minArgCount || this.BaseArgs.Length > maxArgCount)
                {
                    throw new ParserException(this, "Incorrect number of arguments passed to base constructor.");
                }

                for (int i = 0; i < this.BaseArgs.Length; ++i)
                {
                    this.BaseArgs[i] = this.BaseArgs[i].ResolveTypes(parser, typeResolver);

                    ResolvedType actualType   = this.BaseArgs[i].ResolvedType;
                    ResolvedType expectedType = baseConstructorArgTypes[i];
                    if (!actualType.CanAssignToA(expectedType))
                    {
                        throw new ParserException(this.BaseArgs[i], "Argument is incorrect type.");
                    }
                    if (actualType == ResolvedType.ANY && expectedType != ResolvedType.OBJECT && expectedType != ResolvedType.ANY)
                    {
                        this.BaseArgs[i] = new Cast(this.BaseArgs[i].FirstToken, expectedType, this.BaseArgs[i], this, false);
                    }
                }
            }

            foreach (Executable line in this.Code)
            {
                line.ResolveTypes(parser, typeResolver);
            }
        }