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: import java.io.PrintWriter;
024: import java.util.*;
025:
026: /**
027: * @author Gerald Brose
028: * @version $Id: Member.java,v 1.32 2006/06/26 14:37:44 alphonse.bendt Exp $
029: */
030: public class Member extends Declaration {
031: public TypeSpec type_spec;
032:
033: /**
034: this list initially set by the parser but later flattened so that
035: each member only has a single declarator. The list will be null after
036: calling parse()!
037: */
038: SymbolList declarators;
039:
040: public Vector extendVector;
041: public TypeDeclaration containingType;
042:
043: public Declarator declarator;
044:
045: public Member(int num) {
046: super (num);
047: }
048:
049: public void setPackage(String s) {
050: s = parser.pack_replace(s);
051: if (pack_name.length() > 0)
052: pack_name = s + "." + pack_name;
053: else
054: pack_name = s;
055:
056: type_spec.setPackage(s);
057: if (declarators != null)
058: declarators.setPackage(s);
059: }
060:
061: public void setEnclosingSymbol(IdlSymbol s) {
062: enclosing_symbol = s;
063: type_spec.setEnclosingSymbol(s);
064: for (Enumeration e = declarators.v.elements(); e
065: .hasMoreElements();)
066: ((Declarator) e.nextElement()).setEnclosingSymbol(s);
067: }
068:
069: public void setContainingType(TypeDeclaration t) {
070: containingType = t;
071: }
072:
073: /**
074: * must be set by MemberList before parsing
075: */
076: public void setExtendVector(Vector v) {
077: extendVector = v;
078: }
079:
080: /**
081: * Creates a new Member that is similar to this one,
082: * but only for declarator d.
083: */
084: public Member extractMember(Declarator d) {
085: Member result = new Member(new_num());
086: result.declarator = d;
087: return result;
088: }
089:
090: /**
091: * Parsing members means creating new members for definitions
092: * with more than one declarator.
093: */
094: public void parse() {
095: boolean clone_and_parse = true;
096:
097: if (extendVector == null) {
098: lexer.restorePosition(myPosition);
099: parser.fatal_error(
100: "Internal Compiler Error: extendVector not set.",
101: token);
102: }
103:
104: if (type_spec.typeSpec() instanceof ScopedName) {
105: token = type_spec.typeSpec().get_token();
106: String name = type_spec.typeSpec().toString();
107:
108: type_spec = ((ScopedName) type_spec.typeSpec())
109: .resolvedTypeSpec();
110: enclosing_symbol.addImportedName(name, type_spec);
111:
112: if (type_spec instanceof AliasTypeSpec) {
113: AliasTypeSpec aliasTS = (AliasTypeSpec) type_spec;
114: TypeSpec originalType = aliasTS.originalType();
115: if (originalType instanceof SequenceType) {
116: SequenceType sequenceTS = (SequenceType) originalType;
117: if (sequenceTS.elementTypeSpec().typeName().equals(
118: containingType.typeName())) {
119: sequenceTS.setRecursive();
120: }
121: }
122: }
123:
124: clone_and_parse = false;
125: if (type_spec instanceof ConstrTypeSpec) {
126: if (((ConstrTypeSpec) type_spec.typeSpec()).c_type_spec instanceof StructType) {
127: if (((ConstrTypeSpec) type_spec.typeSpec()).c_type_spec
128: .typeName().equals(
129: containingType.typeName())) {
130: parser.fatal_error(
131: "Illegal type recursion (use sequence<"
132: + containingType.typeName()
133: + "> instead)", token);
134: }
135: }
136: }
137: } else if (type_spec.typeSpec() instanceof SequenceType) {
138: TypeSpec ts = ((SequenceType) type_spec.typeSpec())
139: .elementTypeSpec().typeSpec();
140: SequenceType seqTs = (SequenceType) type_spec.typeSpec();
141: while (ts instanceof SequenceType) {
142: seqTs = (SequenceType) ts;
143: ts = ((SequenceType) ts.typeSpec()).elementTypeSpec()
144: .typeSpec();
145: }
146:
147: if (ScopedName.isRecursionScope(ts.typeName())) {
148: seqTs.setRecursive();
149: }
150: } else if (type_spec instanceof ConstrTypeSpec) {
151: type_spec.parse();
152: }
153:
154: String tokName = null;
155: if (token != null && token.line_val != null) {
156: tokName = token.line_val.trim();
157: if (tokName.length() == 0) {
158: tokName = null;
159: }
160: }
161:
162: for (Enumeration e = declarators.v.elements(); e
163: .hasMoreElements();) {
164: Declarator declarator = (Declarator) e.nextElement();
165: String declaratorName = declarator.name();
166:
167: if (tokName != null) {
168: if (org.jacorb.idl.parser.strict_names) {
169: // check for name clashes strictly (i.e. case insensitive)
170: if (declaratorName.equalsIgnoreCase(tokName)) {
171: parser.fatal_error("Declarator "
172: + declaratorName
173: + " already defined in scope.", token);
174: }
175: } else {
176: // check for name clashes only loosely (i.e. case sensitive)
177: if (declaratorName.equals(tokName)) {
178: parser.fatal_error("Declarator "
179: + declaratorName
180: + " already defined in scope.", token);
181: }
182: }
183: }
184:
185: // we don't parse the declarator itself
186: // as that would result in its name getting defined
187: // we define the declarator's name as a type name indirectly
188: // through the cloned type specs.
189:
190: Member m = extractMember(declarator);
191:
192: TypeSpec ts = type_spec.typeSpec();
193:
194: /* create a separate type spec copy for every declarator
195: if the type spec is a new type definition, i.e. a
196: struct, enum, union, sequence or the declarator is
197: an array declarator
198: */
199:
200: if (clone_and_parse
201: || declarator.d instanceof ArrayDeclarator) {
202: /* arrays need special treatment */
203:
204: if (declarator.d instanceof ArrayDeclarator) {
205: ts = new ArrayTypeSpec(new_num(), ts,
206: (ArrayDeclarator) declarator.d, pack_name);
207: ts.parse();
208: } else if (!(ts instanceof BaseType)) {
209: ts = (TypeSpec) ts.clone();
210: if (!(ts instanceof ConstrTypeSpec)) {
211: ts.set_name(declarator.name());
212: }
213:
214: /* important: only parse type specs once (we do it for the last
215: declarator only) */
216: if (!e.hasMoreElements()) {
217: ts.parse();
218: }
219: }
220: }
221:
222: // else
223: if (!(declarator.d instanceof ArrayDeclarator)) {
224: try {
225: NameTable.define(containingType + "."
226: + declarator.name(), "declarator");
227: } catch (NameAlreadyDefined nad) {
228: parser.fatal_error("Declarator "
229: + declarator.name()
230: + " already defined in scope.", token);
231: }
232: }
233:
234: /* if the type spec is a scoped name, it is already parsed and
235: * the type name is defined
236: */
237: m.type_spec = ts;
238: m.pack_name = this .pack_name;
239: m.name = this .name;
240: extendVector.addElement(m);
241: }
242: declarators = null;
243: }
244:
245: /**
246: *
247: */
248:
249: public void print(PrintWriter ps) {
250: member_print(ps, "\tpublic ");
251: }
252:
253: /**
254: *
255: */
256:
257: public void member_print(PrintWriter ps, String prefix) {
258: /* only print members that are not interfaces */
259:
260: if ((type_spec.typeSpec() instanceof ConstrTypeSpec && !((((ConstrTypeSpec) type_spec
261: .typeSpec()).c_type_spec.declaration() instanceof Interface) || (((ConstrTypeSpec) type_spec
262: .typeSpec()).c_type_spec.declaration() instanceof Value)))
263: || type_spec.typeSpec() instanceof SequenceType
264: || type_spec.typeSpec() instanceof ArrayTypeSpec) {
265: type_spec.print(ps);
266: }
267:
268: if (type_spec.typeSpec() instanceof StringType) {
269: ps.print(prefix + type_spec.toString() + " "
270: + declarator.toString() + " = \"\";");
271: } else {
272: ps.print(prefix + type_spec.toString() + " "
273: + declarator.toString() + ";");
274: }
275: }
276:
277: public TypeSpec typeSpec() {
278: return type_spec.typeSpec();
279: }
280: }
|