Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 11cfab8cf624391549394538333842585557c6bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*******************************************************************************
 * Copyright (c) 2005, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    IBM Rational Software - Initial API and implementation 
 *    Markus Schorn (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.c;

import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IBasicType;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.c.ICASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICBasicType;
import org.eclipse.cdt.internal.core.index.IIndexType;

/**
 * @author dsteffle
 */
public class CBasicType implements ICBasicType {
	static public final int IS_LONG = 1;
	static public final int IS_LONGLONG = 1 << 1;
	static public final int IS_SHORT    = 1 << 2;
	static public final int IS_SIGNED   = 1 << 3;
	static public final int IS_UNSIGNED = 1 << 4;
	static public final int IS_COMPLEX  = 1 << 5;
	static public final int IS_IMAGINARY= 1 << 6;
	
	private int type = 0;
	private int qualifiers = 0;
	private IASTExpression value = null;
	
	/**
	 * keep a reference to the declaration specifier so that duplicate information isn't generated.
	 * 
	 * @param sds the simple declaration specifier
	 */
	public CBasicType(ICASTSimpleDeclSpecifier sds) {
		this.type = sds.getType();
		this.qualifiers = ( sds.isLong()    ? CBasicType.IS_LONG  : 0 ) |
		   				  ( sds.isShort()   ? CBasicType.IS_SHORT : 0 ) |
		   				  ( sds.isSigned()  ? CBasicType.IS_SIGNED: 0 ) |
		   				  ( sds.isUnsigned()? CBasicType.IS_UNSIGNED : 0 ) |
						  ( sds.isLongLong()? CBasicType.IS_LONGLONG : 0 ) |
						  ( sds.isComplex() ? CBasicType.IS_COMPLEX : 0 ) |
						  ( sds.isImaginary()?CBasicType.IS_IMAGINARY : 0 );
		
		if( type == IBasicType.t_unspecified ){
			if( (qualifiers & ( IS_COMPLEX | IS_IMAGINARY )) != 0 )
				type = IBasicType.t_float;
			else {
				type = IBasicType.t_int;
			}
		}
	}
	
	public CBasicType( int type, int qualifiers ){
		this.type = type;
		this.qualifiers = qualifiers;
		
		if( type == IBasicType.t_unspecified ){
			if( (qualifiers & ( IS_COMPLEX | IS_IMAGINARY )) != 0 )
				type = IBasicType.t_float;
			else {
				type = IBasicType.t_int;
			}
		}
	}
	
	public CBasicType( int type, int qualifiers, IASTExpression value ){
		this.type = type;
		this.qualifiers = qualifiers;
		this.value = value;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.IBasicType#getType()
	 */
	public int getType() {
		return type;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.IBasicType#isSigned()
	 */
	public boolean isSigned() {
		return ( qualifiers & IS_SIGNED) != 0;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.IBasicType#isUnsigned()
	 */
	public boolean isUnsigned() {
		return ( qualifiers & IS_UNSIGNED) != 0;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.IBasicType#isShort()
	 */
	public boolean isShort() {
		return ( qualifiers & IS_SHORT) != 0;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.IBasicType#isLong()
	 */
	public boolean isLong() {
		return ( qualifiers & IS_LONG) != 0;
	}

	public boolean isLongLong() {
		return ( qualifiers & IS_LONGLONG) != 0;
	}

	public boolean isSameType(IType obj) {
	    if( obj == this )
	        return true;
	    if( obj instanceof ITypedef || obj instanceof IIndexType)
	        return obj.isSameType( this );
	    
		if (!(obj instanceof CBasicType)) return false;
		
		CBasicType cObj = (CBasicType)obj;
		
		return (cObj.getType() == this.getType()
				&& cObj.isLong() == this.isLong() 
				&& cObj.isShort() == this.isShort() 
				&& cObj.isSigned() == this.isSigned() 
				&& cObj.isUnsigned() == this.isUnsigned()
				&& cObj.isLongLong() == this.isLongLong()
				&& cObj.isComplex() == this.isComplex() 
				&& cObj.isImaginary() == this.isImaginary());
	}
	
    @Override
	public Object clone(){
        IType t = null;
   		try {
            t = (IType) super.clone();
        } catch ( CloneNotSupportedException e ) {
            //not going to happen
        }
        return t;
    }

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.IBasicType#getValue()
	 */
	public IASTExpression getValue() {
		return value;
	}
	
	public void setValue( IASTExpression expression ){
		this.value = expression;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.c.ICBasicType#isComplex()
	 */
	public boolean isComplex() {
		return ( qualifiers & IS_COMPLEX) != 0;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.c.ICBasicType#isImaginary()
	 */
	public boolean isImaginary() {
		return ( qualifiers & IS_IMAGINARY) != 0;
	}
}

Back to the top