~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPBasicType.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2011-10-06 21:15:04 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111006211504-8dutmljjih0zikfv
Tags: 8.0.1-1
* New upstream release.
* Split the JNI packages into a separate architecture dependent
  package and made eclipse-cdt architecture independent.
* Install JNI libraries into multiarch aware location
* Bumped Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
7
 * 
 
8
 *  Contributors:
 
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;
 
13
 
 
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;
 
25
 
 
26
/**
 
27
 * Built-in c++ type.
 
28
 */
 
29
public class CPPBasicType implements ICPPBasicType, ISerializableType {
 
30
        public static final CPPBasicType BOOLEAN = new CPPBasicType(Kind.eBoolean, 0, null);
 
31
        
 
32
        private final Kind fKind;
 
33
        private final int fModifiers;
 
34
        private IASTExpression fExpression;
 
35
 
 
36
        public CPPBasicType(Kind kind, int qualifiers, IASTExpression expression) {
 
37
                if (kind == Kind.eUnspecified) {
 
38
                        if ((qualifiers & (IS_COMPLEX | IS_IMAGINARY)) != 0) {
 
39
                                fKind= Kind.eFloat;
 
40
                        } else if ((qualifiers & (IS_LONG | IS_SHORT | IS_SIGNED | IS_UNSIGNED | IS_LONG_LONG)) != 0) {
 
41
                                fKind= Kind.eInt;
 
42
                        } else {
 
43
                                fKind= Kind.eUnspecified;
 
44
                        }
 
45
                } else {
 
46
                        fKind= kind;
 
47
                }
 
48
                fModifiers= qualifiers;
 
49
                fExpression= expression;
 
50
        }
 
51
 
 
52
        public CPPBasicType(Kind kind, int qualifiers) {
 
53
                this(kind, qualifiers, null);
 
54
        }
 
55
        
 
56
        public CPPBasicType(ICPPASTSimpleDeclSpecifier sds) {
 
57
                this(getKind(sds), getModifiers(sds), null);
 
58
        }
 
59
        
 
60
        private static int getModifiers(ICPPASTSimpleDeclSpecifier sds) {
 
61
                return
 
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);
 
69
        }
 
70
        
 
71
        private static Kind getKind(ICPPASTSimpleDeclSpecifier sds) {
 
72
                return getKind(sds.getType());
 
73
        }
 
74
 
 
75
        static Kind getKind(final int simpleDeclSpecType) {
 
76
                switch (simpleDeclSpecType) {
 
77
                case IASTSimpleDeclSpecifier.t_bool:
 
78
                        return Kind.eBoolean;
 
79
                case IASTSimpleDeclSpecifier.t_char:
 
80
                        return Kind.eChar;
 
81
                case IASTSimpleDeclSpecifier.t_wchar_t:
 
82
                        return Kind.eWChar;
 
83
                case IASTSimpleDeclSpecifier.t_char16_t:
 
84
                        return Kind.eChar16;
 
85
                case IASTSimpleDeclSpecifier.t_char32_t:
 
86
                        return Kind.eChar32;
 
87
                case IASTSimpleDeclSpecifier.t_double:
 
88
                        return Kind.eDouble;
 
89
                case IASTSimpleDeclSpecifier.t_float:
 
90
                        return Kind.eFloat;
 
91
                case IASTSimpleDeclSpecifier.t_int:
 
92
                        return Kind.eInt;
 
93
                case IASTSimpleDeclSpecifier.t_void:
 
94
                        return Kind.eVoid;
 
95
                default:
 
96
                        return Kind.eUnspecified;
 
97
                }
 
98
        }
 
99
 
 
100
        public boolean isSameType(IType object) {
 
101
                if (object == this)
 
102
                        return true;
 
103
 
 
104
            if (object instanceof ITypedef)
 
105
                return object.isSameType(this);
 
106
 
 
107
                if (!(object instanceof ICPPBasicType))
 
108
                        return false;
 
109
 
 
110
                ICPPBasicType t = (ICPPBasicType) object;
 
111
                if (fKind != t.getKind())
 
112
                        return false;
 
113
 
 
114
                if (fKind == Kind.eInt) {
 
115
                        //signed int and int are equivalent
 
116
                        return (fModifiers & ~IS_SIGNED) == (t.getModifiers() & ~IS_SIGNED);
 
117
                }
 
118
                return fModifiers == t.getModifiers();
 
119
        }
 
120
 
 
121
        public Kind getKind() {
 
122
                return fKind;
 
123
        }
 
124
        
 
125
        public boolean isSigned() {
 
126
                return (fModifiers & IS_SIGNED) != 0;
 
127
        }
 
128
 
 
129
        public boolean isUnsigned() {
 
130
                return (fModifiers & IS_UNSIGNED) != 0;
 
131
        }
 
132
 
 
133
        public boolean isShort() {
 
134
                return (fModifiers & IS_SHORT) != 0;
 
135
        }
 
136
 
 
137
        public boolean isLong() {
 
138
                return (fModifiers & IS_LONG) != 0;
 
139
        }
 
140
 
 
141
        public boolean isLongLong() {
 
142
                return (fModifiers & IS_LONG_LONG) != 0;
 
143
        }
 
144
 
 
145
        public boolean isComplex() {
 
146
                return (fModifiers & IS_COMPLEX) != 0;
 
147
        }
 
148
 
 
149
        public boolean isImaginary() {
 
150
                return (fModifiers & IS_IMAGINARY) != 0;
 
151
        }
 
152
 
 
153
        @Override
 
154
        public Object clone() {
 
155
        IType t = null;
 
156
                try {
 
157
            t = (IType) super.clone();
 
158
        } catch (CloneNotSupportedException e) {
 
159
            //not going to happen
 
160
        }
 
161
        return t;
 
162
    }
 
163
 
 
164
        public void setFromExpression(IASTExpression val) {
 
165
                fExpression = val;
 
166
        }
 
167
 
 
168
        /**
 
169
         * Returns the expression the type was created for, or <code>null</code>.
 
170
         */
 
171
        public IASTExpression getCreatedFromExpression() {
 
172
                return fExpression;
 
173
        }
 
174
        
 
175
        public int getModifiers() {
 
176
                return fModifiers;
 
177
        }
 
178
        
 
179
        @Override
 
180
        public String toString() {
 
181
                return ASTTypeUtil.getType(this);
 
182
        }
 
183
        
 
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));
 
190
                } else {
 
191
                        buffer.putByte((byte) (ITypeMarshalBuffer.BASIC_TYPE | ITypeMarshalBuffer.FLAG4));
 
192
                        buffer.putByte((byte) kind);
 
193
                        buffer.putByte((byte) modifiers);
 
194
                } 
 
195
        }
 
196
        
 
197
        public static IType unmarshal(int firstByte, ITypeMarshalBuffer buffer) throws CoreException {
 
198
                final boolean dense= (firstByte & ITypeMarshalBuffer.FLAG4) == 0;
 
199
                int modifiers= 0;
 
200
                int kind;
 
201
                if (dense) {
 
202
                        kind= (firstByte & (ITypeMarshalBuffer.FLAG4-1))/ITypeMarshalBuffer.FLAG1;
 
203
                } else {
 
204
                        kind= buffer.getByte();
 
205
                        modifiers= buffer.getByte();
 
206
                } 
 
207
                return new CPPBasicType(Kind.values()[kind], modifiers);
 
208
        }
 
209
 
 
210
        @Deprecated
 
211
        public int getQualifierBits() {
 
212
                return getModifiers();
 
213
        }
 
214
 
 
215
        @Deprecated
 
216
        public int getType() {
 
217
                switch (fKind) {
 
218
                case eBoolean:
 
219
                        return t_bool;
 
220
                case eChar:
 
221
                case eChar16:
 
222
                case eChar32:
 
223
                        return t_char;
 
224
                case eWChar:
 
225
                        return t_wchar_t;
 
226
                case eDouble:
 
227
                        return t_double;
 
228
                case eFloat:
 
229
                        return t_float;
 
230
                case eInt:
 
231
                        return t_int;
 
232
                case eVoid:
 
233
                        return t_void;
 
234
                case eUnspecified:
 
235
                        return t_unspecified;
 
236
                }
 
237
                return t_unspecified;
 
238
        }
 
239
 
 
240
    /**
 
241
     * @deprecated types don't have values
 
242
     */
 
243
        @Deprecated
 
244
        public IASTExpression getValue() {
 
245
                return fExpression;
 
246
        }
 
247
}