TCOffsets.java revision 608:7e06bf1dcb09
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.toJava
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.toJavaPortable;
37
38// NOTES:
39
40import java.util.Enumeration;
41import java.util.Hashtable;
42
43import com.sun.tools.corba.se.idl.*;
44
45// This class is passed through the JavaGenerator.HelperType methods.
46// It is ONLY used when a recursive sequence is detected. ie.
47//
48//   struct S1
49//   {
50//     sequence <S1> others;
51//   };
52
53/**
54 *
55 **/
56public class TCOffsets
57{
58  /**
59   * Return -1 if the given name is not in the list of types.
60   **/
61  public int offset (String name)
62  {
63    Integer value = (Integer)tcs.get (name);
64    return value == null ? -1 : value.intValue ();
65  } // offset
66
67  /**
68   *
69   **/
70  public void set (SymtabEntry entry)
71  {
72    if (entry == null)
73      offset += 8;
74    else
75    {
76      tcs.put (entry.fullName (), new Integer (offset));
77      offset += 4;
78      String repID = Util.stripLeadingUnderscoresFromID (entry.repositoryID ().ID ());
79      if (entry instanceof InterfaceEntry)
80        offset += alignStrLen (repID) + alignStrLen (entry.name ());
81      else if (entry instanceof StructEntry)
82        offset += alignStrLen (repID) + alignStrLen (entry.name ()) + 4;
83      else if (entry instanceof UnionEntry)
84        offset += alignStrLen (repID) + alignStrLen (entry.name ()) + 12;
85      else if (entry instanceof EnumEntry)
86      {
87        offset += alignStrLen (repID) + alignStrLen (entry.name ()) + 4;
88        Enumeration e = ((EnumEntry)entry).elements ().elements ();
89        while (e.hasMoreElements ())
90          offset += alignStrLen ((String)e.nextElement ());
91      }
92      else if (entry instanceof StringEntry)
93        offset += 4;
94      else if (entry instanceof TypedefEntry)
95      {
96        offset += alignStrLen (repID) + alignStrLen (entry.name ());
97        if (((TypedefEntry)entry).arrayInfo ().size () != 0)
98          offset += 8;
99      }
100    }
101  } // set
102
103  /**
104   * Return the full length of the string type:  4 byte length, x bytes for
105   * string + 1 for the null terminator, align it so it ends on a 4-byte
106   * boundary.  This method assumes the string starts at a 4-byte boundary
107   * since it doesn't do any leading alignment.
108   **/
109  public int alignStrLen (String string)
110  {
111    int len = string.length () + 1;
112    int align = 4 - (len % 4);
113    if (align == 4) align = 0;
114    return len + align + 4;
115  } // alignStrLen
116
117  /**
118   *
119   **/
120  public void setMember (SymtabEntry entry)
121  {
122    offset += alignStrLen (entry.name ());
123    if (((TypedefEntry)entry).arrayInfo ().size () != 0)
124      offset += 4;
125  } // setMember
126
127  /**
128   *
129   **/
130  public int currentOffset ()
131  {
132    return offset;
133  } // currentOffset
134
135  /**
136   *
137   **/
138  public void bumpCurrentOffset (int value)
139  {
140    offset += value;
141  } // bumpCurrentOffset
142
143  private Hashtable tcs    = new Hashtable ();
144  private int       offset = 0;
145} // class TCOffsets
146