SymtabEntry.java revision 672:2bb058ce572e
1/*
2 * Copyright (c) 1999, 2004, 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
36package com.sun.tools.corba.se.idl;
37
38// NOTES:
39
40import java.io.PrintWriter;
41
42import java.util.Hashtable;
43import java.util.Stack;
44import java.util.Vector;
45
46/**
47 * This is the base class for all symbol table entries.
48 * @see AttributeEntry
49 * @see ConstEntry
50 * @see EnumEntry
51 * @see ExceptionEntry
52 * @see IncludeEntry
53 * @see InterfaceEntry
54 * @see MethodEntry
55 * @see ModuleEntry
56 * @see ParameterEntry
57 * @see PragmaEntry
58 * @see PrimitiveEntry
59 * @see SequenceEntry
60 * @see StructEntry
61 * @see TypedefEntry
62 * @see UnionEntry
63 **/
64public class SymtabEntry
65{
66  public SymtabEntry ()
67  {
68    initDynamicVars ();
69  } // ctor
70
71  SymtabEntry (SymtabEntry that, IDLID clone)
72  {
73    _module     = that._module;
74    _name       = that._name;
75    _type       = that._type;
76    _typeName   = that._typeName;
77    _sourceFile = that._sourceFile;
78    _info       = that._info;
79    _repID      = (RepositoryID)clone.clone ();
80    ((IDLID)_repID).appendToName (_name);
81    if (that instanceof InterfaceEntry || that instanceof ModuleEntry || that instanceof StructEntry || that instanceof UnionEntry || (that instanceof SequenceEntry && this instanceof SequenceEntry))
82      _container = that;
83    else
84      _container = that._container;
85    initDynamicVars ();
86        _comment = that._comment;       // <21jul1997daz>
87  } // ctor
88
89  /** This is a shallow copy constructor */
90  SymtabEntry (SymtabEntry that)
91  {
92    _module     = that._module;
93    _name       = that._name;
94    _type       = that._type;
95    _typeName   = that._typeName;
96    _sourceFile = that._sourceFile;
97    _info       = that._info;
98    _repID      = (RepositoryID)that._repID.clone ();
99    _container  = that._container;
100
101    if (_type instanceof ForwardEntry)
102      ((ForwardEntry)_type).types.addElement (this);
103
104    initDynamicVars ();
105        // <21JUL1997>
106        _comment = that._comment;
107  } // ctor
108
109  void initDynamicVars ()
110  {
111    _dynamicVars = new Vector (maxKey + 1);
112    for (int i = 0; i <= maxKey; ++i)
113      _dynamicVars.addElement (null);
114  } // initDynamicVars
115
116  /** This is a shallow copy clone */
117  public Object clone ()
118  {
119    return new SymtabEntry (this);
120  } // clone
121
122  /** @return the concatenation of the module and the name, delimited by '/'. */
123  public final String fullName ()
124  {
125    return _module.equals ("") ? _name : _module + '/' + _name;
126  } // fullName
127
128  /** Get the name of this entry's module.  If there are modules within
129      modules, each module name is separated by '/'.
130      @return this entry's module name. */
131  public String module ()
132  {
133    return _module;
134  } // module
135
136  /** Set the module for this entry.
137      @param newName the new name of the module. */
138  public void module (String newName)
139  {
140    if (newName == null)
141      _module = "";
142    else
143      _module = newName;
144  } // module
145
146  /** @return the name of this entry. */
147  public String name ()
148  {
149    return _name;
150  } // name
151
152  /** Set the name.
153      @param newName the new name. */
154  public void name (String newName)
155  {
156    if (newName == null)
157      _name = "";
158    else
159      _name = newName;
160
161    // Update the RepositoryID
162    if (_repID instanceof IDLID)
163      ((IDLID)_repID).replaceName (newName);
164  } // name
165
166  /** @return the type name of this entry. */
167  public String typeName ()
168  {
169    return _typeName;
170  } // typeName
171
172  protected void typeName (String typeName)
173  {
174    _typeName = typeName;
175  } // typeName
176
177  /** @return the type entry of this entry */
178  public SymtabEntry type ()
179  {
180    return _type;
181  } // type
182
183  public void type (SymtabEntry newType)
184  {
185    if (newType == null)
186      typeName ("");
187    else
188      typeName (newType.fullName ());
189    _type = newType;
190
191    if (_type instanceof ForwardEntry)
192      ((ForwardEntry)_type).types.addElement (this);
193  } // type
194
195  /** The file name in which this entry was defined. */
196  public IncludeEntry sourceFile ()
197  {
198    return _sourceFile;
199  } // sourceFile
200
201  /** The file name in which this entry was defined. */
202  public void sourceFile (IncludeEntry file)
203  {
204    _sourceFile = file;
205  } // sourceFile
206
207  /** This must be either an InterfaceEntry or a ModuleEntry.
208      It can be nothing else. */
209  public SymtabEntry container ()
210  {
211    return _container;
212  } // container
213
214  /** This must be either an InterfaceEntry or a ModuleEntry.
215      It can be nothing else. */
216  public void container (SymtabEntry newContainer)
217  {
218    if (newContainer instanceof InterfaceEntry || newContainer instanceof ModuleEntry)
219      _container = newContainer;
220  } // container
221
222  /** @return the repository ID for this entry. */
223  public RepositoryID repositoryID ()
224  {
225    return _repID;
226  } // repositoryID
227
228  /** Set the repository ID for this entry.
229      @param id the new repository ID. */
230  public void repositoryID (RepositoryID id)
231  {
232    _repID = id;
233  } // repositoryID
234
235  /** Should this type be emitted? */
236  public boolean emit ()
237  {
238    return _emit && _isReferencable ;
239  } // emit
240
241  public void emit (boolean emit)
242  {
243    _emit = emit;
244  } // emit
245
246  /* <21jul1997daz> Accessors for comment */
247
248  public Comment comment()
249  {
250    return _comment;
251  }
252
253  public void comment( Comment comment )
254  {
255    _comment = comment;
256  }
257
258  public boolean isReferencable()
259  {
260    return _isReferencable ;
261  }
262
263  public void isReferencable( boolean value )
264  {
265    _isReferencable = value ;
266  }
267
268  static Stack includeStack = new Stack ();
269
270  static void enteringInclude ()
271  {
272    includeStack.push (new Boolean (setEmit));
273    setEmit = false;
274  } // enteringInclude
275
276  static void exitingInclude ()
277  {
278    setEmit = ((Boolean)includeStack.pop ()).booleanValue ();
279  } // exitingInclude
280
281  /** Other variables besides the default ones can be dynamically placed
282      into SymTabEntry (and therefore on all symbol table entries) by
283      extenders.  Before such a variable can exist, its key must be
284      obtained by calling getVariableKey. */
285  public static int getVariableKey ()
286  {
287    return ++maxKey;
288  } // dynamicVariable
289
290  /** Other variables besides the default ones can be dynamically placed
291      into SymTabEntry (and therefore on all symbol table entries) by
292      extenders.  This method assigns the value to the variable of the
293      given key.  A valid key must be obtained by calling the method
294      getVariableKey.  If the key is invalid, NoSuchFieldException is
295      thrown. */
296  public void dynamicVariable (int key, Object value) throws NoSuchFieldException
297  {
298    if (key > maxKey)
299      throw new NoSuchFieldException (Integer.toString (key));
300    else
301    {
302      if (key >= _dynamicVars.size ())
303        growVars ();
304      _dynamicVars.setElementAt (value, key);
305    }
306  } // dynamicVariable
307
308  /** Other variables besides the default ones can be dynamically placed
309      into SymTabEntry (and therefore on all symbol table entries) by
310      extenders.  This method gets the value of the variable of the
311      given key.  A valid key must be obtained by calling the method
312      getVariableKey.  If the key is invalid, NoSuchFieldException is
313      thrown. */
314  public Object dynamicVariable (int key) throws NoSuchFieldException
315  {
316    if (key > maxKey)
317      throw new NoSuchFieldException (Integer.toString (key));
318    else
319    {
320      if (key >= _dynamicVars.size ())
321        growVars ();
322      return _dynamicVars.elementAt (key);
323    }
324  } // dynamicVariable
325
326  void growVars ()
327  {
328    int diff = maxKey - _dynamicVars.size () + 1;
329    for (int i = 0; i < diff; ++i)
330      _dynamicVars.addElement (null);
331  } // growVars
332
333  /** Invoke a generator.  A call to this method is only meaningful
334      for subclasses of SymtabEntry.  If called on this class, it
335      is a no-op.
336      @param symbolTable the symbol table is a hash table whose key is
337       a fully qualified type name and whose value is a SymtabEntry or
338       a subclass of SymtabEntry.
339      @param stream the stream to which the generator should sent its output. */
340  public void generate (Hashtable symbolTable, PrintWriter stream)
341  {
342  } // generate
343
344  /** Access a generator.  A call to this method is only meaningful
345      for subclasses of SymtabEntry.  If called on this class, it
346      is a no-op.
347      @return an object which implements the Generator interface. */
348  public Generator generator ()
349  {
350    return null;
351  } // generator
352
353          static boolean setEmit   = true;
354          static int   maxKey      = -1;
355
356  private SymtabEntry  _container  = null;
357  private String       _module     = "";
358  private String       _name       = "";
359  private String       _typeName   = "";
360  private SymtabEntry  _type       = null;
361  private IncludeEntry _sourceFile = null;
362  private Object       _info       = null;
363  private RepositoryID _repID      = new IDLID ("", "", "1.0");
364  private boolean      _emit       = setEmit;
365  private Comment      _comment    = null;
366  private Vector       _dynamicVars;
367  private boolean      _isReferencable = true ;
368} // class SymtabEntry
369
370/*=======================================================================================
371  DATE<AUTHOR>   ACTION
372  ---------------------------------------------------------------------------------------
373  21jul1997<daz> Added _comment data member to afford transferring comments from source
374                 file to target; added acessor methods for comment.
375  =======================================================================================*/
376