Example #1
0
        public override AstNode Visit(NewRawExpression node)
        {
            // Begin the node.
            builder.BeginNode(node);

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

            // Emit a create new object for primitives.
            if(constructor == null)
            {
                builder.CreateNewRawObject(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(node.IsHeapAlloc())
                builder.CreateNewRawObject(node.GetObjectType(), constructor, (uint)numargs);
            else
                builder.CreateNewStackObject(node.GetObjectType(), constructor, (uint)numargs);

            return builder.EndNode();
        }
Example #2
0
        public override AstNode Visit(NewRawExpression node)
        {
            // Don't use under safe contexts.
            UnsafeError(node, "cannot use array {0} under safe contexts.",
                    node.IsHeapAlloc() ? "heapalloc" : "stackalloc");

            // 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 primitive/pointer/object.");

            // Create the different objects.
            Structure building = null;
            if(objectType.IsPrimitive() || objectType.IsPointer())
            {
                if(node.GetArguments() != null)
                    Error(node, "primitives/pointers hasn't constructors.");

                // Set the node type and return.
                node.SetNodeType(PointerType.Create(objectType));
                return node;
            }
            else if(objectType.IsStructure())
            {
                building = (Structure)objectType;
                node.SetNodeType(PointerType.Create(objectType));
            }
            else if(objectType.IsClass())
            {
                building = (Structure)objectType;
                node.SetNodeType(ReferenceType.Create(objectType));
            }

            // 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;
        }