コード例 #1
0
    /**
     * @see parser.IDLParserVisitor#visit(ASTcasex, Object)
     */
    public Object visit(ASTcasex node, Object data) {
        if (!(data is UnionBuildInfo)) {
            throw new InternalCompilerException("invalid parameter in visit ASTswitch_body");
        }
        UnionBuildInfo buildInfo = (UnionBuildInfo)data;
        // REFACTORING possiblity: replace direct use of values by using the Literals
        // case node consists of one or more case-labels followed by an element spec
        // collect the data for this switch-case
        object[] discriminatorValues = CollectDiscriminatorValuesForCase(node, 
                                                                         buildInfo.GetGenerationHelper().DiscriminatorType, 
                                                                         buildInfo);
        
        ASTelement_spec elemSpec = (ASTelement_spec)node.jjtGetChild(node.jjtGetNumChildren() - 1);
        ASTtype_spec typeSpecNode = (ASTtype_spec)elemSpec.jjtGetChild(0);
        TypeContainer elemType = (TypeContainer)typeSpecNode.jjtAccept(this, buildInfo);
        if (elemType == null) {
            throw new InvalidIdlException(
                String.Format("union elem type not defined for {0}",
                              node.GetIdentification()));
        }
        elemType = ReplaceByCustomMappedIfNeeded(elemType);
        Node elemDecl = elemSpec.jjtGetChild(1);
        string elemDeclIdent = 
                DetermineTypeAndNameForDeclarator((ASTdeclarator)elemDecl, data,
                                                  ref elemType);
        // generate the methods/field for this switch-case
        buildInfo.GetGenerationHelper().GenerateSwitchCase(elemType, elemDeclIdent, discriminatorValues);

        return null;
    }
コード例 #2
0
 /// <summary>helper methods to collect discriminator values for casex node; checks if const-type is ok</summary>
 private object[] CollectDiscriminatorValuesForCase(ASTcasex node, TypeContainer discrType,
                                                    BuildInfo unionInfo) {
     object[] result = new object[node.jjtGetNumChildren() - 1];
     for (int i = 0; i < node.jjtGetNumChildren() - 1; i++) {
         if (!((ASTcase_label)node.jjtGetChild(i)).isDefault()) {
             Literal litVal = ((Literal)node.jjtGetChild(i).jjtAccept(this, unionInfo));
             if (litVal == null) {
                 throw new InvalidIdlException(
                     String.Format("invalid {0}, discrimitator value for case not retrievable",
                                   node.GetIdentification()));
             }                
             // check if val ok ...
             CheckDiscrValAssignableToDiscrType(litVal, discrType);
             result[i] = litVal.GetValue();
         } else {
             // default case
             result[i] = UnionGenerationHelper.DefaultCaseDiscriminator;
         }
     }
     return result;
 }