1
/*******************************************************************************
2
* Copyright (c) 2004, 2010 IBM Corporation and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Devin Steffler (IBM Corporation) - initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.internal.core.dom.parser.c;
13
import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
14
import org.eclipse.cdt.core.dom.ast.IASTExpression;
15
import org.eclipse.cdt.core.dom.ast.IArrayType;
16
import org.eclipse.cdt.core.dom.ast.IType;
17
import org.eclipse.cdt.core.dom.ast.ITypedef;
18
import org.eclipse.cdt.core.dom.ast.IValue;
19
import org.eclipse.cdt.core.dom.ast.c.ICASTArrayModifier;
20
import org.eclipse.cdt.core.dom.ast.c.ICArrayType;
21
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
22
import org.eclipse.cdt.internal.core.dom.parser.ISerializableType;
23
import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer;
24
import org.eclipse.cdt.internal.core.dom.parser.ITypeMarshalBuffer;
25
import org.eclipse.cdt.internal.core.dom.parser.Value;
26
import org.eclipse.core.runtime.CoreException;
28
public class CArrayType implements ICArrayType, ITypeContainer, ISerializableType {
30
private IASTExpression sizeExpression;
31
private IValue value= Value.NOT_INITIALIZED;
32
private boolean isConst;
33
private boolean isVolatile;
34
private boolean isRestrict;
35
private boolean isStatic;
36
private boolean isVariableSized;
38
public CArrayType(IType type) {
42
public CArrayType(IType type, boolean isConst, boolean isVolatile, boolean isRestrict, IValue size) {
44
this.isConst= isConst;
45
this.isVolatile= isVolatile;
46
this.isRestrict= isRestrict;
50
public void setIsStatic(boolean val) {
53
public void setIsVariableLength(boolean val) {
57
public boolean isSameType(IType obj) {
60
if (obj instanceof ITypedef)
61
return obj.isSameType(this);
62
if (obj instanceof ICArrayType) {
63
ICArrayType at = (ICArrayType) obj;
64
if (isConst() != at.isConst()) return false;
65
if (isRestrict() != at.isRestrict()) return false;
66
if (isStatic() != at.isStatic()) return false;
67
if (isVolatile() != at.isVolatile()) return false;
68
if (isVariableLength() != at.isVariableLength()) return false;
70
return at.getType().isSameType(type) && hasSameSize(at);
75
private boolean hasSameSize(IArrayType rhs) {
76
IValue s1 = getSize();
77
IValue s2 = rhs.getSize();
80
if (s1 == null || s2 == null)
82
return CharArrayUtils.equals(s1.getSignature(), s2.getSignature());
86
* @see org.eclipse.cdt.core.dom.ast.IArrayType#getType()
88
public IType getType() {
92
public void setType(IType t) {
96
public void setModifier(ICASTArrayModifier mod) {
97
isConst= mod.isConst();
98
isVolatile= mod.isVolatile();
99
isRestrict= mod.isRestrict();
100
isStatic= mod.isStatic();
101
isVariableSized= mod.isVariableSized();
102
sizeExpression= mod.getConstantExpression();
105
public boolean isConst() {
109
public boolean isRestrict() {
113
public boolean isVolatile() {
117
public boolean isStatic() {
121
public boolean isVariableLength() {
122
return isVariableSized;
125
public IValue getSize() {
126
if (value != Value.NOT_INITIALIZED)
129
if (sizeExpression == null)
132
return value= Value.create(sizeExpression, Value.MAX_RECURSION_DEPTH);
136
public Object clone() {
139
t = (IType) super.clone();
140
} catch (CloneNotSupportedException e) {
141
// Not going to happen
147
public String toString() {
148
return ASTTypeUtil.getType(this);
152
public void marshal(ITypeMarshalBuffer buffer) throws CoreException {
153
int firstByte= ITypeMarshalBuffer.ARRAY;
158
if (isConst()) flags |= 0x01;
159
if (isVolatile()) flags |= 0x02;
160
if (isRestrict()) flags |= 0x04;
161
if (isStatic()) flags |= 0x08;
162
if (isVariableLength()) flags |= 0x10;
164
firstByte |= ITypeMarshalBuffer.FLAG1;
170
firstByte |= ITypeMarshalBuffer.FLAG2;
171
Long num= val.numericalValue();
174
if (l>=0 && l <= Short.MAX_VALUE) {
176
firstByte |= ITypeMarshalBuffer.FLAG3;
180
buffer.putByte((byte) firstByte);
182
buffer.putByte((byte) flags);
185
buffer.putShort(nval);
186
} else if (val != null) {
187
buffer.marshalValue(val);
189
buffer.marshalType(getType());
192
public static IType unmarshal(int firstByte, ITypeMarshalBuffer buffer) throws CoreException {
195
if ( (firstByte & ITypeMarshalBuffer.FLAG1) != 0) {
196
flags= buffer.getByte();
198
if ((firstByte & ITypeMarshalBuffer.FLAG3) != 0) {
199
value = Value.create(buffer.getShort());
200
} else if ((firstByte & ITypeMarshalBuffer.FLAG2) != 0) {
201
value = buffer.unmarshalValue();
203
IType nested= buffer.unmarshalType();
204
CArrayType result= new CArrayType(nested, (flags & 0x01) != 0, (flags & 0x02) != 0, (flags & 0x04) != 0, value);
205
result.setIsStatic((flags & 0x08) != 0);
206
result.setIsVariableLength((flags & 0x10) != 0);
211
public IASTExpression getArraySizeExpression() {
212
if (sizeExpression != null)
213
return sizeExpression;