01: /*
02: * xtc - The eXTensible Compiler
03: * Copyright (C) 2006-2007 Robert Grimm
04: *
05: * This program is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU General Public License
07: * version 2 as published by the Free Software Foundation.
08: *
09: * This program is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU General Public License for more details.
13: *
14: * You should have received a copy of the GNU General Public License
15: * along with this program; if not, write to the Free Software
16: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17: * USA.
18: */
19: package xtc.type;
20:
21: import java.io.IOException;
22:
23: /**
24: * Representation of a field reference.
25: *
26: * @author Robert Grimm
27: * @version $Revision: 1.6 $
28: */
29: public class FieldReference extends RelativeReference {
30:
31: /** The field name. */
32: private final String name;
33:
34: /**
35: * Create a new field reference. The specified base reference must
36: * have a struct or union type with a member of the specified name.
37: * The type of the newly created field reference is the named
38: * member's type, unless that type is an array, in which case the
39: * type is the array's element type.
40: *
41: * @param base The base reference.
42: * @param name The member name.
43: * @throws IllegalArgumentException Signals that the base reference
44: * does not have a struct/union with the named member.
45: */
46: public FieldReference(Reference base, String name) {
47: super (base.type, base);
48: this .name = name;
49:
50: // Update the type.
51: if (!type.hasStructOrUnion()) {
52: throw new IllegalArgumentException("not a struct/union");
53: }
54:
55: type = type.toTagged().lookup(name).resolve();
56: if (type.isError()) {
57: throw new IllegalArgumentException(
58: "struct/union without member '" + name + "'");
59: }
60: normalize();
61: }
62:
63: public boolean hasField() {
64: return true;
65: }
66:
67: public String getField() {
68: return name;
69: }
70:
71: public int hashCode() {
72: return name.hashCode();
73: }
74:
75: public boolean equals(Object o) {
76: if (this == o)
77: return true;
78: if (!(o instanceof FieldReference))
79: return false;
80: FieldReference other = (FieldReference) o;
81: return this .name.equals(other.name)
82: && this .base.equals(other.base);
83: }
84:
85: public void write(Appendable out) throws IOException {
86: if (base.isPrefix())
87: out.append('(');
88: base.write(out);
89: if (base.isPrefix())
90: out.append(')');
91: out.append('.');
92: out.append(name);
93: }
94:
95: }
|