Пример #1
0
        public override AstNode Visit(NewExpression node)
        {
            // Begin the node.
            builder.BeginNode(node);

            // Get the functional expression.
            Method constructor = node.GetConstructor();

            // Create the object.
            IChelaType objectType = node.GetObjectType();
            if(constructor == null)
            {
                if(objectType.IsStructure())
                    builder.CreateNewStruct(objectType, null, 0);
                else
                    builder.CreateNewObject(node.GetObjectType(), null, 0);
                return builder.EndNode();
            }

            // Get the function type.
            FunctionType functionType = (FunctionType)constructor.GetFunctionType();

            // Push the arguments.
            AstNode argExpr = node.GetArguments();
            int index = 1;
            while(argExpr != null)
            {
                // Visit the argument.
                argExpr.Accept(this);

                // Coerce it.
                IChelaType argExprType = argExpr.GetNodeType();
                IChelaType argType = functionType.GetArgument(index++);
                if(argType != argExprType)
                    Cast(node, argExpr.GetNodeValue(), argExprType, argType);

                // Read the next argument.
                argExpr = argExpr.GetNext();
            }

            // Store the number of arguments.
            int numargs = index-1;

            // Create the object.
            if(objectType.IsStructure())
                builder.CreateNewStruct(objectType, constructor, (uint)numargs);
            else
                builder.CreateNewObject(objectType, constructor, (uint)numargs);

            return builder.EndNode();
        }