Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1ea5e20d6de9641002eef534f6e0eec5a02e97c2 (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*******************************************************************************
 * Copyright (c) 2004, 2010 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:
 *     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.ILinkage;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IEnumerator;
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.IValue;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTEnumerationSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateId;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBlockScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPEnumeration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPScope;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexBinding;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.Linkage;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.core.runtime.PlatformObject;

/**
 * Enumerations in C++
 */
public class CPPEnumeration extends PlatformObject implements ICPPEnumeration, ICPPInternalBinding {
	private static final IASTName NOT_INITIALIZED = CPPASTName.NOT_INITIALIZED;
	private static final IEnumerator[] EMPTY_ENUMERATORS = {};

	private final boolean fIsScoped;
	private final IType fFixedType;
	private IASTName fDefinition= NOT_INITIALIZED;
    private IASTName[] fDeclarations= IASTName.EMPTY_NAME_ARRAY;
	private Long fMaxValue;
	private Long fMinValue;

	private ICPPEnumeration fIndexBinding= null;
	private boolean fSearchedIndex= false;

    public CPPEnumeration(ICPPASTEnumerationSpecifier spec, IType fixedType) {
        final IASTName name = spec.getName();
        fIsScoped= spec.isScoped();
        fFixedType= fixedType;
        if (spec.isOpaque()) {
        	addDeclaration(name);
        } else {
        	addDefinition(name);
        }
		name.setBinding(this);
    }

    @Override
	public IASTNode[] getDeclarations() {
    	fDeclarations= ArrayUtil.trim(fDeclarations);
    	return fDeclarations;
    }

    private class FindDefinitionAction extends ASTVisitor {
		private char[] nameArray = CPPEnumeration.this.getNameCharArray();
		{
			shouldVisitNames          = true;
			shouldVisitDeclarations   = true;
			shouldVisitDeclSpecifiers = true;
			shouldVisitDeclarators    = true;
		}

		@Override
		public int visit(IASTName name) {
			if (name instanceof ICPPASTTemplateId || name instanceof ICPPASTQualifiedName)
				return PROCESS_SKIP;
			char[] c = name.getLookupKey();
			final IASTNode parent = name.getParent();
			if (parent instanceof ICPPASTEnumerationSpecifier &&
					!((ICPPASTEnumerationSpecifier) parent).isOpaque() && 
					CharArrayUtils.equals(c, nameArray)) {
				IBinding binding = name.resolveBinding();
				if (binding == CPPEnumeration.this && getDefinition() == name) {
					return PROCESS_ABORT;
				}
			}
			return PROCESS_CONTINUE; 
		}

		@Override
		public int visit(IASTDeclaration declaration) { 
			if (declaration instanceof IASTSimpleDeclaration)
				return PROCESS_CONTINUE;
			return PROCESS_SKIP; 
		}
		@Override
		public int visit(IASTDeclSpecifier declSpec) {
			return (declSpec instanceof ICPPASTEnumerationSpecifier) ? PROCESS_CONTINUE : PROCESS_SKIP; 
		}
		@Override
		public int visit(IASTDeclarator declarator) {
			return PROCESS_SKIP;
		}
	}

    @Override
	public IASTName getDefinition() {
    	if (fDefinition == NOT_INITIALIZED)
    		return null;
        return fDefinition;
    }

    @Override
	public String getName() {
        return new String(getNameCharArray());
    }

    @Override
	public char[] getNameCharArray() {
    	return getADeclaration().getSimpleID();
    }

	private IASTName getADeclaration() {
    	if (fDefinition != null && fDefinition != NOT_INITIALIZED)
    		return fDefinition;
    	return fDeclarations[0];
	}

	@Override
	public IScope getScope() {
        return CPPVisitor.getContainingScope(getADeclaration());
    }

    @Override
	public Object clone() {
    	throw new IllegalArgumentException("Enums must not be cloned"); //$NON-NLS-1$
    }

    @Override
	public String[] getQualifiedName() {
        return CPPVisitor.getQualifiedName(this);
    }

    @Override
	public char[][] getQualifiedNameCharArray() {
        return CPPVisitor.getQualifiedNameCharArray(this);
    }

    @Override
	public boolean isGloballyQualified() throws DOMException {
        IScope scope = getScope();
        while (scope != null) {
            if (scope instanceof ICPPBlockScope)
                return false;
            scope = scope.getParent();
        }
        return true;
    }

	@Override
	public void addDefinition(IASTNode node) {
		assert fDefinition == null || fDefinition == NOT_INITIALIZED;
		fDefinition= (IASTName) node;
	}

	@Override
	public void addDeclaration(IASTNode node) {
		assert node instanceof IASTName;
		if (fDeclarations == null) {
			fDeclarations= new IASTName[] {(IASTName) node};
		} else {
			fDeclarations= ArrayUtil.append(fDeclarations, (IASTName) node);
		}
	}
	
    @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.CPP_LINKAGE;
	}

	@Override
	public IBinding getOwner() {
		return CPPVisitor.findDeclarationOwner(getADeclaration(), true);
	}

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

		long minValue = Long.MAX_VALUE;
		IEnumerator[] enumerators = getEnumerators();
		for (IEnumerator enumerator : enumerators) {
			IValue value = enumerator.getValue();
			if (value != null) {
				Long val = value.numericalValue();
				if (val != null) {
					long v = val.longValue();
					if (v < minValue) {
						minValue = v;
					}
				}
			}
		}
		fMinValue= minValue;
		return minValue;
	}

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

		long maxValue = Long.MIN_VALUE;
		IEnumerator[] enumerators = getEnumerators();
		for (IEnumerator enumerator : enumerators) {
			IValue value = enumerator.getValue();
			if (value != null) {
				Long val = value.numericalValue();
				if (val != null) {
					long v = val.longValue();
					if (v > maxValue) {
						maxValue = v;
					}
				}
			}
		}
		fMaxValue= maxValue;
		return maxValue;
	}

	@Override
	public boolean isScoped() {
		return fIsScoped;
	}

	@Override
	public IType getFixedType() {
		return fFixedType;
	}

    @Override
	public IEnumerator[] getEnumerators() {
    	findDefinition();
    	final IASTName definition = getDefinition();
		if (definition == null) {
			ICPPEnumeration typeInIndex= getIndexBinding();
			if (typeInIndex != null) {
				try {
					return typeInIndex.getEnumerators();
				} catch (DOMException e) {
				}
			}
    		return EMPTY_ENUMERATORS;
    	}

		IASTEnumerator[] enums = ((IASTEnumerationSpecifier) definition.getParent()).getEnumerators();
        IEnumerator[] bindings = new IEnumerator[enums.length];
        for (int i = 0; i < enums.length; i++) {
            bindings[i] = (IEnumerator) enums[i].getName().resolveBinding();
        }
        return bindings;
    }

	private ICPPEnumeration getIndexBinding() {
		if (!fSearchedIndex) {
			final IASTTranslationUnit translationUnit = getADeclaration().getTranslationUnit();
			IIndex index= translationUnit.getIndex();
			if (index != null) {
				fIndexBinding= (ICPPEnumeration) index.adaptBinding(this);
			}
		}
		return fIndexBinding;
	}

	@Override
	public ICPPScope asScope() {
		findDefinition();
		IASTName def = getDefinition();
		if (def == null) {
			ICPPEnumeration indexBinding= getIndexBinding();
			if (indexBinding != null) {
				return indexBinding.asScope();
			}
			def= getADeclaration();
		}
		return ((ICPPASTEnumerationSpecifier) def.getParent()).getScope();
	}

	private void findDefinition() {
    	if (fDefinition == NOT_INITIALIZED) {
    		FindDefinitionAction action = new FindDefinitionAction();
    		IASTNode node = CPPVisitor.getContainingBlockItem(getADeclaration()).getParent();
    		node.accept(action);
    	}
	}
}

Back to the top