UnionEntry.java revision 608:7e06bf1dcb09
1248590Smm/*
2248590Smm * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
3248590Smm * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4248590Smm *
5248590Smm * This code is free software; you can redistribute it and/or modify it
6248590Smm * under the terms of the GNU General Public License version 2 only, as
7248590Smm * published by the Free Software Foundation.  Oracle designates this
8248590Smm * particular file as subject to the "Classpath" exception as provided
9248590Smm * by Oracle in the LICENSE file that accompanied this code.
10248590Smm *
11248590Smm * This code is distributed in the hope that it will be useful, but WITHOUT
12248590Smm * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13248590Smm * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14248590Smm * version 2 for more details (a copy is included in the LICENSE file that
15248590Smm * accompanied this code).
16248590Smm *
17248590Smm * You should have received a copy of the GNU General Public License version
18248590Smm * 2 along with this work; if not, write to the Free Software Foundation,
19248590Smm * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20248590Smm *
21248590Smm * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22248590Smm * or visit www.oracle.com if you need additional information or have any
23248590Smm * questions.
24248590Smm */
25248590Smm/*
26248590Smm * COMPONENT_NAME: idl.parser
27248590Smm *
28248590Smm * ORIGINS: 27
29248590Smm *
30248590Smm * Licensed Materials - Property of IBM
31248590Smm * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
32248590Smm * RMI-IIOP v1.0
33248590Smm *
34248590Smm */
35248590Smm
36248590Smmpackage com.sun.tools.corba.se.idl;
37248590Smm
38248590Smm// NOTES:
39248590Smm
40248590Smmimport java.io.PrintWriter;
41248590Smmimport java.util.Enumeration;
42248590Smmimport java.util.Hashtable;
43248590Smmimport java.util.Vector;
44248590Smm
45248590Smmimport com.sun.tools.corba.se.idl.constExpr.Expression;
46248590Smm
47248590Smm/**
48302001Smm * This is the symbol table entry for unions.
49302001Smm **/
50248590Smmpublic class UnionEntry extends SymtabEntry
51248590Smm{
52248590Smm  protected UnionEntry ()
53248590Smm  {
54248590Smm    super ();
55248590Smm  } // ctor
56248590Smm
57248590Smm  protected UnionEntry (UnionEntry that)
58248590Smm  {
59248590Smm    super (that);
60248590Smm    if (!name ().equals (""))
61248590Smm    {
62248590Smm      module (module () + name ());
63248590Smm      name ("");
64248590Smm    }
65248590Smm    _branches      = (Vector)that._branches.clone ();
66248590Smm    _defaultBranch = that._defaultBranch;
67248590Smm    _contained     = that._contained;
68248590Smm  } // ctor
69248590Smm
70248590Smm  protected UnionEntry (SymtabEntry that, IDLID clone)
71248590Smm  {
72248590Smm    super (that, clone);
73248590Smm    if (module ().equals (""))
74248590Smm      module (name ());
75248590Smm    else if (!name ().equals (""))
76248590Smm      module (module () + "/" + name ());
77248590Smm  } // ctor
78248590Smm
79248590Smm  public Object clone ()
80248590Smm  {
81248590Smm    return new UnionEntry (this);
82248590Smm  } // clone
83248590Smm
84248590Smm  /** Invoke the union generator.
85248590Smm      @param symbolTable the symbol table is a hash table whose key is
86248590Smm       a fully qualified type name and whose value is a SymtabEntry or
87248590Smm       a subclass of SymtabEntry.
88248590Smm      @param stream the stream to which the generator should sent its output.
89248590Smm      @see SymtabEntry */
90248590Smm  public void generate (Hashtable symbolTable, PrintWriter stream)
91248590Smm  {
92248590Smm    unionGen.generate (symbolTable, this, stream);
93248590Smm  } // generate
94248590Smm
95248590Smm  /** Access the union generator.
96248590Smm      @returns an object which implements the UnionGen interface.
97248590Smm      @see UnionGen */
98248590Smm  public Generator generator ()
99248590Smm  {
100248590Smm    return unionGen;
101248590Smm  } // generator
102248590Smm
103248590Smm  public void addBranch (UnionBranch branch)
104248590Smm  {
105248590Smm    _branches.addElement (branch);
106248590Smm  } // addBranch
107248590Smm
108248590Smm  /** This is a vector of UnionBranch's. */
109248590Smm  public Vector branches ()
110248590Smm  {
111248590Smm    return _branches;
112248590Smm  } // branches
113248590Smm
114248590Smm  /** This TypedefEntry describes the type and name for the default branch.
115248590Smm      Like the entries in the branches vector, only the type and name fields
116248590Smm      are pertinent. */
117248590Smm  public void defaultBranch (TypedefEntry branch)
118248590Smm  {
119248590Smm    _defaultBranch = branch;
120248590Smm  } // defaultBranch
121248590Smm
122248590Smm  /** This TypedefEntry describes the type and name for the default branch.
123248590Smm      Like the entries in the branches vector, only the type and name fields
124248590Smm      are pertinent. */
125248590Smm  public TypedefEntry defaultBranch ()
126248590Smm  {
127248590Smm    return _defaultBranch;
128248590Smm  } // defaultBranch
129248590Smm
130248590Smm  public void addContained (SymtabEntry entry)
131248590Smm  {
132248590Smm    _contained.addElement (entry);
133248590Smm  } // addContained
134248590Smm
135248590Smm  /** This is a vector of SymtabEntry's.  It itemizes any types which
136248590Smm      this union contains.  For example:
137248590Smm
138248590Smm      <pre>
139248590Smm      union A
140248590Smm      switch (long)
141248590Smm      {
142248590Smm        case 0: long x;
143248590Smm        case 1:
144248590Smm          Struct B
145248590Smm          {
146248590Smm            long a;
147248590Smm            long b;
148248590Smm          } y;
149248590Smm      }
150248590Smm      </pre>
151248590Smm      Struct B is contained within union A. */
152248590Smm  public Vector contained ()
153248590Smm  {
154248590Smm    return _contained;
155248590Smm  } // contained
156248590Smm
157248590Smm  boolean has (Expression label)
158248590Smm  {
159248590Smm    Enumeration eBranches = _branches.elements ();
160248590Smm    while (eBranches.hasMoreElements ())
161248590Smm    {
162248590Smm      Enumeration eLabels = ((UnionBranch)eBranches.nextElement ()).labels.elements ();
163248590Smm      while (eLabels.hasMoreElements ())
164248590Smm      {
165248590Smm        Expression exp = (Expression)eLabels.nextElement ();
166248590Smm        if (exp.equals (label) || exp.value ().equals (label.value ()))
167248590Smm          return true;
168248590Smm      }
169248590Smm    }
170248590Smm    return false;
171248590Smm  } // has
172248590Smm
173248590Smm  boolean has (TypedefEntry typedef)
174248590Smm  {
175248590Smm    Enumeration e = _branches.elements ();
176248590Smm    while (e.hasMoreElements ())
177248590Smm    {
178248590Smm      UnionBranch branch = (UnionBranch)e.nextElement ();
179248590Smm      if (!branch.typedef.equals (typedef) && branch.typedef.name ().equals (typedef.name ()))
180248590Smm        return true;
181248590Smm    }
182248590Smm    return false;
183248590Smm  } // has
184248590Smm
185248590Smm  /** A vector of UnionBranch's. */
186248590Smm  private Vector       _branches      = new Vector ();
187248590Smm  private TypedefEntry _defaultBranch = null;
188248590Smm  private Vector       _contained     = new Vector ();
189248590Smm
190248590Smm  static UnionGen unionGen;
191248590Smm} // class UnionEntry
192248590Smm