001: package org.jacorb.idl;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2004 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: import java.io.File;
024: import java.io.PrintWriter;
025: import java.util.*;
026:
027: /**
028: * @author Andre Spiegel
029: * @author Gerald Brose
030: * @version $Id: ValueAbsDecl.java,v 1.26 2006/07/13 08:48:28 nick.cross Exp $
031: *
032: * This class is basically the same as Interface.java, but we can't extend
033: * that one because we have to extend Value, and delegating some parts and
034: * not others is a nuisance...
035: */
036: public class ValueAbsDecl extends Value {
037: ValueBody body = null;
038: ValueInheritanceSpec inheritanceSpec;
039:
040: public ValueAbsDecl(int num) {
041: super (num);
042: pack_name = "";
043: }
044:
045: public void setPackage(String s) {
046: s = parser.pack_replace(s);
047: if (pack_name.length() > 0) {
048: pack_name = s + "." + pack_name;
049: } else {
050: pack_name = s;
051: }
052:
053: if (body != null) // could've been a forward declaration)
054: {
055: body.setPackage(s); // a new scope!
056: }
057:
058: if (inheritanceSpec != null) {
059: inheritanceSpec.setPackage(s);
060: }
061: }
062:
063: public void setInheritanceSpec(ValueInheritanceSpec spec) {
064: inheritanceSpec = spec;
065: }
066:
067: public ValueInheritanceSpec setInheritanceSpec() {
068: return inheritanceSpec;
069: }
070:
071: public TypeDeclaration declaration() {
072: return this ;
073: };
074:
075: public String typeName() {
076: return full_name();
077: }
078:
079: public Object clone() {
080: throw new RuntimeException("Don't clone me, i am an interface!");
081: }
082:
083: public void setEnclosingSymbol(IdlSymbol s) {
084: if (enclosing_symbol != null && enclosing_symbol != s) {
085: logger.error("was " + enclosing_symbol.getClass().getName()
086: + " now: " + s.getClass().getName());
087: throw new RuntimeException(
088: "Compiler Error: trying to reassign container for "
089: + name);
090: }
091: enclosing_symbol = s;
092: }
093:
094: public boolean basic() {
095: return true;
096: }
097:
098: public String holderName() {
099: return javaName() + "Holder";
100: }
101:
102: public String helperName() throws NoHelperException {
103: throw new NoHelperException();
104: }
105:
106: public String toString() {
107: return getFullName(typeName());
108: }
109:
110: public void set_included(boolean i) {
111: included = i;
112: }
113:
114: public void parse() {
115: boolean justAnotherOne = false;
116:
117: escapeName();
118:
119: ConstrTypeSpec ctspec = new ConstrTypeSpec(new_num());
120: try {
121: ScopedName.definePseudoScope(full_name());
122: ctspec.c_type_spec = this ;
123:
124: NameTable.define(full_name(), "type");
125: TypeMap.typedef(full_name(), ctspec);
126: } catch (IllegalRedefinition ill) {
127: parser.fatal_error("Cannot redefine " + token.str_val
128: + " in nested scope as " + ill.newDef, token);
129: } catch (NameAlreadyDefined nad) {
130: // if we get here, there is already a type spec for this interface
131: // in the global type table for a forward declaration of this
132: // interface. We must replace that table entry with this type spec
133: // if this is not yet another forwad declaration
134:
135: if (body != null) {
136: justAnotherOne = true;
137: }
138:
139: if (!full_name().equals("org.omg.CORBA.TypeCode")
140: && body != null) {
141: TypeMap.replaceForwardDeclaration(full_name(), ctspec);
142: }
143: }
144:
145: if (body != null) {
146: if (inheritanceSpec != null && inheritanceSpec.v.size() > 0) {
147: if (logger.isDebugEnabled())
148: logger.debug("Checking inheritanceSpec of "
149: + full_name());
150: for (Enumeration e = inheritanceSpec.v.elements(); e
151: .hasMoreElements();) {
152: ScopedName name = (ScopedName) e.nextElement();
153:
154: TypeSpec resolvedTSpec = name.resolvedTypeSpec();
155:
156: // struct checking can be turned off to tolerate strange
157: // typedefs generated by rmic
158: if (parser.strict_inheritance) {
159: // unwind any typedef's interface names
160: while (resolvedTSpec instanceof AliasTypeSpec) {
161: resolvedTSpec = ((AliasTypeSpec) resolvedTSpec)
162: .originalType();
163: }
164:
165: if (!(resolvedTSpec instanceof ConstrTypeSpec)) {
166: if (logger.isDebugEnabled()) {
167: logger
168: .debug("Illegal inheritance spec, not a constr. type but "
169: + resolvedTSpec
170: .getClass()
171: + ", name " + name);
172: }
173: parser.fatal_error(
174: "Illegal inheritance spec (not a constr. type): "
175: + inheritanceSpec, token);
176: }
177:
178: ConstrTypeSpec ts = (ConstrTypeSpec) resolvedTSpec;
179: if (!(ts.declaration() instanceof Interface)
180: && !(ts.declaration() instanceof ValueAbsDecl)) {
181: parser.fatal_error(
182: "Illegal inheritance spec (not an intf. or abs. value type): "
183: + inheritanceSpec, token);
184: }
185: }
186: }
187: body.set_ancestors(inheritanceSpec);
188: }
189: body.parse();
190: NameTable.parsed_interfaces.put(full_name(), "");
191: } else if (!justAnotherOne) {
192: // i am forward declared, must set myself as
193: // pending further parsing
194: parser.set_pending(full_name());
195: }
196: }
197:
198: ValueBody getBody() {
199: if (parser.get_pending(full_name()) != null) {
200: parser.fatal_error(full_name()
201: + " is forward declared and still pending!", token);
202: } else if (body == null) {
203: if (((ValueAbsDecl) ((ConstrTypeSpec) TypeMap
204: .map(full_name())).c_type_spec) != this ) {
205: body = ((ValueAbsDecl) ((ConstrTypeSpec) TypeMap
206: .map(full_name())).c_type_spec).getBody();
207: }
208: if (body == null) {
209: parser.fatal_error(full_name()
210: + " still has an empty body!", token);
211: }
212: }
213: return body;
214: }
215:
216: public String getTypeCodeExpression() {
217: return this .getTypeCodeExpression(new HashSet());
218: }
219:
220: public String getTypeCodeExpression(Set knownTypes) {
221: if (knownTypes.contains(this )) {
222: return this .getRecursiveTypeCodeExpression();
223: }
224:
225: knownTypes.add(this );
226:
227: return "org.omg.CORBA.ORB.init().create_value_tc(\"" + id()
228: + "\", \"" + name
229: + "\", org.omg.CORBA.VM_ABSTRACT.value "
230: + ", null, null)";
231: }
232:
233: public String printReadExpression(String streamname) {
234: return "(" + javaName() + ")"
235: + "((org.omg.CORBA_2_3.portable.InputStream)"
236: + streamname + ")" + ".read_value (\"" + id() + "\")";
237: }
238:
239: public String printReadStatement(String var_name, String streamname) {
240: return var_name + " = " + printReadExpression(streamname);
241: }
242:
243: public String printWriteStatement(String var_name, String streamname) {
244: return "((org.omg.CORBA_2_3.portable.OutputStream)"
245: + streamname + ")" + ".write_value (" + var_name + ");";
246: }
247:
248: /**
249: * generate the mapped class that extends ValueBase and has the
250: * operations and attributes
251: */
252:
253: public void print(PrintWriter unused) {
254: if (included && !generateIncluded()) {
255: return;
256: }
257:
258: // divert output into class files
259: if (body != null) // forward declaration
260: {
261: try {
262: // Java Interface file
263:
264: String path = parser.out_dir + fileSeparator
265: + pack_name.replace('.', fileSeparator);
266: File dir = new File(path);
267: if (!dir.exists()) {
268: if (!dir.mkdirs()) {
269: org.jacorb.idl.parser.fatal_error(
270: "Unable to create " + path, null);
271: }
272: }
273:
274: File f = new File(dir, name + ".java");
275:
276: if (GlobalInputStream.isMoreRecentThan(f)) {
277: PrintWriter ps = new PrintWriter(
278: new java.io.FileWriter(f));
279:
280: if (Environment.JAVA14 && pack_name.equals(""))
281: lexer.emit_warn("No package defined for "
282: + name + " - illegal in JDK1.4", token);
283: if (!pack_name.equals(""))
284: ps.println("package " + pack_name + ";\n");
285:
286: printClassComment("abstract value type", name, ps);
287:
288: // do we inherit from a class in the unnamed package?
289: // if so, we have to import this class explicitly
290:
291: if (inheritanceSpec != null
292: && inheritanceSpec.v.size() > 0) {
293: Enumeration e = inheritanceSpec.v.elements();
294: for (; e.hasMoreElements();) {
295: ScopedName sn = (ScopedName) e
296: .nextElement();
297: if (sn.resolvedName().indexOf('.') < 0) {
298: ps.println("import " + sn
299: + "Operations;");
300: }
301: }
302: }
303: printImport(ps);
304:
305: ps.println("public interface " + name);
306: ps
307: .print("\textends org.omg.CORBA.portable.ValueBase ");
308:
309: if (inheritanceSpec != null
310: && inheritanceSpec.v.size() > 0) {
311: for (Enumeration e = inheritanceSpec.v
312: .elements(); e.hasMoreElements();) {
313: ps.print(", " + e.nextElement());
314: }
315: }
316:
317: ps.println("\n{");
318: if (body != null) {
319: // forward declaration
320: body.printOperationSignatures(ps);
321: }
322: ps.println("}");
323: ps.close();
324: }
325: } catch (java.io.IOException i) {
326: throw new RuntimeException("File IO error" + i);
327: }
328: }
329: }
330:
331: public void printInsertIntoAny(PrintWriter ps, String anyname,
332: String varname) {
333: ps.println("\t\t" + anyname + ".insert_Value(" + varname + ", "
334: + varname + "._type());");
335: }
336:
337: public void printExtractResult(PrintWriter ps, String resultname,
338: String anyname, String resulttype) {
339: ps.println("\t\t" + resultname + " = (" + resulttype + ")"
340: + anyname + ".extract_Value();");
341: }
342:
343: public void accept(IDLTreeVisitor visitor) {
344: visitor.visitValue(this);
345: }
346: }
|