Exemplo n.º 1
0
        public override AstNode Visit(ThrowStatement node)
        {
            // Begin the node.
            builder.BeginNode(node);

            // Get the exception.
            Expression exception = node.GetException();
            exception.Accept(this);

            // Cast the exception.
            IChelaType exceptionType = exception.GetNodeType();
            IChelaType coercionType = node.GetCoercionType();
            if(exceptionType != coercionType)
                Cast(node, exception.GetNodeValue(), exceptionType, coercionType);

            // Throw the exception.
            builder.CreateThrow();

            return builder.EndNode();
        }
Exemplo n.º 2
0
        public override AstNode Visit(ThrowStatement node)
        {
            // Get the exception.
            Expression exception = node.GetException();
            exception.Accept(this);

            // Get the exception type.
            IChelaType exceptionType = exception.GetNodeType();
            ReferenceType refType;

            // Dereference the exception.
            if(exceptionType.IsReference())
            {
                refType = (ReferenceType)exceptionType;
                IChelaType referencedType = refType.GetReferencedType();
                if(referencedType.IsReference())
                    exceptionType = referencedType;
            }

            // The expression must be an object reference.
            if(!exceptionType.IsReference())
                Error(node, "cannot throw something that isn't a reference.");

            // Get the object type.
            refType = (ReferenceType)exceptionType;
            IChelaType objectType = refType.GetReferencedType();
            if(!objectType.IsClass())
                Error(node, "cannot throw something that isn't a class instance.");

            // Set the coercion type.
            node.SetCoercionType(exceptionType);

            // Remove dead code.
            AstNode dead = node.GetNext();
            if(dead != null)
            {
                Warning(dead, "detected unreachable code.");
                VisitList(dead);
                node.SetNext(null);
            }

            return node;
        }