corba/src/share/classes/com/sun/tools/corba/se/idl/toJavaPortable/UnionGen.java
author ohair
Tue, 25 May 2010 15:52:11 -0700
changeset 5555 b2b5ed3f0d0d
parent 4 02bb8761fcce
child 17218 0007d4b16b10
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     1
/*
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     2
 * Copyright (c) 1999, 2004, Oracle and/or its affiliates. All rights reserved.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02bb8761fcce Initial load
duke
parents:
diff changeset
     4
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02bb8761fcce Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    10
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02bb8761fcce Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02bb8761fcce Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02bb8761fcce Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02bb8761fcce Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
02bb8761fcce Initial load
duke
parents:
diff changeset
    16
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
02bb8761fcce Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02bb8761fcce Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02bb8761fcce Initial load
duke
parents:
diff changeset
    20
 *
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    23
 * questions.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    24
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    25
/*
02bb8761fcce Initial load
duke
parents:
diff changeset
    26
 * COMPONENT_NAME: idl.toJava
02bb8761fcce Initial load
duke
parents:
diff changeset
    27
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    28
 * ORIGINS: 27
02bb8761fcce Initial load
duke
parents:
diff changeset
    29
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    30
 * Licensed Materials - Property of IBM
02bb8761fcce Initial load
duke
parents:
diff changeset
    31
 * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
02bb8761fcce Initial load
duke
parents:
diff changeset
    32
 * RMI-IIOP v1.0
02bb8761fcce Initial load
duke
parents:
diff changeset
    33
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    34
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    35
02bb8761fcce Initial load
duke
parents:
diff changeset
    36
package com.sun.tools.corba.se.idl.toJavaPortable;
02bb8761fcce Initial load
duke
parents:
diff changeset
    37
02bb8761fcce Initial load
duke
parents:
diff changeset
    38
// NOTES:
02bb8761fcce Initial load
duke
parents:
diff changeset
    39
// -cast() does not support longlong types yet.
02bb8761fcce Initial load
duke
parents:
diff changeset
    40
// -Deal with typedef changes.
02bb8761fcce Initial load
duke
parents:
diff changeset
    41
// -Scoped names for the discriminator are ignored at the moment.
02bb8761fcce Initial load
duke
parents:
diff changeset
    42
// -F46082.51<daz> Remove -stateful feature; javaStatefulName() obsolete.
02bb8761fcce Initial load
duke
parents:
diff changeset
    43
// -D61056   <klr> Use Util.helperName
02bb8761fcce Initial load
duke
parents:
diff changeset
    44
02bb8761fcce Initial load
duke
parents:
diff changeset
    45
import java.io.File;
02bb8761fcce Initial load
duke
parents:
diff changeset
    46
import java.io.IOException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    47
import java.io.PrintWriter;
02bb8761fcce Initial load
duke
parents:
diff changeset
    48
import java.util.Enumeration;
02bb8761fcce Initial load
duke
parents:
diff changeset
    49
import java.util.Hashtable;
02bb8761fcce Initial load
duke
parents:
diff changeset
    50
import java.util.Vector;
02bb8761fcce Initial load
duke
parents:
diff changeset
    51
02bb8761fcce Initial load
duke
parents:
diff changeset
    52
import com.sun.tools.corba.se.idl.GenFileStream;
02bb8761fcce Initial load
duke
parents:
diff changeset
    53
import com.sun.tools.corba.se.idl.ConstEntry;
02bb8761fcce Initial load
duke
parents:
diff changeset
    54
import com.sun.tools.corba.se.idl.EnumEntry;
02bb8761fcce Initial load
duke
parents:
diff changeset
    55
import com.sun.tools.corba.se.idl.InterfaceEntry;
02bb8761fcce Initial load
duke
parents:
diff changeset
    56
import com.sun.tools.corba.se.idl.PrimitiveEntry;
02bb8761fcce Initial load
duke
parents:
diff changeset
    57
import com.sun.tools.corba.se.idl.SequenceEntry;
02bb8761fcce Initial load
duke
parents:
diff changeset
    58
import com.sun.tools.corba.se.idl.StringEntry;
02bb8761fcce Initial load
duke
parents:
diff changeset
    59
import com.sun.tools.corba.se.idl.SymtabEntry;
02bb8761fcce Initial load
duke
parents:
diff changeset
    60
import com.sun.tools.corba.se.idl.TypedefEntry;
02bb8761fcce Initial load
duke
parents:
diff changeset
    61
import com.sun.tools.corba.se.idl.UnionBranch;
02bb8761fcce Initial load
duke
parents:
diff changeset
    62
import com.sun.tools.corba.se.idl.UnionEntry;
02bb8761fcce Initial load
duke
parents:
diff changeset
    63
02bb8761fcce Initial load
duke
parents:
diff changeset
    64
import com.sun.tools.corba.se.idl.constExpr.Expression;
02bb8761fcce Initial load
duke
parents:
diff changeset
    65
import com.sun.tools.corba.se.idl.constExpr.EvaluationException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    66
02bb8761fcce Initial load
duke
parents:
diff changeset
    67
/**
02bb8761fcce Initial load
duke
parents:
diff changeset
    68
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    69
 **/
02bb8761fcce Initial load
duke
parents:
diff changeset
    70
public class UnionGen implements com.sun.tools.corba.se.idl.UnionGen, JavaGenerator
02bb8761fcce Initial load
duke
parents:
diff changeset
    71
{
02bb8761fcce Initial load
duke
parents:
diff changeset
    72
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    73
   * Public zero-argument constructor.
02bb8761fcce Initial load
duke
parents:
diff changeset
    74
   **/
02bb8761fcce Initial load
duke
parents:
diff changeset
    75
  public UnionGen ()
02bb8761fcce Initial load
duke
parents:
diff changeset
    76
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
    77
  } // ctor
02bb8761fcce Initial load
duke
parents:
diff changeset
    78
02bb8761fcce Initial load
duke
parents:
diff changeset
    79
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    80
   *
02bb8761fcce Initial load
duke
parents:
diff changeset
    81
   **/
02bb8761fcce Initial load
duke
parents:
diff changeset
    82
  public void generate (Hashtable symbolTable, UnionEntry u, PrintWriter s)
02bb8761fcce Initial load
duke
parents:
diff changeset
    83
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
    84
    this.symbolTable = symbolTable;
02bb8761fcce Initial load
duke
parents:
diff changeset
    85
    this.u           = u;
02bb8761fcce Initial load
duke
parents:
diff changeset
    86
    init ();
02bb8761fcce Initial load
duke
parents:
diff changeset
    87
02bb8761fcce Initial load
duke
parents:
diff changeset
    88
    openStream ();
02bb8761fcce Initial load
duke
parents:
diff changeset
    89
    if (stream == null)
02bb8761fcce Initial load
duke
parents:
diff changeset
    90
      return;
02bb8761fcce Initial load
duke
parents:
diff changeset
    91
    generateHelper ();
02bb8761fcce Initial load
duke
parents:
diff changeset
    92
    generateHolder ();
02bb8761fcce Initial load
duke
parents:
diff changeset
    93
    writeHeading ();
02bb8761fcce Initial load
duke
parents:
diff changeset
    94
    writeBody ();
02bb8761fcce Initial load
duke
parents:
diff changeset
    95
    writeClosing ();
02bb8761fcce Initial load
duke
parents:
diff changeset
    96
    closeStream ();
02bb8761fcce Initial load
duke
parents:
diff changeset
    97
    generateContainedTypes ();
02bb8761fcce Initial load
duke
parents:
diff changeset
    98
  } // generate
02bb8761fcce Initial load
duke
parents:
diff changeset
    99
02bb8761fcce Initial load
duke
parents:
diff changeset
   100
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   101
   * Initialize members unique to this generator.
02bb8761fcce Initial load
duke
parents:
diff changeset
   102
   **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   103
  protected void init ()
02bb8761fcce Initial load
duke
parents:
diff changeset
   104
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
   105
    utype       = Util.typeOf (u.type ());
02bb8761fcce Initial load
duke
parents:
diff changeset
   106
    unionIsEnum = utype instanceof EnumEntry;
02bb8761fcce Initial load
duke
parents:
diff changeset
   107
  } // init
02bb8761fcce Initial load
duke
parents:
diff changeset
   108
02bb8761fcce Initial load
duke
parents:
diff changeset
   109
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   110
   *
02bb8761fcce Initial load
duke
parents:
diff changeset
   111
   **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   112
  protected void openStream ()
02bb8761fcce Initial load
duke
parents:
diff changeset
   113
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
   114
    stream = Util.stream (u, ".java");
02bb8761fcce Initial load
duke
parents:
diff changeset
   115
  } // openStream
02bb8761fcce Initial load
duke
parents:
diff changeset
   116
02bb8761fcce Initial load
duke
parents:
diff changeset
   117
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   118
   *
02bb8761fcce Initial load
duke
parents:
diff changeset
   119
   **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   120
  protected void generateHelper ()
02bb8761fcce Initial load
duke
parents:
diff changeset
   121
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
   122
    ((Factories)Compile.compiler.factories ()).helper ().generate (symbolTable, u);
02bb8761fcce Initial load
duke
parents:
diff changeset
   123
  } // generateHelper
02bb8761fcce Initial load
duke
parents:
diff changeset
   124
02bb8761fcce Initial load
duke
parents:
diff changeset
   125
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   126
   *
02bb8761fcce Initial load
duke
parents:
diff changeset
   127
   **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   128
  protected void generateHolder ()
02bb8761fcce Initial load
duke
parents:
diff changeset
   129
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
   130
   ((Factories)Compile.compiler.factories ()).holder ().generate (symbolTable, u);
02bb8761fcce Initial load
duke
parents:
diff changeset
   131
  } // generateHolder
02bb8761fcce Initial load
duke
parents:
diff changeset
   132
02bb8761fcce Initial load
duke
parents:
diff changeset
   133
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   134
   *
02bb8761fcce Initial load
duke
parents:
diff changeset
   135
   **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   136
  protected void writeHeading ()
02bb8761fcce Initial load
duke
parents:
diff changeset
   137
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
   138
    // If the discriminator is an enum, assign the typePackage string.
02bb8761fcce Initial load
duke
parents:
diff changeset
   139
    if (unionIsEnum)
02bb8761fcce Initial load
duke
parents:
diff changeset
   140
      typePackage = Util.javaQualifiedName (utype) + '.';
02bb8761fcce Initial load
duke
parents:
diff changeset
   141
    else
02bb8761fcce Initial load
duke
parents:
diff changeset
   142
      typePackage = "";
02bb8761fcce Initial load
duke
parents:
diff changeset
   143
02bb8761fcce Initial load
duke
parents:
diff changeset
   144
    Util.writePackage (stream, u);
02bb8761fcce Initial load
duke
parents:
diff changeset
   145
    Util.writeProlog (stream, ((GenFileStream)stream).name ());
02bb8761fcce Initial load
duke
parents:
diff changeset
   146
02bb8761fcce Initial load
duke
parents:
diff changeset
   147
    String className = u.name ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   148
    stream.println ("public final class " + u.name () + " implements org.omg.CORBA.portable.IDLEntity");
02bb8761fcce Initial load
duke
parents:
diff changeset
   149
    stream.println ("{");
02bb8761fcce Initial load
duke
parents:
diff changeset
   150
  } // writeHeading
02bb8761fcce Initial load
duke
parents:
diff changeset
   151
02bb8761fcce Initial load
duke
parents:
diff changeset
   152
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   153
   *
02bb8761fcce Initial load
duke
parents:
diff changeset
   154
   **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   155
  protected void writeBody ()
02bb8761fcce Initial load
duke
parents:
diff changeset
   156
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
   157
    // Write branches and populate quality arrays
02bb8761fcce Initial load
duke
parents:
diff changeset
   158
    int size = u.branches ().size () + 1;
02bb8761fcce Initial load
duke
parents:
diff changeset
   159
    Enumeration e = u.branches ().elements ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   160
    int i = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   161
    while (e.hasMoreElements ())
02bb8761fcce Initial load
duke
parents:
diff changeset
   162
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   163
      UnionBranch branch = (UnionBranch)e.nextElement ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   164
      Util.fillInfo (branch.typedef);
02bb8761fcce Initial load
duke
parents:
diff changeset
   165
      // <f46082.51> Remove -stateful feature; javaStatefulName() obsolete.
02bb8761fcce Initial load
duke
parents:
diff changeset
   166
      //stream.println ("  private " + Util.javaStatefulName (branch.typedef) + " ___" + branch.typedef.name () + ";");
02bb8761fcce Initial load
duke
parents:
diff changeset
   167
      stream.println ("  private " + Util.javaName (branch.typedef) + " ___" + branch.typedef.name () + ";");
02bb8761fcce Initial load
duke
parents:
diff changeset
   168
      ++i;
02bb8761fcce Initial load
duke
parents:
diff changeset
   169
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   170
    stream.println ("  private " + Util.javaName (utype) + " __discriminator;");
02bb8761fcce Initial load
duke
parents:
diff changeset
   171
    stream.println ("  private boolean __uninitialized = true;");
02bb8761fcce Initial load
duke
parents:
diff changeset
   172
02bb8761fcce Initial load
duke
parents:
diff changeset
   173
    // Write ctor
02bb8761fcce Initial load
duke
parents:
diff changeset
   174
    stream.println ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   175
    stream.println ("  public " + u.name () + " ()");
02bb8761fcce Initial load
duke
parents:
diff changeset
   176
    stream.println ("  {");
02bb8761fcce Initial load
duke
parents:
diff changeset
   177
    stream.println ("  }");
02bb8761fcce Initial load
duke
parents:
diff changeset
   178
02bb8761fcce Initial load
duke
parents:
diff changeset
   179
    // Write discriminator
02bb8761fcce Initial load
duke
parents:
diff changeset
   180
    stream.println ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   181
    stream.println ("  public " + Util.javaName (utype) + " " + safeName (u, "discriminator") + " ()");
02bb8761fcce Initial load
duke
parents:
diff changeset
   182
    stream.println ("  {");
02bb8761fcce Initial load
duke
parents:
diff changeset
   183
    stream.println ("    if (__uninitialized)");
02bb8761fcce Initial load
duke
parents:
diff changeset
   184
    stream.println ("      throw new org.omg.CORBA.BAD_OPERATION ();");
02bb8761fcce Initial load
duke
parents:
diff changeset
   185
    stream.println ("    return __discriminator;");
02bb8761fcce Initial load
duke
parents:
diff changeset
   186
    stream.println ("  }");
02bb8761fcce Initial load
duke
parents:
diff changeset
   187
02bb8761fcce Initial load
duke
parents:
diff changeset
   188
    // Write for each branch:
02bb8761fcce Initial load
duke
parents:
diff changeset
   189
    // - setter
02bb8761fcce Initial load
duke
parents:
diff changeset
   190
    // - getter
02bb8761fcce Initial load
duke
parents:
diff changeset
   191
    // - private verifyXXX
02bb8761fcce Initial load
duke
parents:
diff changeset
   192
    e = u.branches ().elements ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   193
    i = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   194
    while (e.hasMoreElements ())
02bb8761fcce Initial load
duke
parents:
diff changeset
   195
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   196
      UnionBranch branch = (UnionBranch)e.nextElement ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   197
      writeBranchMethods (stream, u, branch, i++);
02bb8761fcce Initial load
duke
parents:
diff changeset
   198
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   199
    if (u.defaultBranch () == null && !coversAll (u))
02bb8761fcce Initial load
duke
parents:
diff changeset
   200
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   201
      stream.println ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   202
      stream.println ("  public void _default ()");
02bb8761fcce Initial load
duke
parents:
diff changeset
   203
      stream.println ("  {");
02bb8761fcce Initial load
duke
parents:
diff changeset
   204
      stream.println ("    __discriminator = " + defaultDiscriminator (u) + ';');
02bb8761fcce Initial load
duke
parents:
diff changeset
   205
      stream.println ("    __uninitialized = false;");
02bb8761fcce Initial load
duke
parents:
diff changeset
   206
      stream.println ("  }");
02bb8761fcce Initial load
duke
parents:
diff changeset
   207
02bb8761fcce Initial load
duke
parents:
diff changeset
   208
      stream.println ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   209
      stream.println ("  public void _default (" + Util.javaName(utype) +
02bb8761fcce Initial load
duke
parents:
diff changeset
   210
        " discriminator)");
02bb8761fcce Initial load
duke
parents:
diff changeset
   211
      stream.println ("  {");
02bb8761fcce Initial load
duke
parents:
diff changeset
   212
      stream.println ("    verifyDefault( discriminator ) ;" );
02bb8761fcce Initial load
duke
parents:
diff changeset
   213
      stream.println ("    __discriminator = discriminator ;");
02bb8761fcce Initial load
duke
parents:
diff changeset
   214
      stream.println ("    __uninitialized = false;");
02bb8761fcce Initial load
duke
parents:
diff changeset
   215
      stream.println ("  }");
02bb8761fcce Initial load
duke
parents:
diff changeset
   216
02bb8761fcce Initial load
duke
parents:
diff changeset
   217
      writeVerifyDefault() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   218
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   219
    stream.println ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   220
  } // writeBody
02bb8761fcce Initial load
duke
parents:
diff changeset
   221
02bb8761fcce Initial load
duke
parents:
diff changeset
   222
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   223
   *
02bb8761fcce Initial load
duke
parents:
diff changeset
   224
   **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   225
  protected void writeClosing ()
02bb8761fcce Initial load
duke
parents:
diff changeset
   226
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
   227
    stream.println ("} // class " + u.name ());
02bb8761fcce Initial load
duke
parents:
diff changeset
   228
  } // writeClosing
02bb8761fcce Initial load
duke
parents:
diff changeset
   229
02bb8761fcce Initial load
duke
parents:
diff changeset
   230
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   231
   *
02bb8761fcce Initial load
duke
parents:
diff changeset
   232
   **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   233
  protected void closeStream ()
02bb8761fcce Initial load
duke
parents:
diff changeset
   234
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
   235
    stream.close ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   236
  } // closeStream
02bb8761fcce Initial load
duke
parents:
diff changeset
   237
02bb8761fcce Initial load
duke
parents:
diff changeset
   238
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   239
   *
02bb8761fcce Initial load
duke
parents:
diff changeset
   240
   **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   241
  protected void generateContainedTypes ()
02bb8761fcce Initial load
duke
parents:
diff changeset
   242
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
   243
    Enumeration e = u.contained ().elements ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   244
    while (e.hasMoreElements ())
02bb8761fcce Initial load
duke
parents:
diff changeset
   245
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   246
      SymtabEntry entry = (SymtabEntry)e.nextElement ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   247
02bb8761fcce Initial load
duke
parents:
diff changeset
   248
      // Don't generate contained entries if they are sequences.
02bb8761fcce Initial load
duke
parents:
diff changeset
   249
      // Sequences are unnamed and since they translate to arrays,
02bb8761fcce Initial load
duke
parents:
diff changeset
   250
      // no classes are generated for them, not even holders in this
02bb8761fcce Initial load
duke
parents:
diff changeset
   251
      // case since they cannot be accessed outside of this union.
02bb8761fcce Initial load
duke
parents:
diff changeset
   252
      if (!(entry instanceof SequenceEntry))
02bb8761fcce Initial load
duke
parents:
diff changeset
   253
        entry.generate (symbolTable, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   254
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   255
  } // generateContainedTypes
02bb8761fcce Initial load
duke
parents:
diff changeset
   256
02bb8761fcce Initial load
duke
parents:
diff changeset
   257
  private void writeVerifyDefault()
02bb8761fcce Initial load
duke
parents:
diff changeset
   258
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
   259
    Vector labels = vectorizeLabels (u.branches (), true);
02bb8761fcce Initial load
duke
parents:
diff changeset
   260
02bb8761fcce Initial load
duke
parents:
diff changeset
   261
    stream.println( "" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   262
    stream.println( "  private void verifyDefault( " + Util.javaName(utype) +
02bb8761fcce Initial load
duke
parents:
diff changeset
   263
        " value )" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   264
    stream.println( "  {" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   265
02bb8761fcce Initial load
duke
parents:
diff changeset
   266
    if (unionIsEnum)
02bb8761fcce Initial load
duke
parents:
diff changeset
   267
        stream.println( "    switch (value.value()) {" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   268
    else
02bb8761fcce Initial load
duke
parents:
diff changeset
   269
        stream.println( "    switch (value) {" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   270
02bb8761fcce Initial load
duke
parents:
diff changeset
   271
    Enumeration e = labels.elements() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   272
    while (e.hasMoreElements()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   273
        String str = (String)(e.nextElement()) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   274
        stream.println( "      case " + str + ":" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   275
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   276
02bb8761fcce Initial load
duke
parents:
diff changeset
   277
    stream.println( "        throw new org.omg.CORBA.BAD_OPERATION() ;" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   278
    stream.println( "" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   279
    stream.println( "      default:" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   280
    stream.println( "        return;" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   281
    stream.println( "    }" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   282
    stream.println( "  }" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   283
  }
02bb8761fcce Initial load
duke
parents:
diff changeset
   284
02bb8761fcce Initial load
duke
parents:
diff changeset
   285
  private String defaultDiscriminator (UnionEntry u)
02bb8761fcce Initial load
duke
parents:
diff changeset
   286
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
   287
    Vector labels = vectorizeLabels (u.branches (), false );
02bb8761fcce Initial load
duke
parents:
diff changeset
   288
    String ret = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   289
    SymtabEntry utype = Util.typeOf (u.type ());
02bb8761fcce Initial load
duke
parents:
diff changeset
   290
    if (utype instanceof PrimitiveEntry  && utype.name ().equals ("boolean")) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   291
        // If it got this far, then:
02bb8761fcce Initial load
duke
parents:
diff changeset
   292
        // - there is only one branch;
02bb8761fcce Initial load
duke
parents:
diff changeset
   293
        // - that branch has only one label.
02bb8761fcce Initial load
duke
parents:
diff changeset
   294
        if (labels.contains ("true"))
02bb8761fcce Initial load
duke
parents:
diff changeset
   295
            ret = "false";
02bb8761fcce Initial load
duke
parents:
diff changeset
   296
        else
02bb8761fcce Initial load
duke
parents:
diff changeset
   297
            ret = "true";
02bb8761fcce Initial load
duke
parents:
diff changeset
   298
    } else if (utype.name ().equals ("char")) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   299
        // This doesn't handle '\u0030' == '0'.  Unions are so
02bb8761fcce Initial load
duke
parents:
diff changeset
   300
        // seldom used.  I don't have time to make this perfect.
02bb8761fcce Initial load
duke
parents:
diff changeset
   301
        int def = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   302
        String string = "'\\u0000'";
02bb8761fcce Initial load
duke
parents:
diff changeset
   303
        while (def != 0xFFFF && labels.contains (string))
02bb8761fcce Initial load
duke
parents:
diff changeset
   304
            if (++def / 0x10 == 0)
02bb8761fcce Initial load
duke
parents:
diff changeset
   305
                string = "'\\u000" + def + "'";
02bb8761fcce Initial load
duke
parents:
diff changeset
   306
            else if (def / 0x100 == 0)
02bb8761fcce Initial load
duke
parents:
diff changeset
   307
                string = "\\u00" + def + "'";
02bb8761fcce Initial load
duke
parents:
diff changeset
   308
            else if (def / 0x1000 == 0)
02bb8761fcce Initial load
duke
parents:
diff changeset
   309
                string = "\\u0" + def + "'";
02bb8761fcce Initial load
duke
parents:
diff changeset
   310
            else
02bb8761fcce Initial load
duke
parents:
diff changeset
   311
                string = "\\u" + def + "'";
02bb8761fcce Initial load
duke
parents:
diff changeset
   312
        ret = string;
02bb8761fcce Initial load
duke
parents:
diff changeset
   313
    } else if (utype instanceof EnumEntry) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   314
        Enumeration e = labels.elements ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   315
        EnumEntry enumEntry = (EnumEntry)utype;
02bb8761fcce Initial load
duke
parents:
diff changeset
   316
        Vector enumList = (Vector)enumEntry.elements ().clone ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   317
        // cull out those elements in the enumeration list that are
02bb8761fcce Initial load
duke
parents:
diff changeset
   318
        // in the cases of this union
02bb8761fcce Initial load
duke
parents:
diff changeset
   319
        while (e.hasMoreElements ())
02bb8761fcce Initial load
duke
parents:
diff changeset
   320
            enumList.removeElement (e.nextElement ());
02bb8761fcce Initial load
duke
parents:
diff changeset
   321
        // If all of the enum elements are covered in this union and
02bb8761fcce Initial load
duke
parents:
diff changeset
   322
        // there is a default statement, just pick one of the
02bb8761fcce Initial load
duke
parents:
diff changeset
   323
        // elements for the default.  If there are enum elements
02bb8761fcce Initial load
duke
parents:
diff changeset
   324
        // which are NOT covered by the cases, pick one as the
02bb8761fcce Initial load
duke
parents:
diff changeset
   325
        // default.
02bb8761fcce Initial load
duke
parents:
diff changeset
   326
        if (enumList.size () == 0)
02bb8761fcce Initial load
duke
parents:
diff changeset
   327
            ret = typePackage + (String)enumEntry.elements ().lastElement ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   328
        else
02bb8761fcce Initial load
duke
parents:
diff changeset
   329
            ret = typePackage + (String)enumList.firstElement ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   330
    } else if (utype.name ().equals ("octet")) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   331
        short def = Byte.MIN_VALUE;
02bb8761fcce Initial load
duke
parents:
diff changeset
   332
        while (def != Byte.MAX_VALUE && labels.contains (Integer.toString (def)))
02bb8761fcce Initial load
duke
parents:
diff changeset
   333
            ++def;
02bb8761fcce Initial load
duke
parents:
diff changeset
   334
        ret = Integer.toString (def);
02bb8761fcce Initial load
duke
parents:
diff changeset
   335
    } else if (utype.name ().equals ("short")) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   336
        short def = Short.MIN_VALUE;
02bb8761fcce Initial load
duke
parents:
diff changeset
   337
        while (def != Short.MAX_VALUE && labels.contains (Integer.toString (def)))
02bb8761fcce Initial load
duke
parents:
diff changeset
   338
            ++def;
02bb8761fcce Initial load
duke
parents:
diff changeset
   339
        ret = Integer.toString (def);
02bb8761fcce Initial load
duke
parents:
diff changeset
   340
    } else if (utype.name ().equals ("long")) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   341
        int def = Integer.MIN_VALUE;
02bb8761fcce Initial load
duke
parents:
diff changeset
   342
        while (def != Integer.MAX_VALUE && labels.contains (Integer.toString (def)))
02bb8761fcce Initial load
duke
parents:
diff changeset
   343
            ++def;
02bb8761fcce Initial load
duke
parents:
diff changeset
   344
        ret = Integer.toString (def);
02bb8761fcce Initial load
duke
parents:
diff changeset
   345
    } else if (utype.name ().equals ("long long")) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   346
        long def = Long.MIN_VALUE;
02bb8761fcce Initial load
duke
parents:
diff changeset
   347
        while (def != Long.MAX_VALUE && labels.contains (Long.toString (def)))
02bb8761fcce Initial load
duke
parents:
diff changeset
   348
            ++def;
02bb8761fcce Initial load
duke
parents:
diff changeset
   349
        ret = Long.toString (def);
02bb8761fcce Initial load
duke
parents:
diff changeset
   350
    } else if (utype.name ().equals ("unsigned short")) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   351
        short def = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   352
        while (def != Short.MAX_VALUE && labels.contains (Integer.toString (def)))
02bb8761fcce Initial load
duke
parents:
diff changeset
   353
            ++def;
02bb8761fcce Initial load
duke
parents:
diff changeset
   354
        ret = Integer.toString (def);
02bb8761fcce Initial load
duke
parents:
diff changeset
   355
    } else if (utype.name ().equals ("unsigned long")) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   356
        int def = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   357
        while (def != Integer.MAX_VALUE && labels.contains (Integer.toString (def)))
02bb8761fcce Initial load
duke
parents:
diff changeset
   358
            ++def;
02bb8761fcce Initial load
duke
parents:
diff changeset
   359
        ret = Integer.toString (def);
02bb8761fcce Initial load
duke
parents:
diff changeset
   360
    } else if (utype.name ().equals ("unsigned long long")) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   361
        long def = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   362
        while (def != Long.MAX_VALUE && labels.contains (Long.toString (def)))
02bb8761fcce Initial load
duke
parents:
diff changeset
   363
            ++def;
02bb8761fcce Initial load
duke
parents:
diff changeset
   364
        ret = Long.toString (def);
02bb8761fcce Initial load
duke
parents:
diff changeset
   365
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   366
02bb8761fcce Initial load
duke
parents:
diff changeset
   367
    return ret;
02bb8761fcce Initial load
duke
parents:
diff changeset
   368
  } // defaultDiscriminator
02bb8761fcce Initial load
duke
parents:
diff changeset
   369
02bb8761fcce Initial load
duke
parents:
diff changeset
   370
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   371
   *
02bb8761fcce Initial load
duke
parents:
diff changeset
   372
   **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   373
  private Vector vectorizeLabels (Vector branchVector, boolean useIntsForEnums )
02bb8761fcce Initial load
duke
parents:
diff changeset
   374
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
   375
    Vector mergedLabels = new Vector ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   376
    Enumeration branches = branchVector.elements ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   377
    while (branches.hasMoreElements ())
02bb8761fcce Initial load
duke
parents:
diff changeset
   378
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   379
      UnionBranch branch = (UnionBranch)branches.nextElement ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   380
      Enumeration labels = branch.labels.elements ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   381
      while (labels.hasMoreElements ())
02bb8761fcce Initial load
duke
parents:
diff changeset
   382
      {
02bb8761fcce Initial load
duke
parents:
diff changeset
   383
        Expression expr = (Expression)labels.nextElement ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   384
        String str ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   385
02bb8761fcce Initial load
duke
parents:
diff changeset
   386
        if (unionIsEnum)
02bb8761fcce Initial load
duke
parents:
diff changeset
   387
          if (useIntsForEnums)
02bb8761fcce Initial load
duke
parents:
diff changeset
   388
            str = typePackage + "_" + Util.parseExpression( expr ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   389
          else
02bb8761fcce Initial load
duke
parents:
diff changeset
   390
            str = typePackage + Util.parseExpression( expr ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   391
        else
02bb8761fcce Initial load
duke
parents:
diff changeset
   392
          str = Util.parseExpression( expr ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   393
02bb8761fcce Initial load
duke
parents:
diff changeset
   394
        mergedLabels.addElement (str);
02bb8761fcce Initial load
duke
parents:
diff changeset
   395
      }
02bb8761fcce Initial load
duke
parents:
diff changeset
   396
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   397
    return mergedLabels;
02bb8761fcce Initial load
duke
parents:
diff changeset
   398
  } // vectorizeLabels
02bb8761fcce Initial load
duke
parents:
diff changeset
   399
02bb8761fcce Initial load
duke
parents:
diff changeset
   400
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   401
   *
02bb8761fcce Initial load
duke
parents:
diff changeset
   402
   **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   403
  private String safeName (UnionEntry u, String name)
02bb8761fcce Initial load
duke
parents:
diff changeset
   404
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
   405
    Enumeration e = u.branches ().elements ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   406
    while (e.hasMoreElements ())
02bb8761fcce Initial load
duke
parents:
diff changeset
   407
      if (((UnionBranch)e.nextElement ()).typedef.name ().equals (name))
02bb8761fcce Initial load
duke
parents:
diff changeset
   408
      {
02bb8761fcce Initial load
duke
parents:
diff changeset
   409
        name = '_' + name;
02bb8761fcce Initial load
duke
parents:
diff changeset
   410
        break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   411
      }
02bb8761fcce Initial load
duke
parents:
diff changeset
   412
    return name;
02bb8761fcce Initial load
duke
parents:
diff changeset
   413
  } // safeName
02bb8761fcce Initial load
duke
parents:
diff changeset
   414
02bb8761fcce Initial load
duke
parents:
diff changeset
   415
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   416
   *
02bb8761fcce Initial load
duke
parents:
diff changeset
   417
   **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   418
  private boolean coversAll (UnionEntry u)
02bb8761fcce Initial load
duke
parents:
diff changeset
   419
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
   420
    // This assumes that it is not possible to cover types other than
02bb8761fcce Initial load
duke
parents:
diff changeset
   421
    // boolean and enums.  This is not quite correct, but since octet
02bb8761fcce Initial load
duke
parents:
diff changeset
   422
    // is not a valid discriminator type, it's not too bad in practice.
02bb8761fcce Initial load
duke
parents:
diff changeset
   423
    // It may also be possible to cover a char type, but we won't worry
02bb8761fcce Initial load
duke
parents:
diff changeset
   424
    // about that either.
02bb8761fcce Initial load
duke
parents:
diff changeset
   425
    SymtabEntry utype = Util.typeOf (u.type ());
02bb8761fcce Initial load
duke
parents:
diff changeset
   426
02bb8761fcce Initial load
duke
parents:
diff changeset
   427
    boolean coversAll = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   428
    if (utype.name ().equals ("boolean")) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   429
      if (u.branches ().size () == 2)
02bb8761fcce Initial load
duke
parents:
diff changeset
   430
        coversAll = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   431
    } else if (utype instanceof EnumEntry) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   432
      Vector labels = vectorizeLabels (u.branches (), true);
02bb8761fcce Initial load
duke
parents:
diff changeset
   433
      if (labels.size () == ((EnumEntry)utype).elements ().size ())
02bb8761fcce Initial load
duke
parents:
diff changeset
   434
        coversAll = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   435
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   436
02bb8761fcce Initial load
duke
parents:
diff changeset
   437
    return coversAll;
02bb8761fcce Initial load
duke
parents:
diff changeset
   438
  } // coversAll
02bb8761fcce Initial load
duke
parents:
diff changeset
   439
02bb8761fcce Initial load
duke
parents:
diff changeset
   440
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   441
   *
02bb8761fcce Initial load
duke
parents:
diff changeset
   442
   **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   443
  private void writeBranchMethods (PrintWriter stream, UnionEntry u, UnionBranch branch, int i)
02bb8761fcce Initial load
duke
parents:
diff changeset
   444
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
   445
    // Write getter
02bb8761fcce Initial load
duke
parents:
diff changeset
   446
    stream.println ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   447
    // <f46082.51> Remove -stateful feature; javaStatefulName() obsolete.
02bb8761fcce Initial load
duke
parents:
diff changeset
   448
    //stream.println ("  public " + Util.javaStatefulName (branch.typedef) + " " + branch.typedef.name () + " ()");
02bb8761fcce Initial load
duke
parents:
diff changeset
   449
    stream.println ("  public " + Util.javaName (branch.typedef) + " " + branch.typedef.name () + " ()");
02bb8761fcce Initial load
duke
parents:
diff changeset
   450
    stream.println ("  {");
02bb8761fcce Initial load
duke
parents:
diff changeset
   451
    stream.println ("    if (__uninitialized)");
02bb8761fcce Initial load
duke
parents:
diff changeset
   452
    stream.println ("      throw new org.omg.CORBA.BAD_OPERATION ();");
02bb8761fcce Initial load
duke
parents:
diff changeset
   453
    stream.println ("    verify" + branch.typedef.name () + " (__discriminator);");
02bb8761fcce Initial load
duke
parents:
diff changeset
   454
    stream.println ("    return ___" + branch.typedef.name () + ";");
02bb8761fcce Initial load
duke
parents:
diff changeset
   455
    stream.println ("  }");
02bb8761fcce Initial load
duke
parents:
diff changeset
   456
02bb8761fcce Initial load
duke
parents:
diff changeset
   457
    // Write setter(s)
02bb8761fcce Initial load
duke
parents:
diff changeset
   458
    stream.println ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   459
    // <f46082.51> Remove -stateful feature; javaStatefulName() obsolete.
02bb8761fcce Initial load
duke
parents:
diff changeset
   460
    //stream.println ("  public void " + branch.typedef.name () + " (" + Util.javaStatefulName (branch.typedef) + " value)");
02bb8761fcce Initial load
duke
parents:
diff changeset
   461
    stream.println ("  public void " + branch.typedef.name () + " (" + Util.javaName (branch.typedef) + " value)");
02bb8761fcce Initial load
duke
parents:
diff changeset
   462
    stream.println ("  {");
02bb8761fcce Initial load
duke
parents:
diff changeset
   463
    if (branch.labels.size () == 0)
02bb8761fcce Initial load
duke
parents:
diff changeset
   464
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   465
      // This is a default branch
02bb8761fcce Initial load
duke
parents:
diff changeset
   466
      stream.println ("    __discriminator = " + defaultDiscriminator (u) + ";");
02bb8761fcce Initial load
duke
parents:
diff changeset
   467
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   468
    else
02bb8761fcce Initial load
duke
parents:
diff changeset
   469
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   470
      // This is a non-default branch
02bb8761fcce Initial load
duke
parents:
diff changeset
   471
      if (unionIsEnum)
02bb8761fcce Initial load
duke
parents:
diff changeset
   472
        stream.println ("    __discriminator = " + typePackage + Util.parseExpression ((Expression)branch.labels.firstElement ()) + ";");
02bb8761fcce Initial load
duke
parents:
diff changeset
   473
      else
02bb8761fcce Initial load
duke
parents:
diff changeset
   474
        stream.println ("    __discriminator = " + cast ((Expression)branch.labels.firstElement (), u.type ()) + ";");
02bb8761fcce Initial load
duke
parents:
diff changeset
   475
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   476
    stream.println ("    ___" + branch.typedef.name () + " = value;");
02bb8761fcce Initial load
duke
parents:
diff changeset
   477
    stream.println ("    __uninitialized = false;");
02bb8761fcce Initial load
duke
parents:
diff changeset
   478
    stream.println ("  }");
02bb8761fcce Initial load
duke
parents:
diff changeset
   479
02bb8761fcce Initial load
duke
parents:
diff changeset
   480
    SymtabEntry utype = Util.typeOf (u.type ());
02bb8761fcce Initial load
duke
parents:
diff changeset
   481
02bb8761fcce Initial load
duke
parents:
diff changeset
   482
    // If there are multiple labels for one branch, write the
02bb8761fcce Initial load
duke
parents:
diff changeset
   483
    // setter that takes a discriminator.
02bb8761fcce Initial load
duke
parents:
diff changeset
   484
    if (branch.labels.size () > 0 || branch.isDefault)
02bb8761fcce Initial load
duke
parents:
diff changeset
   485
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   486
      stream.println ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   487
      // <f46082.51> Remove -stateful feature; javaStatefulName() obsolete.
02bb8761fcce Initial load
duke
parents:
diff changeset
   488
      //stream.println ("  public void " + branch.typedef.name () + " (" + Util.javaName (utype) + " discriminator, " + Util.javaStatefulName (branch.typedef) + " value)");
02bb8761fcce Initial load
duke
parents:
diff changeset
   489
      stream.println ("  public void " + branch.typedef.name () + " (" + Util.javaName (utype) + " discriminator, " + Util.javaName (branch.typedef) + " value)");
02bb8761fcce Initial load
duke
parents:
diff changeset
   490
      stream.println ("  {");
02bb8761fcce Initial load
duke
parents:
diff changeset
   491
      stream.println ("    verify" + branch.typedef.name () + " (discriminator);");
02bb8761fcce Initial load
duke
parents:
diff changeset
   492
      stream.println ("    __discriminator = discriminator;");
02bb8761fcce Initial load
duke
parents:
diff changeset
   493
      stream.println ("    ___" + branch.typedef.name () + " = value;");
02bb8761fcce Initial load
duke
parents:
diff changeset
   494
      stream.println ("    __uninitialized = false;");
02bb8761fcce Initial load
duke
parents:
diff changeset
   495
      stream.println ("  }");
02bb8761fcce Initial load
duke
parents:
diff changeset
   496
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   497
02bb8761fcce Initial load
duke
parents:
diff changeset
   498
    // Write verifyXXX
02bb8761fcce Initial load
duke
parents:
diff changeset
   499
    stream.println ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   500
    stream.println ("  private void verify" + branch.typedef.name () + " (" + Util.javaName (utype) + " discriminator)");
02bb8761fcce Initial load
duke
parents:
diff changeset
   501
    stream.println ("  {");
02bb8761fcce Initial load
duke
parents:
diff changeset
   502
02bb8761fcce Initial load
duke
parents:
diff changeset
   503
    boolean onlyOne = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   504
02bb8761fcce Initial load
duke
parents:
diff changeset
   505
    if (branch.isDefault && u.branches ().size () == 1)
02bb8761fcce Initial load
duke
parents:
diff changeset
   506
      ;// If all that is in this union is a default branch,
02bb8761fcce Initial load
duke
parents:
diff changeset
   507
      // all discriminators are legal.  Don't print any
02bb8761fcce Initial load
duke
parents:
diff changeset
   508
      // body to this method in that case.
02bb8761fcce Initial load
duke
parents:
diff changeset
   509
    else
02bb8761fcce Initial load
duke
parents:
diff changeset
   510
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   511
      // Otherwise this code is executed and a body is printed.
02bb8761fcce Initial load
duke
parents:
diff changeset
   512
      stream.print ("    if (");
02bb8761fcce Initial load
duke
parents:
diff changeset
   513
      if (branch.isDefault)
02bb8761fcce Initial load
duke
parents:
diff changeset
   514
      {
02bb8761fcce Initial load
duke
parents:
diff changeset
   515
        Enumeration eBranches = u.branches ().elements ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   516
        while (eBranches.hasMoreElements ())
02bb8761fcce Initial load
duke
parents:
diff changeset
   517
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   518
          UnionBranch b = (UnionBranch)eBranches.nextElement ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   519
          if (b != branch)
02bb8761fcce Initial load
duke
parents:
diff changeset
   520
          {
02bb8761fcce Initial load
duke
parents:
diff changeset
   521
            Enumeration eLabels = b.labels.elements ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   522
            while (eLabels.hasMoreElements ())
02bb8761fcce Initial load
duke
parents:
diff changeset
   523
            {
02bb8761fcce Initial load
duke
parents:
diff changeset
   524
              Expression label = (Expression)eLabels.nextElement ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   525
              if (!onlyOne)
02bb8761fcce Initial load
duke
parents:
diff changeset
   526
                stream.print (" || ");
02bb8761fcce Initial load
duke
parents:
diff changeset
   527
              if (unionIsEnum)
02bb8761fcce Initial load
duke
parents:
diff changeset
   528
                stream.print ("discriminator == " + typePackage + Util.parseExpression (label));
02bb8761fcce Initial load
duke
parents:
diff changeset
   529
              else
02bb8761fcce Initial load
duke
parents:
diff changeset
   530
                stream.print ("discriminator == " + Util.parseExpression (label));
02bb8761fcce Initial load
duke
parents:
diff changeset
   531
              onlyOne = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   532
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   533
          }
02bb8761fcce Initial load
duke
parents:
diff changeset
   534
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   535
      }
02bb8761fcce Initial load
duke
parents:
diff changeset
   536
      else
02bb8761fcce Initial load
duke
parents:
diff changeset
   537
      {
02bb8761fcce Initial load
duke
parents:
diff changeset
   538
        Enumeration e = branch.labels.elements ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   539
        while (e.hasMoreElements ())
02bb8761fcce Initial load
duke
parents:
diff changeset
   540
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   541
          Expression label = (Expression)e.nextElement ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   542
          if (!onlyOne)
02bb8761fcce Initial load
duke
parents:
diff changeset
   543
            stream.print (" && ");
02bb8761fcce Initial load
duke
parents:
diff changeset
   544
          if (unionIsEnum)
02bb8761fcce Initial load
duke
parents:
diff changeset
   545
            stream.print ("discriminator != " + typePackage + Util.parseExpression (label));
02bb8761fcce Initial load
duke
parents:
diff changeset
   546
          else
02bb8761fcce Initial load
duke
parents:
diff changeset
   547
            stream.print ("discriminator != " + Util.parseExpression (label));
02bb8761fcce Initial load
duke
parents:
diff changeset
   548
          onlyOne = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   549
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   550
      }
02bb8761fcce Initial load
duke
parents:
diff changeset
   551
      stream.println (")");
02bb8761fcce Initial load
duke
parents:
diff changeset
   552
      stream.println ("      throw new org.omg.CORBA.BAD_OPERATION ();");
02bb8761fcce Initial load
duke
parents:
diff changeset
   553
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   554
    stream.println ("  }");
02bb8761fcce Initial load
duke
parents:
diff changeset
   555
  } // writeBranchMethods
02bb8761fcce Initial load
duke
parents:
diff changeset
   556
02bb8761fcce Initial load
duke
parents:
diff changeset
   557
  ///////////////
02bb8761fcce Initial load
duke
parents:
diff changeset
   558
  // From JavaGenerator
02bb8761fcce Initial load
duke
parents:
diff changeset
   559
02bb8761fcce Initial load
duke
parents:
diff changeset
   560
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   561
   *
02bb8761fcce Initial load
duke
parents:
diff changeset
   562
   **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   563
02bb8761fcce Initial load
duke
parents:
diff changeset
   564
  // Computes the total number of labels in the union, which is the sum
02bb8761fcce Initial load
duke
parents:
diff changeset
   565
  // of the number of labels in each branch of the union.  Note that the
02bb8761fcce Initial load
duke
parents:
diff changeset
   566
  // label for the default branch has size 0, but still counts in the total
02bb8761fcce Initial load
duke
parents:
diff changeset
   567
  // size.
02bb8761fcce Initial load
duke
parents:
diff changeset
   568
  private int unionLabelSize( UnionEntry un )
02bb8761fcce Initial load
duke
parents:
diff changeset
   569
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
   570
    int size = 0 ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   571
    Vector branches = un.branches() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   572
    for (int i = 0; i < branches.size (); ++i) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   573
        UnionBranch branch = (UnionBranch)(branches.get(i)) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   574
        int branchSize = branch.labels.size() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   575
        size += ((branchSize == 0) ? 1 : branchSize) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   576
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   577
    return size ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   578
  }
02bb8761fcce Initial load
duke
parents:
diff changeset
   579
02bb8761fcce Initial load
duke
parents:
diff changeset
   580
  public int helperType (int index, String indent, TCOffsets tcoffsets,
02bb8761fcce Initial load
duke
parents:
diff changeset
   581
    String name, SymtabEntry entry, PrintWriter stream)
02bb8761fcce Initial load
duke
parents:
diff changeset
   582
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
   583
    TCOffsets innerOffsets = new TCOffsets ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   584
    UnionEntry u = (UnionEntry)entry;
02bb8761fcce Initial load
duke
parents:
diff changeset
   585
    String discTypeCode = "_disTypeCode" + index;
02bb8761fcce Initial load
duke
parents:
diff changeset
   586
    String membersName = "_members" + index;
02bb8761fcce Initial load
duke
parents:
diff changeset
   587
02bb8761fcce Initial load
duke
parents:
diff changeset
   588
    // Build discriminator tc
02bb8761fcce Initial load
duke
parents:
diff changeset
   589
    stream.println (indent + "org.omg.CORBA.TypeCode " + discTypeCode + ';');
02bb8761fcce Initial load
duke
parents:
diff changeset
   590
    index = ((JavaGenerator)u.type ().generator ()).type (index + 1, indent,
02bb8761fcce Initial load
duke
parents:
diff changeset
   591
        innerOffsets, discTypeCode, u.type (), stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   592
    tcoffsets.bumpCurrentOffset (innerOffsets.currentOffset ());
02bb8761fcce Initial load
duke
parents:
diff changeset
   593
02bb8761fcce Initial load
duke
parents:
diff changeset
   594
    stream.println (indent + "org.omg.CORBA.UnionMember[] " + membersName +
02bb8761fcce Initial load
duke
parents:
diff changeset
   595
        " = new org.omg.CORBA.UnionMember [" + unionLabelSize(u) + "];");
02bb8761fcce Initial load
duke
parents:
diff changeset
   596
    String tcOfMembers = "_tcOf" + membersName;
02bb8761fcce Initial load
duke
parents:
diff changeset
   597
    String anyOfMembers = "_anyOf" + membersName;
02bb8761fcce Initial load
duke
parents:
diff changeset
   598
    stream.println (indent + "org.omg.CORBA.TypeCode " + tcOfMembers + ';');
02bb8761fcce Initial load
duke
parents:
diff changeset
   599
    stream.println (indent + "org.omg.CORBA.Any " + anyOfMembers + ';');
02bb8761fcce Initial load
duke
parents:
diff changeset
   600
02bb8761fcce Initial load
duke
parents:
diff changeset
   601
    innerOffsets = new TCOffsets ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   602
    innerOffsets.set (entry);
02bb8761fcce Initial load
duke
parents:
diff changeset
   603
    int offsetForUnion = innerOffsets.currentOffset ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   604
    for (int i = 0; i < u.branches ().size (); ++i) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   605
        UnionBranch branch = (UnionBranch)u.branches ().elementAt (i);
02bb8761fcce Initial load
duke
parents:
diff changeset
   606
        TypedefEntry member = branch.typedef;
02bb8761fcce Initial load
duke
parents:
diff changeset
   607
        Vector labels = branch.labels;
02bb8761fcce Initial load
duke
parents:
diff changeset
   608
        String memberName = Util.stripLeadingUnderscores (member.name ());
02bb8761fcce Initial load
duke
parents:
diff changeset
   609
02bb8761fcce Initial load
duke
parents:
diff changeset
   610
        if (labels.size() == 0) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   611
            stream.println ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   612
            stream.println (indent + "// Branch for " + memberName +
02bb8761fcce Initial load
duke
parents:
diff changeset
   613
                " (Default case)" );
02bb8761fcce Initial load
duke
parents:
diff changeset
   614
            SymtabEntry utype = Util.typeOf (u.type ());
02bb8761fcce Initial load
duke
parents:
diff changeset
   615
            stream.println (indent + anyOfMembers + " = org.omg.CORBA.ORB.init ().create_any ();");
02bb8761fcce Initial load
duke
parents:
diff changeset
   616
            // For default member, label is the zero octet (per CORBA spec.)
02bb8761fcce Initial load
duke
parents:
diff changeset
   617
            stream.println (indent + anyOfMembers + ".insert_octet ((byte)0); // default member label");
02bb8761fcce Initial load
duke
parents:
diff changeset
   618
02bb8761fcce Initial load
duke
parents:
diff changeset
   619
            // Build typecode
02bb8761fcce Initial load
duke
parents:
diff changeset
   620
            innerOffsets.bumpCurrentOffset (4); // label value
02bb8761fcce Initial load
duke
parents:
diff changeset
   621
            index = ((JavaGenerator)member.generator ()).type (index, indent, innerOffsets, tcOfMembers, member, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   622
            int offsetSoFar = innerOffsets.currentOffset ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   623
            innerOffsets = new TCOffsets ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   624
            innerOffsets.set (entry);
02bb8761fcce Initial load
duke
parents:
diff changeset
   625
            innerOffsets.bumpCurrentOffset (offsetSoFar - offsetForUnion);
02bb8761fcce Initial load
duke
parents:
diff changeset
   626
02bb8761fcce Initial load
duke
parents:
diff changeset
   627
            // Build union member
02bb8761fcce Initial load
duke
parents:
diff changeset
   628
            stream.println (indent + membersName + '[' + i + "] = new org.omg.CORBA.UnionMember (");
02bb8761fcce Initial load
duke
parents:
diff changeset
   629
            stream.println (indent + "  \"" + memberName + "\",");
02bb8761fcce Initial load
duke
parents:
diff changeset
   630
            stream.println (indent + "  " + anyOfMembers + ',');
02bb8761fcce Initial load
duke
parents:
diff changeset
   631
            stream.println (indent + "  " + tcOfMembers + ',');
02bb8761fcce Initial load
duke
parents:
diff changeset
   632
            stream.println (indent + "  null);");
02bb8761fcce Initial load
duke
parents:
diff changeset
   633
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   634
            Enumeration enumeration = labels.elements() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   635
            while (enumeration.hasMoreElements()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   636
                Expression expr = (Expression)(enumeration.nextElement()) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   637
                String elem = Util.parseExpression( expr ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   638
02bb8761fcce Initial load
duke
parents:
diff changeset
   639
                stream.println ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   640
                stream.println (indent + "// Branch for " + memberName +
02bb8761fcce Initial load
duke
parents:
diff changeset
   641
                    " (case label " + elem + ")" );
02bb8761fcce Initial load
duke
parents:
diff changeset
   642
02bb8761fcce Initial load
duke
parents:
diff changeset
   643
                SymtabEntry utype = Util.typeOf (u.type ());
02bb8761fcce Initial load
duke
parents:
diff changeset
   644
02bb8761fcce Initial load
duke
parents:
diff changeset
   645
                // Build any
02bb8761fcce Initial load
duke
parents:
diff changeset
   646
                stream.println (indent + anyOfMembers + " = org.omg.CORBA.ORB.init ().create_any ();");
02bb8761fcce Initial load
duke
parents:
diff changeset
   647
02bb8761fcce Initial load
duke
parents:
diff changeset
   648
                if (utype instanceof PrimitiveEntry)
02bb8761fcce Initial load
duke
parents:
diff changeset
   649
                    stream.println (indent + anyOfMembers + ".insert_" +
02bb8761fcce Initial load
duke
parents:
diff changeset
   650
                    Util.collapseName (utype.name ()) + " ((" + Util.javaName (utype) +
02bb8761fcce Initial load
duke
parents:
diff changeset
   651
                        ')' + elem + ");");
02bb8761fcce Initial load
duke
parents:
diff changeset
   652
                else { // it must be enum
02bb8761fcce Initial load
duke
parents:
diff changeset
   653
                    String enumClass = Util.javaName (utype);
02bb8761fcce Initial load
duke
parents:
diff changeset
   654
                    stream.println (indent + Util.helperName (utype, false) + ".insert (" +
02bb8761fcce Initial load
duke
parents:
diff changeset
   655
                        anyOfMembers + ", " + enumClass + '.' + elem + ");"); // <d61056>
02bb8761fcce Initial load
duke
parents:
diff changeset
   656
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   657
02bb8761fcce Initial load
duke
parents:
diff changeset
   658
                // Build typecode
02bb8761fcce Initial load
duke
parents:
diff changeset
   659
                innerOffsets.bumpCurrentOffset (4); // label value
02bb8761fcce Initial load
duke
parents:
diff changeset
   660
                index = ((JavaGenerator)member.generator ()).type (index, indent, innerOffsets, tcOfMembers, member, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   661
                int offsetSoFar = innerOffsets.currentOffset ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   662
                innerOffsets = new TCOffsets ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   663
                innerOffsets.set (entry);
02bb8761fcce Initial load
duke
parents:
diff changeset
   664
                innerOffsets.bumpCurrentOffset (offsetSoFar - offsetForUnion);
02bb8761fcce Initial load
duke
parents:
diff changeset
   665
02bb8761fcce Initial load
duke
parents:
diff changeset
   666
                // Build union member
02bb8761fcce Initial load
duke
parents:
diff changeset
   667
                stream.println (indent + membersName + '[' + i + "] = new org.omg.CORBA.UnionMember (");
02bb8761fcce Initial load
duke
parents:
diff changeset
   668
                stream.println (indent + "  \"" + memberName + "\",");
02bb8761fcce Initial load
duke
parents:
diff changeset
   669
                stream.println (indent + "  " + anyOfMembers + ',');
02bb8761fcce Initial load
duke
parents:
diff changeset
   670
                stream.println (indent + "  " + tcOfMembers + ',');
02bb8761fcce Initial load
duke
parents:
diff changeset
   671
                stream.println (indent + "  null);");
02bb8761fcce Initial load
duke
parents:
diff changeset
   672
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   673
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   674
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   675
02bb8761fcce Initial load
duke
parents:
diff changeset
   676
    tcoffsets.bumpCurrentOffset (innerOffsets.currentOffset ());
02bb8761fcce Initial load
duke
parents:
diff changeset
   677
02bb8761fcce Initial load
duke
parents:
diff changeset
   678
    // Build create_union_tc
02bb8761fcce Initial load
duke
parents:
diff changeset
   679
    stream.println (indent + name + " = org.omg.CORBA.ORB.init ().create_union_tc (" +
02bb8761fcce Initial load
duke
parents:
diff changeset
   680
        Util.helperName (u, true) + ".id (), \"" + entry.name () + "\", " +
02bb8761fcce Initial load
duke
parents:
diff changeset
   681
        discTypeCode + ", " + membersName + ");");
02bb8761fcce Initial load
duke
parents:
diff changeset
   682
    return index;
02bb8761fcce Initial load
duke
parents:
diff changeset
   683
  } // helperType
02bb8761fcce Initial load
duke
parents:
diff changeset
   684
02bb8761fcce Initial load
duke
parents:
diff changeset
   685
    public int type (int index, String indent, TCOffsets tcoffsets, String name,
02bb8761fcce Initial load
duke
parents:
diff changeset
   686
        SymtabEntry entry, PrintWriter stream)
02bb8761fcce Initial load
duke
parents:
diff changeset
   687
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   688
        stream.println (indent + name + " = " + Util.helperName (entry, true) + ".type ();");
02bb8761fcce Initial load
duke
parents:
diff changeset
   689
        return index;
02bb8761fcce Initial load
duke
parents:
diff changeset
   690
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   691
02bb8761fcce Initial load
duke
parents:
diff changeset
   692
    public void helperRead (String entryName, SymtabEntry entry, PrintWriter stream)
02bb8761fcce Initial load
duke
parents:
diff changeset
   693
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   694
        stream.println ("    " + entryName + " value = new " + entryName + " ();");
02bb8761fcce Initial load
duke
parents:
diff changeset
   695
        read (0, "    ", "value", entry, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   696
        stream.println ("    return value;");
02bb8761fcce Initial load
duke
parents:
diff changeset
   697
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   698
02bb8761fcce Initial load
duke
parents:
diff changeset
   699
    public void helperWrite (SymtabEntry entry, PrintWriter stream)
02bb8761fcce Initial load
duke
parents:
diff changeset
   700
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   701
        write (0, "    ", "value", entry, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   702
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   703
02bb8761fcce Initial load
duke
parents:
diff changeset
   704
    public int read (int index, String indent, String name,
02bb8761fcce Initial load
duke
parents:
diff changeset
   705
        SymtabEntry entry, PrintWriter stream)
02bb8761fcce Initial load
duke
parents:
diff changeset
   706
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   707
        UnionEntry u = (UnionEntry)entry;
02bb8761fcce Initial load
duke
parents:
diff changeset
   708
        String disName = "_dis" + index++;
02bb8761fcce Initial load
duke
parents:
diff changeset
   709
        SymtabEntry utype = Util.typeOf (u.type ());
02bb8761fcce Initial load
duke
parents:
diff changeset
   710
        Util.writeInitializer (indent, disName, "", utype, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   711
02bb8761fcce Initial load
duke
parents:
diff changeset
   712
        if (utype instanceof PrimitiveEntry)
02bb8761fcce Initial load
duke
parents:
diff changeset
   713
            index = ((JavaGenerator)utype.generator ()).read (index, indent, disName, utype, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   714
        else
02bb8761fcce Initial load
duke
parents:
diff changeset
   715
            stream.println (indent + disName + " = " + Util.helperName (utype, true) + ".read (istream);");
02bb8761fcce Initial load
duke
parents:
diff changeset
   716
02bb8761fcce Initial load
duke
parents:
diff changeset
   717
        if (utype.name ().equals ("boolean"))
02bb8761fcce Initial load
duke
parents:
diff changeset
   718
            index = readBoolean (disName, index, indent, name, u, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   719
        else
02bb8761fcce Initial load
duke
parents:
diff changeset
   720
            index = readNonBoolean (disName, index, indent, name, u, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   721
02bb8761fcce Initial load
duke
parents:
diff changeset
   722
        return index;
02bb8761fcce Initial load
duke
parents:
diff changeset
   723
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   724
02bb8761fcce Initial load
duke
parents:
diff changeset
   725
    private int readBoolean (String disName, int index, String indent,
02bb8761fcce Initial load
duke
parents:
diff changeset
   726
        String name, UnionEntry u, PrintWriter stream)
02bb8761fcce Initial load
duke
parents:
diff changeset
   727
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   728
        UnionBranch firstBranch = (UnionBranch)u.branches ().firstElement ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   729
        UnionBranch secondBranch;
02bb8761fcce Initial load
duke
parents:
diff changeset
   730
02bb8761fcce Initial load
duke
parents:
diff changeset
   731
        if (u.branches ().size () == 2)
02bb8761fcce Initial load
duke
parents:
diff changeset
   732
            secondBranch = (UnionBranch)u.branches ().lastElement ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   733
        else
02bb8761fcce Initial load
duke
parents:
diff changeset
   734
            secondBranch = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   735
02bb8761fcce Initial load
duke
parents:
diff changeset
   736
        boolean firstBranchIsTrue = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   737
        boolean noCases = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   738
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   739
            if (u.branches ().size () == 1 &&
02bb8761fcce Initial load
duke
parents:
diff changeset
   740
                (u.defaultBranch () != null || firstBranch.labels.size () == 2)) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   741
                noCases = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   742
            } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   743
                Expression expr = (Expression)(firstBranch.labels.firstElement()) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   744
                Boolean bool = (Boolean)(expr.evaluate()) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   745
                firstBranchIsTrue = bool.booleanValue ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   746
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   747
        } catch (EvaluationException ex) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   748
            // no action
02bb8761fcce Initial load
duke
parents:
diff changeset
   749
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   750
02bb8761fcce Initial load
duke
parents:
diff changeset
   751
        if (noCases) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   752
            // There is only a default label.  Since there are no cases,
02bb8761fcce Initial load
duke
parents:
diff changeset
   753
            // there is no need for if...else branches.
02bb8761fcce Initial load
duke
parents:
diff changeset
   754
            index = readBranch (index, indent, firstBranch.typedef.name (), "",  firstBranch.typedef, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   755
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   756
            // If first branch is false, swap branches
02bb8761fcce Initial load
duke
parents:
diff changeset
   757
            if (!firstBranchIsTrue) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   758
                UnionBranch tmp = firstBranch;
02bb8761fcce Initial load
duke
parents:
diff changeset
   759
                firstBranch = secondBranch;
02bb8761fcce Initial load
duke
parents:
diff changeset
   760
                secondBranch = tmp;
02bb8761fcce Initial load
duke
parents:
diff changeset
   761
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   762
02bb8761fcce Initial load
duke
parents:
diff changeset
   763
            stream.println (indent + "if (" + disName + ')');
02bb8761fcce Initial load
duke
parents:
diff changeset
   764
02bb8761fcce Initial load
duke
parents:
diff changeset
   765
            if (firstBranch == null)
02bb8761fcce Initial load
duke
parents:
diff changeset
   766
                stream.println (indent + "  throw new org.omg.CORBA.BAD_OPERATION ();");
02bb8761fcce Initial load
duke
parents:
diff changeset
   767
            else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   768
                stream.println (indent + '{');
02bb8761fcce Initial load
duke
parents:
diff changeset
   769
                index = readBranch (index, indent + "  ", firstBranch.typedef.name (),
02bb8761fcce Initial load
duke
parents:
diff changeset
   770
                    disName, firstBranch.typedef, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   771
                stream.println (indent + '}');
02bb8761fcce Initial load
duke
parents:
diff changeset
   772
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   773
02bb8761fcce Initial load
duke
parents:
diff changeset
   774
            stream.println (indent + "else");
02bb8761fcce Initial load
duke
parents:
diff changeset
   775
02bb8761fcce Initial load
duke
parents:
diff changeset
   776
            if (secondBranch == null)
02bb8761fcce Initial load
duke
parents:
diff changeset
   777
                stream.println (indent + "  throw new org.omg.CORBA.BAD_OPERATION ();");
02bb8761fcce Initial load
duke
parents:
diff changeset
   778
            else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   779
                stream.println (indent + '{');
02bb8761fcce Initial load
duke
parents:
diff changeset
   780
                index = readBranch (index, indent + "  ", secondBranch.typedef.name (),
02bb8761fcce Initial load
duke
parents:
diff changeset
   781
                    disName, secondBranch.typedef, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   782
                stream.println (indent + '}');
02bb8761fcce Initial load
duke
parents:
diff changeset
   783
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   784
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   785
02bb8761fcce Initial load
duke
parents:
diff changeset
   786
        return index;
02bb8761fcce Initial load
duke
parents:
diff changeset
   787
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   788
02bb8761fcce Initial load
duke
parents:
diff changeset
   789
    private int readNonBoolean (String disName, int index, String indent,
02bb8761fcce Initial load
duke
parents:
diff changeset
   790
        String name, UnionEntry u, PrintWriter stream)
02bb8761fcce Initial load
duke
parents:
diff changeset
   791
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   792
        SymtabEntry utype = Util.typeOf (u.type ());
02bb8761fcce Initial load
duke
parents:
diff changeset
   793
02bb8761fcce Initial load
duke
parents:
diff changeset
   794
        if (utype instanceof EnumEntry)
02bb8761fcce Initial load
duke
parents:
diff changeset
   795
            stream.println (indent + "switch (" + disName + ".value ())");
02bb8761fcce Initial load
duke
parents:
diff changeset
   796
        else
02bb8761fcce Initial load
duke
parents:
diff changeset
   797
            stream.println (indent + "switch (" + disName + ')');
02bb8761fcce Initial load
duke
parents:
diff changeset
   798
02bb8761fcce Initial load
duke
parents:
diff changeset
   799
        stream.println (indent + '{');
02bb8761fcce Initial load
duke
parents:
diff changeset
   800
        String typePackage = Util.javaQualifiedName (utype) + '.';
02bb8761fcce Initial load
duke
parents:
diff changeset
   801
02bb8761fcce Initial load
duke
parents:
diff changeset
   802
        Enumeration e = u.branches ().elements ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   803
        while (e.hasMoreElements ()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   804
            UnionBranch branch = (UnionBranch)e.nextElement ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   805
            Enumeration labels = branch.labels.elements ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   806
02bb8761fcce Initial load
duke
parents:
diff changeset
   807
            while (labels.hasMoreElements ()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   808
                Expression label = (Expression)labels.nextElement ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   809
02bb8761fcce Initial load
duke
parents:
diff changeset
   810
                if (utype instanceof EnumEntry) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   811
                    String key = Util.parseExpression (label);
02bb8761fcce Initial load
duke
parents:
diff changeset
   812
                    stream.println (indent + "  case " + typePackage + '_' + key + ':');
02bb8761fcce Initial load
duke
parents:
diff changeset
   813
                } else
02bb8761fcce Initial load
duke
parents:
diff changeset
   814
                    stream.println (indent + "  case " + cast (label, utype) + ':');
02bb8761fcce Initial load
duke
parents:
diff changeset
   815
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   816
02bb8761fcce Initial load
duke
parents:
diff changeset
   817
            if (!branch.typedef.equals (u.defaultBranch ())) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   818
                index = readBranch (index, indent + "    ", branch.typedef.name (),
02bb8761fcce Initial load
duke
parents:
diff changeset
   819
                    branch.labels.size() > 1 ? disName : "" ,
02bb8761fcce Initial load
duke
parents:
diff changeset
   820
                    branch.typedef, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   821
                stream.println (indent + "    break;");
02bb8761fcce Initial load
duke
parents:
diff changeset
   822
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   823
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   824
02bb8761fcce Initial load
duke
parents:
diff changeset
   825
        // We need a default branch unless all of the case of the discriminator type
02bb8761fcce Initial load
duke
parents:
diff changeset
   826
        // are listed in the case branches.
02bb8761fcce Initial load
duke
parents:
diff changeset
   827
        if (!coversAll(u)) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   828
            stream.println( indent + "  default:") ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   829
02bb8761fcce Initial load
duke
parents:
diff changeset
   830
            if (u.defaultBranch () == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   831
                // If the union does not have a default branch, we still need to initialize
02bb8761fcce Initial load
duke
parents:
diff changeset
   832
                // the discriminator.
02bb8761fcce Initial load
duke
parents:
diff changeset
   833
                stream.println( indent + "    value._default( " + disName + " ) ;" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   834
            } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   835
                index = readBranch (index, indent + "    ", u.defaultBranch ().name (), disName,
02bb8761fcce Initial load
duke
parents:
diff changeset
   836
                    u.defaultBranch (), stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   837
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   838
02bb8761fcce Initial load
duke
parents:
diff changeset
   839
            stream.println (indent + "    break;");
02bb8761fcce Initial load
duke
parents:
diff changeset
   840
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   841
02bb8761fcce Initial load
duke
parents:
diff changeset
   842
        stream.println (indent + '}');
02bb8761fcce Initial load
duke
parents:
diff changeset
   843
02bb8761fcce Initial load
duke
parents:
diff changeset
   844
        return index;
02bb8761fcce Initial load
duke
parents:
diff changeset
   845
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   846
02bb8761fcce Initial load
duke
parents:
diff changeset
   847
    private int readBranch (int index, String indent, String name, String disName, TypedefEntry entry, PrintWriter stream)
02bb8761fcce Initial load
duke
parents:
diff changeset
   848
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   849
        SymtabEntry type = entry.type ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   850
        Util.writeInitializer (indent, '_' + name, "", entry, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   851
02bb8761fcce Initial load
duke
parents:
diff changeset
   852
        if (!entry.arrayInfo ().isEmpty () ||
02bb8761fcce Initial load
duke
parents:
diff changeset
   853
            type instanceof SequenceEntry ||
02bb8761fcce Initial load
duke
parents:
diff changeset
   854
            type instanceof PrimitiveEntry ||
02bb8761fcce Initial load
duke
parents:
diff changeset
   855
            type instanceof StringEntry) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   856
            index = ((JavaGenerator)entry.generator ()).read (index, indent, '_' + name, entry, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   857
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   858
            stream.println (indent + '_' + name + " = " + Util.helperName (type, true) + ".read (istream);");
02bb8761fcce Initial load
duke
parents:
diff changeset
   859
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   860
02bb8761fcce Initial load
duke
parents:
diff changeset
   861
        stream.print (indent + "value." + name + " (");
02bb8761fcce Initial load
duke
parents:
diff changeset
   862
        if( disName == "" )
02bb8761fcce Initial load
duke
parents:
diff changeset
   863
            stream.println("_" + name + ");");
02bb8761fcce Initial load
duke
parents:
diff changeset
   864
        else
02bb8761fcce Initial load
duke
parents:
diff changeset
   865
            stream.println(disName + ", " + "_" + name + ");");
02bb8761fcce Initial load
duke
parents:
diff changeset
   866
02bb8761fcce Initial load
duke
parents:
diff changeset
   867
        return index;
02bb8761fcce Initial load
duke
parents:
diff changeset
   868
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   869
02bb8761fcce Initial load
duke
parents:
diff changeset
   870
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   871
   *
02bb8761fcce Initial load
duke
parents:
diff changeset
   872
   **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   873
  public int write (int index, String indent, String name, SymtabEntry entry, PrintWriter stream)
02bb8761fcce Initial load
duke
parents:
diff changeset
   874
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
   875
    UnionEntry u = (UnionEntry)entry;
02bb8761fcce Initial load
duke
parents:
diff changeset
   876
    SymtabEntry utype = Util.typeOf (u.type ());
02bb8761fcce Initial load
duke
parents:
diff changeset
   877
    if (utype instanceof PrimitiveEntry)
02bb8761fcce Initial load
duke
parents:
diff changeset
   878
      index = ((JavaGenerator)utype.generator ()).write (index, indent, name + ".discriminator ()", utype, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   879
    else
02bb8761fcce Initial load
duke
parents:
diff changeset
   880
      stream.println (indent + Util.helperName (utype, true) + ".write (ostream, " + name + ".discriminator ());"); // <d61056>
02bb8761fcce Initial load
duke
parents:
diff changeset
   881
    if (utype.name ().equals ("boolean"))
02bb8761fcce Initial load
duke
parents:
diff changeset
   882
      index = writeBoolean (name + ".discriminator ()", index, indent, name, u, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   883
    else
02bb8761fcce Initial load
duke
parents:
diff changeset
   884
      index = writeNonBoolean (name + ".discriminator ()", index, indent, name, u, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   885
    return index;
02bb8761fcce Initial load
duke
parents:
diff changeset
   886
  } // write
02bb8761fcce Initial load
duke
parents:
diff changeset
   887
02bb8761fcce Initial load
duke
parents:
diff changeset
   888
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   889
   *
02bb8761fcce Initial load
duke
parents:
diff changeset
   890
   **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   891
  private int writeBoolean (String disName, int index, String indent, String name, UnionEntry u, PrintWriter stream)
02bb8761fcce Initial load
duke
parents:
diff changeset
   892
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
   893
    SymtabEntry utype = Util.typeOf (u.type ());
02bb8761fcce Initial load
duke
parents:
diff changeset
   894
    UnionBranch firstBranch = (UnionBranch)u.branches ().firstElement ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   895
    UnionBranch secondBranch;
02bb8761fcce Initial load
duke
parents:
diff changeset
   896
    if (u.branches ().size () == 2)
02bb8761fcce Initial load
duke
parents:
diff changeset
   897
      secondBranch = (UnionBranch)u.branches ().lastElement ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   898
    else
02bb8761fcce Initial load
duke
parents:
diff changeset
   899
      secondBranch = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   900
    boolean firstBranchIsTrue = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   901
    boolean noCases = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   902
    try
02bb8761fcce Initial load
duke
parents:
diff changeset
   903
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   904
      if (u.branches ().size () == 1 && (u.defaultBranch () != null || firstBranch.labels.size () == 2))
02bb8761fcce Initial load
duke
parents:
diff changeset
   905
        noCases = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   906
      else
02bb8761fcce Initial load
duke
parents:
diff changeset
   907
        firstBranchIsTrue = ((Boolean)((Expression)firstBranch.labels.firstElement ()).evaluate ()).booleanValue ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   908
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   909
    catch (EvaluationException ex)
02bb8761fcce Initial load
duke
parents:
diff changeset
   910
    {}
02bb8761fcce Initial load
duke
parents:
diff changeset
   911
02bb8761fcce Initial load
duke
parents:
diff changeset
   912
    if (noCases)
02bb8761fcce Initial load
duke
parents:
diff changeset
   913
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   914
      // There is only a default label.  Since there are no cases,
02bb8761fcce Initial load
duke
parents:
diff changeset
   915
      // there is no need for if...else branches.
02bb8761fcce Initial load
duke
parents:
diff changeset
   916
      index = writeBranch (index, indent, name, firstBranch.typedef, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   917
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   918
    else
02bb8761fcce Initial load
duke
parents:
diff changeset
   919
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   920
      // If first branch is false, swap branches
02bb8761fcce Initial load
duke
parents:
diff changeset
   921
      if (!firstBranchIsTrue)
02bb8761fcce Initial load
duke
parents:
diff changeset
   922
      {
02bb8761fcce Initial load
duke
parents:
diff changeset
   923
        UnionBranch tmp = firstBranch;
02bb8761fcce Initial load
duke
parents:
diff changeset
   924
        firstBranch = secondBranch;
02bb8761fcce Initial load
duke
parents:
diff changeset
   925
        secondBranch = tmp;
02bb8761fcce Initial load
duke
parents:
diff changeset
   926
      }
02bb8761fcce Initial load
duke
parents:
diff changeset
   927
      stream.println (indent + "if (" + disName + ')');
02bb8761fcce Initial load
duke
parents:
diff changeset
   928
      if (firstBranch == null)
02bb8761fcce Initial load
duke
parents:
diff changeset
   929
        stream.println (indent + "  throw new org.omg.CORBA.BAD_OPERATION ();");
02bb8761fcce Initial load
duke
parents:
diff changeset
   930
      else
02bb8761fcce Initial load
duke
parents:
diff changeset
   931
      {
02bb8761fcce Initial load
duke
parents:
diff changeset
   932
        stream.println (indent + '{');
02bb8761fcce Initial load
duke
parents:
diff changeset
   933
        index = writeBranch (index, indent + "  ", name, firstBranch.typedef, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   934
        stream.println (indent + '}');
02bb8761fcce Initial load
duke
parents:
diff changeset
   935
      }
02bb8761fcce Initial load
duke
parents:
diff changeset
   936
      stream.println (indent + "else");
02bb8761fcce Initial load
duke
parents:
diff changeset
   937
      if (secondBranch == null)
02bb8761fcce Initial load
duke
parents:
diff changeset
   938
        stream.println (indent + "  throw new org.omg.CORBA.BAD_OPERATION ();");
02bb8761fcce Initial load
duke
parents:
diff changeset
   939
      else
02bb8761fcce Initial load
duke
parents:
diff changeset
   940
      {
02bb8761fcce Initial load
duke
parents:
diff changeset
   941
        stream.println (indent + '{');
02bb8761fcce Initial load
duke
parents:
diff changeset
   942
        index = writeBranch (index, indent + "  ", name, secondBranch.typedef, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   943
        stream.println (indent + '}');
02bb8761fcce Initial load
duke
parents:
diff changeset
   944
      }
02bb8761fcce Initial load
duke
parents:
diff changeset
   945
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   946
    return index;
02bb8761fcce Initial load
duke
parents:
diff changeset
   947
  } // writeBoolean
02bb8761fcce Initial load
duke
parents:
diff changeset
   948
02bb8761fcce Initial load
duke
parents:
diff changeset
   949
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   950
   *
02bb8761fcce Initial load
duke
parents:
diff changeset
   951
   **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   952
  private int writeNonBoolean (String disName, int index, String indent, String name, UnionEntry u, PrintWriter stream)
02bb8761fcce Initial load
duke
parents:
diff changeset
   953
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
   954
    SymtabEntry utype = Util.typeOf (u.type ());
02bb8761fcce Initial load
duke
parents:
diff changeset
   955
    if (utype instanceof EnumEntry)
02bb8761fcce Initial load
duke
parents:
diff changeset
   956
      stream.println (indent + "switch (" + name + ".discriminator ().value ())");
02bb8761fcce Initial load
duke
parents:
diff changeset
   957
    else
02bb8761fcce Initial load
duke
parents:
diff changeset
   958
      stream.println (indent + "switch (" + name + ".discriminator ())");
02bb8761fcce Initial load
duke
parents:
diff changeset
   959
    stream.println (indent + "{");
02bb8761fcce Initial load
duke
parents:
diff changeset
   960
    String typePackage = Util.javaQualifiedName (utype) + '.';
02bb8761fcce Initial load
duke
parents:
diff changeset
   961
    Enumeration e = u.branches ().elements ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   962
    while (e.hasMoreElements ())
02bb8761fcce Initial load
duke
parents:
diff changeset
   963
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   964
      UnionBranch branch = (UnionBranch)e.nextElement ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   965
      Enumeration labels = branch.labels.elements ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   966
      while (labels.hasMoreElements ())
02bb8761fcce Initial load
duke
parents:
diff changeset
   967
      {
02bb8761fcce Initial load
duke
parents:
diff changeset
   968
        Expression label = (Expression)labels.nextElement ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   969
        if (utype instanceof EnumEntry)
02bb8761fcce Initial load
duke
parents:
diff changeset
   970
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   971
          String key = Util.parseExpression (label);
02bb8761fcce Initial load
duke
parents:
diff changeset
   972
          stream.println (indent + "  case " + typePackage + '_' + key + ":");
02bb8761fcce Initial load
duke
parents:
diff changeset
   973
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   974
        else
02bb8761fcce Initial load
duke
parents:
diff changeset
   975
          stream.println (indent + "  case " + cast (label, utype) + ':');
02bb8761fcce Initial load
duke
parents:
diff changeset
   976
      }
02bb8761fcce Initial load
duke
parents:
diff changeset
   977
      if (!branch.typedef.equals (u.defaultBranch ()))
02bb8761fcce Initial load
duke
parents:
diff changeset
   978
      {
02bb8761fcce Initial load
duke
parents:
diff changeset
   979
        index = writeBranch (index, indent + "    ", name, branch.typedef, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   980
        stream.println (indent + "    break;");
02bb8761fcce Initial load
duke
parents:
diff changeset
   981
      }
02bb8761fcce Initial load
duke
parents:
diff changeset
   982
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   983
    if (u.defaultBranch () != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   984
      stream.println (indent + "  default:");
02bb8761fcce Initial load
duke
parents:
diff changeset
   985
      index = writeBranch (index, indent + "    ", name, u.defaultBranch (), stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   986
      stream.println (indent + "    break;");
02bb8761fcce Initial load
duke
parents:
diff changeset
   987
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   988
    stream.println (indent + "}");
02bb8761fcce Initial load
duke
parents:
diff changeset
   989
    return index;
02bb8761fcce Initial load
duke
parents:
diff changeset
   990
  } // writeNonBoolean
02bb8761fcce Initial load
duke
parents:
diff changeset
   991
02bb8761fcce Initial load
duke
parents:
diff changeset
   992
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   993
   *
02bb8761fcce Initial load
duke
parents:
diff changeset
   994
   **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   995
  private int writeBranch (int index, String indent, String name, TypedefEntry entry, PrintWriter stream)
02bb8761fcce Initial load
duke
parents:
diff changeset
   996
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
   997
    SymtabEntry type = entry.type ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   998
    if (!entry.arrayInfo ().isEmpty () || type instanceof SequenceEntry || type instanceof PrimitiveEntry || type instanceof StringEntry)
02bb8761fcce Initial load
duke
parents:
diff changeset
   999
      index = ((JavaGenerator)entry.generator ()).write (index, indent, name + '.' + entry.name () + " ()", entry, stream);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1000
    else
02bb8761fcce Initial load
duke
parents:
diff changeset
  1001
      stream.println (indent + Util.helperName (type, true) + ".write (ostream, " + name + '.' + entry.name () + " ());"); // <d61056>
02bb8761fcce Initial load
duke
parents:
diff changeset
  1002
    return index;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1003
  } // writeBranch
02bb8761fcce Initial load
duke
parents:
diff changeset
  1004
02bb8761fcce Initial load
duke
parents:
diff changeset
  1005
  // From JavaGenerator
02bb8761fcce Initial load
duke
parents:
diff changeset
  1006
  ///////////////
02bb8761fcce Initial load
duke
parents:
diff changeset
  1007
02bb8761fcce Initial load
duke
parents:
diff changeset
  1008
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
  1009
   *
02bb8761fcce Initial load
duke
parents:
diff changeset
  1010
   **/
02bb8761fcce Initial load
duke
parents:
diff changeset
  1011
  private String cast (Expression expr, SymtabEntry type)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1012
  {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1013
    String ret = Util.parseExpression (expr);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1014
    if (type.name ().indexOf ("short") >= 0)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1015
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1016
      if (expr.value () instanceof Long)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1017
      {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1018
        long value = ((Long)expr.value ()).longValue ();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1019
        if (value > Short.MAX_VALUE)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1020
          ret = "(short)(" + ret + ')';
02bb8761fcce Initial load
duke
parents:
diff changeset
  1021
      }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1022
      else if (expr.value () instanceof Integer)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1023
      {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1024
        int value = ((Integer)expr.value ()).intValue ();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1025
        if (value > Short.MAX_VALUE)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1026
          ret = "(short)(" + ret + ')';
02bb8761fcce Initial load
duke
parents:
diff changeset
  1027
      }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1028
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1029
    else if (type.name ().indexOf ("long") >= 0)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1030
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1031
      if (expr.value () instanceof Long)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1032
      {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1033
        long value = ((Long)expr.value ()).longValue ();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1034
        // value == Integer.MIN_VALUE because if the number is
02bb8761fcce Initial load
duke
parents:
diff changeset
  1035
        // Integer.MIN_VALUE, then it will have the 'L' suffix and
02bb8761fcce Initial load
duke
parents:
diff changeset
  1036
        // the cast will be necessary.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1037
        if (value > Integer.MAX_VALUE || value == Integer.MIN_VALUE)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1038
          ret = "(int)(" + ret + ')';
02bb8761fcce Initial load
duke
parents:
diff changeset
  1039
      }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1040
      else if (expr.value () instanceof Integer)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1041
      {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1042
        int value = ((Integer)expr.value ()).intValue ();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1043
        // value == Integer.MIN_VALUE because if the number is
02bb8761fcce Initial load
duke
parents:
diff changeset
  1044
        // Integer.MIN_VALUE, then it will have the 'L' suffix and
02bb8761fcce Initial load
duke
parents:
diff changeset
  1045
        // the cast will be necessary.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1046
        if (value > Integer.MAX_VALUE || value == Integer.MIN_VALUE)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1047
          ret = "(int)(" + ret + ')';
02bb8761fcce Initial load
duke
parents:
diff changeset
  1048
      }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1049
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1050
    return ret;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1051
  } // cast
02bb8761fcce Initial load
duke
parents:
diff changeset
  1052
02bb8761fcce Initial load
duke
parents:
diff changeset
  1053
  protected Hashtable   symbolTable = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1054
  protected UnionEntry  u           = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1055
  protected PrintWriter stream      = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1056
  protected SymtabEntry utype       = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1057
  protected boolean     unionIsEnum;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1058
  protected String      typePackage = "";
02bb8761fcce Initial load
duke
parents:
diff changeset
  1059
} // class UnionGen