Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 76346e76e6bb9d4ae2dece16b65310ad4ebb860c (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
/*******************************************************************************
 * Copyright (c) 2004, 2014 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Andrew Niefer (IBM Corporation) - initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp;

import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IArrayType;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.IValue;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.parser.ISerializableType;
import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer;
import org.eclipse.cdt.internal.core.dom.parser.ITypeMarshalBuffer;
import org.eclipse.cdt.internal.core.dom.parser.IntegralValue;
import org.eclipse.cdt.internal.core.dom.parser.ValueFactory;
import org.eclipse.core.runtime.CoreException;

public class CPPArrayType implements IArrayType, ITypeContainer, ISerializableType {
    private IType type;
    private IASTExpression sizeExpression;
    private IValue value= IntegralValue.NOT_INITIALIZED;

    public CPPArrayType(IType type, IValue value) {
    	this.value= value;
    	setType(type);
    }

    public CPPArrayType(IType type, IASTExpression sizeExp) {
    	this.sizeExpression = sizeExp;
    	setType(type);
    }

    @Override
	public IType getType() {
        return type;
    }

    @Override
	public final void setType(IType t) {
    	assert t != null;
        this.type = t;
    }

    @Override
	public boolean isSameType(IType obj) {
        if (obj == this)
            return true;
        if (obj instanceof ITypedef)
            return ((ITypedef) obj).isSameType(this);

        if (obj instanceof IArrayType) {
            final IArrayType rhs = (IArrayType) obj;
			IType objType = rhs.getType();
			if (objType != null) {
				if (objType.isSameType(type)) {
					IValue s1= getSize();
					IValue s2= rhs.getSize();
					if (s1 == s2)
						return true;
					if (s1 == null || s2 == null)
						return false;
					return CharArrayUtils.equals(s1.getSignature(), s2.getSignature());
				}
			}
        }
    	return false;
    }

    @Override
	public IValue getSize() {
    	if (value != IntegralValue.NOT_INITIALIZED)
    		return value;

    	if (sizeExpression == null)
    		return value= null;

    	return value= ValueFactory.create(sizeExpression);
    }

    @Override
    public boolean hasSize() {
    	return value == IntegralValue.NOT_INITIALIZED ? sizeExpression != null : value != null;
    }

    @Override
	@Deprecated
    public IASTExpression getArraySizeExpression() {
        return sizeExpression;
    }

    @Override
	public Object clone() {
        IType t = null;
   		try {
            t = (IType) super.clone();
        } catch (CloneNotSupportedException e) {
            // Not going to happen
        }
        return t;
    }

	@Override
	public String toString() {
		return ASTTypeUtil.getType(this);
	}

	@Override
	public void marshal(ITypeMarshalBuffer buffer) throws CoreException {
		final short firstBytes = ITypeMarshalBuffer.ARRAY_TYPE;

		IValue val= getSize();
		if (val == null) {
			buffer.putShort(firstBytes);
			buffer.marshalType(getType());
			return;
		}

		Number num= val.numberValue();
		if (num != null) {
			long lnum= num.longValue();
			if (lnum >= 0) {
				buffer.putShort((short) (firstBytes | ITypeMarshalBuffer.FLAG1));
				buffer.putLong(lnum);
				buffer.marshalType(getType());
				return;
			}
		}
		buffer.putShort((short) (firstBytes | ITypeMarshalBuffer.FLAG2));
		buffer.marshalValue(val);
		buffer.marshalType(getType());
	}

	public static IType unmarshal(short firstBytes, ITypeMarshalBuffer buffer) throws CoreException {
		IValue value= null;
		if ((firstBytes & ITypeMarshalBuffer.FLAG1) != 0) {
			value = IntegralValue.create(buffer.getLong());
		} else if ((firstBytes & ITypeMarshalBuffer.FLAG2) != 0) {
			value = buffer.unmarshalValue();
		}
		IType nested= buffer.unmarshalType();
		return new CPPArrayType(nested, value);
	}
}

Back to the top