示例#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();
        }
示例#2
0
        public override AstNode Visit(NewExpression node)
        {
            // Visit the type expression.
            Expression typeExpression = node.GetTypeExpression();
            typeExpression.SetHints(Expression.TypeHint);
            typeExpression.Accept(this);

            // Visit the arguments.
            VisitList(node.GetArguments());

            // Get the type.
            IChelaType objectType = typeExpression.GetNodeType();
            objectType = ExtractActualType(typeExpression, objectType);
            node.SetObjectType(objectType);

            // Cannot create abstract objects.
            if(objectType.IsAbstract())
                Error(node, "cannot create abstract objects.");

            // Cannot create reference objects.
            if(objectType.IsReference())
                Error(node, "cannot create references, only object/structures.");

            // Create the different objects.
            Structure building = null;
            if(objectType.IsPrimitive() || objectType.IsPointer())
            {
                Error(node, "cannot create primitives with new");
            }
            else if(objectType.IsStructure())
            {
                building = (Structure)objectType;
            }
            else if(objectType.IsClass())
            {
                building = (Structure)objectType;
            }
            else if(objectType.IsInterface())
            {
                Error(node, "cannot instance interfaces.");
            }
            else
                Error(node, "cannot create object of type {0} with new.", objectType);

            // Use the implicit instance.
            if(building != null)
            {
                if(building.IsGeneric() && building.IsGenericImplicit())
                    building = building.GetSelfInstance();

                // Set the node type.
                if(building.IsPassedByReference())
                    node.SetNodeType(ReferenceType.Create(building));
                else
                    node.SetNodeType(building);
            }

            // Get the constructor group.
            FunctionGroup constructorGroup = building.GetConstructor();
            if(constructorGroup == null)
                Error(node, "default constructors unimplemented.");

            // Pick the constructor.
            Method constructor = (Method)PickFunction(node, constructorGroup, false, null, node.GetArguments(), false);
            node.SetConstructor(constructor);

            return node;
        }