1
/*******************************************************************************
2
* Copyright (c) 2004, 2011 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
* Andrew Niefer (IBM Corporation) - initial API and implementation
10
* Markus Schorn (Wind River Systems)
11
*******************************************************************************/
12
package org.eclipse.cdt.internal.core.dom.parser.cpp;
14
import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
15
import org.eclipse.cdt.core.dom.ast.IASTExpression;
16
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
17
import org.eclipse.cdt.core.dom.ast.IBasicType;
18
import org.eclipse.cdt.core.dom.ast.IType;
19
import org.eclipse.cdt.core.dom.ast.ITypedef;
20
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier;
21
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBasicType;
22
import org.eclipse.cdt.internal.core.dom.parser.ISerializableType;
23
import org.eclipse.cdt.internal.core.dom.parser.ITypeMarshalBuffer;
24
import org.eclipse.core.runtime.CoreException;
29
public class CPPBasicType implements ICPPBasicType, ISerializableType {
30
public static final CPPBasicType BOOLEAN = new CPPBasicType(Kind.eBoolean, 0, null);
32
private final Kind fKind;
33
private final int fModifiers;
34
private IASTExpression fExpression;
36
public CPPBasicType(Kind kind, int qualifiers, IASTExpression expression) {
37
if (kind == Kind.eUnspecified) {
38
if ((qualifiers & (IS_COMPLEX | IS_IMAGINARY)) != 0) {
40
} else if ((qualifiers & (IS_LONG | IS_SHORT | IS_SIGNED | IS_UNSIGNED | IS_LONG_LONG)) != 0) {
43
fKind= Kind.eUnspecified;
48
fModifiers= qualifiers;
49
fExpression= expression;
52
public CPPBasicType(Kind kind, int qualifiers) {
53
this(kind, qualifiers, null);
56
public CPPBasicType(ICPPASTSimpleDeclSpecifier sds) {
57
this(getKind(sds), getModifiers(sds), null);
60
private static int getModifiers(ICPPASTSimpleDeclSpecifier sds) {
62
(sds.isLong() ? IBasicType.IS_LONG : 0) |
63
(sds.isShort() ? IBasicType.IS_SHORT : 0) |
64
(sds.isSigned() ? IBasicType.IS_SIGNED: 0) |
65
(sds.isUnsigned() ? IBasicType.IS_UNSIGNED : 0) |
66
(sds.isLongLong() ? IBasicType.IS_LONG_LONG : 0) |
67
(sds.isComplex() ? IBasicType.IS_COMPLEX : 0) |
68
(sds.isImaginary() ? IBasicType.IS_IMAGINARY : 0);
71
private static Kind getKind(ICPPASTSimpleDeclSpecifier sds) {
72
return getKind(sds.getType());
75
static Kind getKind(final int simpleDeclSpecType) {
76
switch (simpleDeclSpecType) {
77
case IASTSimpleDeclSpecifier.t_bool:
79
case IASTSimpleDeclSpecifier.t_char:
81
case IASTSimpleDeclSpecifier.t_wchar_t:
83
case IASTSimpleDeclSpecifier.t_char16_t:
85
case IASTSimpleDeclSpecifier.t_char32_t:
87
case IASTSimpleDeclSpecifier.t_double:
89
case IASTSimpleDeclSpecifier.t_float:
91
case IASTSimpleDeclSpecifier.t_int:
93
case IASTSimpleDeclSpecifier.t_void:
96
return Kind.eUnspecified;
100
public boolean isSameType(IType object) {
104
if (object instanceof ITypedef)
105
return object.isSameType(this);
107
if (!(object instanceof ICPPBasicType))
110
ICPPBasicType t = (ICPPBasicType) object;
111
if (fKind != t.getKind())
114
if (fKind == Kind.eInt) {
115
//signed int and int are equivalent
116
return (fModifiers & ~IS_SIGNED) == (t.getModifiers() & ~IS_SIGNED);
118
return fModifiers == t.getModifiers();
121
public Kind getKind() {
125
public boolean isSigned() {
126
return (fModifiers & IS_SIGNED) != 0;
129
public boolean isUnsigned() {
130
return (fModifiers & IS_UNSIGNED) != 0;
133
public boolean isShort() {
134
return (fModifiers & IS_SHORT) != 0;
137
public boolean isLong() {
138
return (fModifiers & IS_LONG) != 0;
141
public boolean isLongLong() {
142
return (fModifiers & IS_LONG_LONG) != 0;
145
public boolean isComplex() {
146
return (fModifiers & IS_COMPLEX) != 0;
149
public boolean isImaginary() {
150
return (fModifiers & IS_IMAGINARY) != 0;
154
public Object clone() {
157
t = (IType) super.clone();
158
} catch (CloneNotSupportedException e) {
159
//not going to happen
164
public void setFromExpression(IASTExpression val) {
169
* Returns the expression the type was created for, or <code>null</code>.
171
public IASTExpression getCreatedFromExpression() {
175
public int getModifiers() {
180
public String toString() {
181
return ASTTypeUtil.getType(this);
184
public void marshal(ITypeMarshalBuffer buffer) throws CoreException {
185
final int kind= getKind().ordinal();
186
final int shiftedKind= kind * ITypeMarshalBuffer.FLAG1;
187
final int modifiers= getModifiers();
188
if (shiftedKind < ITypeMarshalBuffer.FLAG4 && modifiers == 0) {
189
buffer.putByte((byte) (ITypeMarshalBuffer.BASIC_TYPE | shiftedKind));
191
buffer.putByte((byte) (ITypeMarshalBuffer.BASIC_TYPE | ITypeMarshalBuffer.FLAG4));
192
buffer.putByte((byte) kind);
193
buffer.putByte((byte) modifiers);
197
public static IType unmarshal(int firstByte, ITypeMarshalBuffer buffer) throws CoreException {
198
final boolean dense= (firstByte & ITypeMarshalBuffer.FLAG4) == 0;
202
kind= (firstByte & (ITypeMarshalBuffer.FLAG4-1))/ITypeMarshalBuffer.FLAG1;
204
kind= buffer.getByte();
205
modifiers= buffer.getByte();
207
return new CPPBasicType(Kind.values()[kind], modifiers);
211
public int getQualifierBits() {
212
return getModifiers();
216
public int getType() {
235
return t_unspecified;
237
return t_unspecified;
241
* @deprecated types don't have values
244
public IASTExpression getValue() {