Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 43f0e086f010fed2c2c1fd93b1be573408aac7fd (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*******************************************************************************
 * Copyright (c) 2004, 2013 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)
 *     Nathan Ridge
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.c;

import org.eclipse.cdt.core.dom.ILinkage;
import org.eclipse.cdt.core.dom.ast.ASTNodeProperty;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IEnumeration;
import org.eclipse.cdt.core.dom.ast.IEnumerator;
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.c.ICASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTEnumerationSpecifier;
import org.eclipse.cdt.core.index.IIndexBinding;
import org.eclipse.cdt.internal.core.dom.Linkage;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil;
import org.eclipse.core.runtime.PlatformObject;

/**
 * Binding for enumerations in C.
 */
public class CEnumeration extends PlatformObject implements IEnumeration, ICInternalBinding {
	private IASTName[] declarations = null;
	private IASTName definition = null;
	private Long fMinValue;
	private Long fMaxValue;

	public CEnumeration(IASTName enumeration) {
		ASTNodeProperty prop = enumeration.getPropertyInParent();
		if (prop == IASTElaboratedTypeSpecifier.TYPE_NAME)
			declarations = new IASTName[] { enumeration };
		else
			definition = enumeration;
		enumeration.setBinding(this);
	}

	public void addDeclaration(IASTName decl) {
		if (!decl.isActive())
			return;

		if (decl.getPropertyInParent() != IASTElaboratedTypeSpecifier.TYPE_NAME)
			return;

		decl.setBinding(this);
		if (declarations == null) {
			declarations = new IASTName[] { decl };
			return;
		}
		for (int i = 0; i < declarations.length; i++) {
			if (declarations[i] == null) {
				declarations[i] = decl;
				return;
			}
		}
		IASTName tmp[] = new IASTName[declarations.length * 2];
		System.arraycopy(declarations, 0, tmp, 0, declarations.length);
		tmp[declarations.length] = decl;
		declarations = tmp;
	}

	@Override
	public IASTNode getPhysicalNode() {
		if (definition != null)
			return definition;

		return declarations[0];
	}

	private void checkForDefinition() {
		IASTDeclSpecifier spec = CVisitor.findDefinition((ICASTElaboratedTypeSpecifier) declarations[0].getParent());
		if (spec != null && spec instanceof ICASTEnumerationSpecifier) {
			ICASTEnumerationSpecifier enumSpec = (ICASTEnumerationSpecifier) spec;

			enumSpec.getName().setBinding(this);
			definition = enumSpec.getName();
		}
		return;
	}

	@Override
	public String getName() {
		if (definition != null)
			return definition.toString();

		return declarations[0].toString();
	}

	@Override
	public char[] getNameCharArray() {
		if (definition != null)
			return definition.toCharArray();

		return declarations[0].toCharArray();
	}

	@Override
	public IScope getScope() {
		return CVisitor.getContainingScope(definition != null ? definition : declarations[0].getParent());
	}

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

	@Override
	public IEnumerator[] getEnumerators() {
		if (definition == null) {
			checkForDefinition();
			if (definition == null)
				return new IEnumerator[] { new CEnumerator.CEnumeratorProblem(declarations[0],
						IProblemBinding.SEMANTIC_DEFINITION_NOT_FOUND, declarations[0].toCharArray()) };
		}

		IASTEnumerationSpecifier enumSpec = (IASTEnumerationSpecifier) definition.getParent();
		IASTEnumerationSpecifier.IASTEnumerator[] enums = enumSpec.getEnumerators();
		IEnumerator[] bindings = new IEnumerator[enums.length];

		for (int i = 0; i < enums.length; i++) {
			bindings[i] = (IEnumerator) enums[i].getName().resolveBinding();
		}
		return bindings;
	}

	public void addDefinition(IASTName name) {
		if (name.isActive())
			definition = name;
	}

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

		return false;
	}

	@Override
	public ILinkage getLinkage() {
		return Linkage.C_LINKAGE;
	}

	@Override
	public IASTNode[] getDeclarations() {
		return declarations;
	}

	@Override
	public IASTNode getDefinition() {
		return definition;
	}

	@Override
	public IBinding getOwner() {
		IASTNode node = definition;
		if (node == null && declarations != null && declarations.length > 0) {
			node = declarations[0];
		}
		// either local or global, never part of structs
		return CVisitor.findEnclosingFunction(node);
	}

	@Override
	public String toString() {
		return getName();
	}

	@Override
	public long getMinValue() {
		if (fMinValue != null)
			return fMinValue.longValue();

		long minValue = SemanticUtil.computeMinValue(this);
		fMinValue = minValue;
		return minValue;
	}

	@Override
	public long getMaxValue() {
		if (fMaxValue != null)
			return fMaxValue.longValue();

		long maxValue = SemanticUtil.computeMaxValue(this);
		fMaxValue = maxValue;
		return maxValue;
	}
}

Back to the top