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

            // Get the constant expression.
            Expression constant = node.GetLabel();

            // Expand it.
            if(constant != null)
            {
                // Get the constant value.
                IChelaType coercionType = node.GetCoercionType();
                ConstantValue constantValue = (ConstantValue)constant.GetNodeValue();
                constantValue = constantValue.Cast(coercionType);

                // Make sure the case is in the current switch.
                IDictionary<ConstantValue, CaseLabel> caseDict = node.GetSwitch().CaseDictionary;
                CaseLabel targetLabel;
                if(!caseDict.TryGetValue(constantValue, out targetLabel))
                    Error(node, "current switch doesn't contain a case for [{1}]{0}.", constantValue.ToString(), constantValue.GetHashCode());
                node.SetTargetLabel(targetLabel);
            }

            // Jump to the target case.
            CaseLabel targetCase = node.GetTargetLabel();
            builder.CreateJmp(targetCase.GetBlock());

            // End the node.
            return builder.EndNode();
        }
Ejemplo n.º 2
0
        public override AstNode Visit(GotoCaseStatement node)
        {
            // Store the current switch in the node.
            node.SetSwitch(currentSwitch);

            // Get the constant expression.
            Expression constant = node.GetLabel();

            // Expand it.
            CaseLabel targetLabel;
            if(constant != null)
            {
                constant = (Expression)constant.Accept(this);
                node.SetLabel(constant);

                // Get the constant value.
                IChelaType coercionType = currentSwitch.GetCoercionType();
                node.SetCoercionType(coercionType);
            }
            else
            {
                // Make sure the current switch contains a default case.
                targetLabel = currentSwitch.GetDefaultCase();
                if(targetLabel == null)
                    Error(node, "current switch doesn't contain a default case.");
                node.SetTargetLabel(targetLabel);
            }

            return node;
        }
Ejemplo n.º 3
0
        public override AstNode Visit(GotoCaseStatement node)
        {
            Expression labelConstant = node.GetLabel();

            // This statement only can be used inside of a switch.
            if(currentSwitch == null)
                Error(node, "goto {0} only can be used inside of a switch.",
                        labelConstant == null ? "default" : "case");

            // Coerce the label constant.
            if(labelConstant != null)
            {
                // Visit the label expression.
                labelConstant.Accept(this);

                // Coerce the label expression.
                IChelaType labelType = labelConstant.GetNodeType();
                IChelaType coercionType = currentSwitch.GetCoercionType();
                if(Coerce(node, coercionType, labelType, labelConstant.GetNodeValue()) != coercionType)
                    Error(node, "cannot implicitly cast a label of type {0} into {1}",
                           labelType.GetDisplayName(), coercionType.GetDisplayName());
                // Not all of the cases have been registered yet, so perform the
                // existence checks in generation pass.
            }

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

            return node;
        }