001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1997-2004 Gerald Brose.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Library General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Library General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public
017: * License along with this library; if not, write to the Free
018: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019: */
020:
021: package org.jacorb.idl;
022:
023: /**
024: * @author Gerald Brose
025: * @version $Id: ParamDecl.java,v 1.23 2006/06/26 14:37:45 alphonse.bendt Exp $
026: */
027:
028: import java.io.PrintWriter;
029:
030: public class ParamDecl extends IdlSymbol {
031: public static final int MODE_IN = 1;
032: public static final int MODE_OUT = 2;
033: public static final int MODE_INOUT = 3;
034:
035: public int paramAttribute;
036: public TypeSpec paramTypeSpec;
037: public SimpleDeclarator simple_declarator;
038:
039: public ParamDecl(int num) {
040: super (num);
041: }
042:
043: /**
044: * Constructs a new parameter declaration with the given characteristics.
045: */
046: public ParamDecl(int paramAttribute, TypeSpec paramTypeSpec,
047: SimpleDeclarator simple_declarator) {
048: super (new_num());
049: this .paramAttribute = paramAttribute;
050: this .paramTypeSpec = paramTypeSpec;
051: this .simple_declarator = simple_declarator;
052: }
053:
054: /**
055: * Constructs a new parameter declaration with the given characteristics.
056: */
057: public ParamDecl(int paramAttribute, TypeSpec paramTypeSpec,
058: String name) {
059: super (new_num());
060: this .paramAttribute = paramAttribute;
061: this .paramTypeSpec = paramTypeSpec;
062: this .simple_declarator = new SimpleDeclarator(new_num());
063: this .simple_declarator.name = name;
064: }
065:
066: public void setPackage(String s) {
067: s = parser.pack_replace(s);
068: if (pack_name.length() > 0) {
069: pack_name = s + "." + pack_name;
070: } else {
071: pack_name = s;
072: }
073: paramTypeSpec.setPackage(s);
074: }
075:
076: /**
077: * Returns a new ParamDecl with the same characteristics as this one,
078: * except that its mode is changed to 'in'.
079: */
080: public ParamDecl asIn() {
081: return new ParamDecl(MODE_IN, this .paramTypeSpec,
082: this .simple_declarator);
083: }
084:
085: public void parse() {
086: while (paramTypeSpec.typeSpec() instanceof ScopedName) {
087: TypeSpec ts = ((ScopedName) paramTypeSpec.typeSpec())
088: .resolvedTypeSpec();
089: if (ts != null) {
090: paramTypeSpec = ts;
091: }
092: }
093:
094: if (paramTypeSpec == null) {
095: lexer.restorePosition(myPosition);
096: parser.fatal_error("parameter TypeSpec is null " + name,
097: token);
098: }
099: }
100:
101: public void print(PrintWriter ps) {
102: switch (paramAttribute) {
103: case MODE_IN:
104: ps.print(paramTypeSpec.toString());
105: break;
106: case MODE_OUT:
107: case MODE_INOUT:
108: ps.print(paramTypeSpec.holderName());
109: break;
110: }
111: ps.print(" ");
112: ps.print(simple_declarator);
113: }
114:
115: public String printWriteStatement(String ps) {
116: return printWriteStatement(simple_declarator.toString(), ps);
117: }
118:
119: public String printWriteStatement(String name, String ps) {
120: if (paramAttribute != ParamDecl.MODE_IN) {
121: return paramTypeSpec.typeSpec().printWriteStatement(
122: name + ".value", ps);
123: }
124: return paramTypeSpec.typeSpec().printWriteStatement(name, ps);
125: }
126:
127: public String printReadExpression(String ps) {
128: return paramTypeSpec.typeSpec().printReadExpression(ps);
129: }
130:
131: public void printAddArgumentStatement(PrintWriter ps, String reqname) {
132: String varname = this .simple_declarator.toString();
133: String anyname = "$" + varname;
134: ps.print("\t\torg.omg.CORBA.Any " + anyname + " = " + reqname);
135: switch (this .paramAttribute) {
136: case ParamDecl.MODE_OUT:
137: ps.println(".add_out_arg();");
138: varname = varname + ".value";
139: break;
140: case ParamDecl.MODE_IN:
141: ps.println(".add_in_arg();");
142: break;
143: case ParamDecl.MODE_INOUT:
144: ps.println(".add_inout_arg();");
145: varname = varname + ".value";
146: break;
147: default:
148: throw new ParseException("Wrong parameter declaration");
149: }
150: paramTypeSpec.typeSpec().printInsertIntoAny(ps, anyname,
151: varname);
152: }
153:
154: /**
155: * @param ps
156: */
157: public void printExtractArgumentStatement(PrintWriter ps) {
158: String varname = this .simple_declarator.toString();
159: String anyname = "$" + varname;
160:
161: paramTypeSpec.typeSpec().printExtractResult(ps,
162: varname + ".value", anyname, paramTypeSpec.toString());
163: }
164:
165: public void accept(IDLTreeVisitor visitor) {
166: visitor.visitParamDecl(this);
167: }
168: }
|