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