1 /*
   2  * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 /*
  26  * COMPONENT_NAME: idl.parser
  27  *
  28  * ORIGINS: 27
  29  *
  30  * Licensed Materials - Property of IBM
  31  * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
  32  * RMI-IIOP v1.0
  33  *
  34  */
  35 
  36 package com.sun.tools.corba.se.idl;
  37 
  38 // NOTES:
  39 
  40 import java.io.PrintWriter;
  41 import java.util.Enumeration;
  42 import java.util.Hashtable;
  43 import java.util.Vector;
  44 
  45 import com.sun.tools.corba.se.idl.constExpr.Expression;
  46 
  47 /**
  48  * This is the symbol table entry for unions.
  49  **/
  50 public class UnionEntry extends SymtabEntry
  51 {
  52   protected UnionEntry ()
  53   {
  54     super ();
  55   } // ctor
  56 
  57   protected UnionEntry (UnionEntry that)
  58   {
  59     super (that);
  60     if (!name ().equals (""))
  61     {
  62       module (module () + name ());
  63       name ("");
  64     }
  65     _branches      = (Vector)that._branches.clone ();
  66     _defaultBranch = that._defaultBranch;
  67     _contained     = that._contained;
  68   } // ctor
  69 
  70   protected UnionEntry (SymtabEntry that, IDLID clone)
  71   {
  72     super (that, clone);
  73     if (module ().equals (""))
  74       module (name ());
  75     else if (!name ().equals (""))
  76       module (module () + "/" + name ());
  77   } // ctor
  78 
  79   public Object clone ()
  80   {
  81     return new UnionEntry (this);
  82   } // clone
  83 
  84   /** Invoke the union generator.
  85       @param symbolTable the symbol table is a hash table whose key is
  86        a fully qualified type name and whose value is a SymtabEntry or
  87        a subclass of SymtabEntry.
  88       @param stream the stream to which the generator should sent its output.
  89       @see SymtabEntry */
  90   public void generate (Hashtable symbolTable, PrintWriter stream)
  91   {
  92     unionGen.generate (symbolTable, this, stream);
  93   } // generate
  94 
  95   /** Access the union generator.
  96       @returns an object which implements the UnionGen interface.
  97       @see UnionGen */
  98   public Generator generator ()
  99   {
 100     return unionGen;
 101   } // generator
 102 
 103   public void addBranch (UnionBranch branch)
 104   {
 105     _branches.addElement (branch);
 106   } // addBranch
 107 
 108   /** This is a vector of UnionBranch's. */
 109   public Vector branches ()
 110   {
 111     return _branches;
 112   } // branches
 113 
 114   /** This TypedefEntry describes the type and name for the default branch.
 115       Like the entries in the branches vector, only the type and name fields
 116       are pertinent. */
 117   public void defaultBranch (TypedefEntry branch)
 118   {
 119     _defaultBranch = branch;
 120   } // defaultBranch
 121 
 122   /** This TypedefEntry describes the type and name for the default branch.
 123       Like the entries in the branches vector, only the type and name fields
 124       are pertinent. */
 125   public TypedefEntry defaultBranch ()
 126   {
 127     return _defaultBranch;
 128   } // defaultBranch
 129 
 130   public void addContained (SymtabEntry entry)
 131   {
 132     _contained.addElement (entry);
 133   } // addContained
 134 
 135   /** This is a vector of SymtabEntry's.  It itemizes any types which
 136       this union contains.  For example:
 137 
 138       <pre>
 139       union A
 140       switch (long)
 141       {
 142         case 0: long x;
 143         case 1:
 144           Struct B
 145           {
 146             long a;
 147             long b;
 148           } y;
 149       }
 150       </pre>
 151       Struct B is contained within union A. */
 152   public Vector contained ()
 153   {
 154     return _contained;
 155   } // contained
 156 
 157   boolean has (Expression label)
 158   {
 159     Enumeration eBranches = _branches.elements ();
 160     while (eBranches.hasMoreElements ())
 161     {
 162       Enumeration eLabels = ((UnionBranch)eBranches.nextElement ()).labels.elements ();
 163       while (eLabels.hasMoreElements ())
 164       {
 165         Expression exp = (Expression)eLabels.nextElement ();
 166         if (exp.equals (label) || exp.value ().equals (label.value ()))
 167           return true;
 168       }
 169     }
 170     return false;
 171   } // has
 172 
 173   boolean has (TypedefEntry typedef)
 174   {
 175     Enumeration e = _branches.elements ();
 176     while (e.hasMoreElements ())
 177     {
 178       UnionBranch branch = (UnionBranch)e.nextElement ();
 179       if (!branch.typedef.equals (typedef) && branch.typedef.name ().equals (typedef.name ()))
 180         return true;
 181     }
 182     return false;
 183   } // has
 184 
 185   /** A vector of UnionBranch's. */
 186   private Vector       _branches      = new Vector ();
 187   private TypedefEntry _defaultBranch = null;
 188   private Vector       _contained     = new Vector ();
 189 
 190   static UnionGen unionGen;
 191 } // class UnionEntry