Skip to main content
summaryrefslogtreecommitdiffstats
blob: 103c6ca0431a9d58610a814c861f2ca71e12a7f8 (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
/*******************************************************************************
 * Copyright (c) 2005, 2011 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.ILinkage;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
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.IASTElaboratedTypeSpecifier;
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.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IField;
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.ICASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICCompositeTypeScope;
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.internal.core.dom.Linkage;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.parser.ASTQueries;
import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding;
import org.eclipse.core.runtime.PlatformObject;

/**
 * Represents structs and unions.
 */
public class CStructure extends PlatformObject implements ICompositeType, ICInternalBinding {

	public static class CStructureProblem extends ProblemBinding implements ICompositeType {
		public CStructureProblem(IASTNode node, int id, char[] arg) {
			super(node, id, arg);
		}

		@Override
		public IField findField(String name) {
			return null;
		}

		@Override
		public IScope getCompositeScope() {
			return this;
		}

		@Override
		public IField[] getFields() {
			return IField.EMPTY_FIELD_ARRAY;
		}

		@Override
		public int getKey() {
			return k_struct;
		}
	}

	private IASTName[] declarations;
	private IASTName definition;
	private boolean checked;
	private ICompositeType typeInIndex;

	public CStructure(IASTName name) {
		if (name.getPropertyInParent() == IASTCompositeTypeSpecifier.TYPE_NAME) {
			definition = name;
		} else {
			declarations = new IASTName[] { name };
		}
		name.setBinding(this);
	}

	@Override
	public IASTNode getPhysicalNode() {
		return definition != null ? (IASTNode) definition : (IASTNode) declarations[0];
	}

	private void checkForDefinition() {
		if (!checked && definition == null) {
			IASTNode declSpec = declarations[0].getParent();
			if (declSpec instanceof ICASTElaboratedTypeSpecifier) {
				IASTDeclSpecifier spec = CVisitor.findDefinition((ICASTElaboratedTypeSpecifier) declSpec);
				if (spec instanceof ICASTCompositeTypeSpecifier) {
					ICASTCompositeTypeSpecifier compTypeSpec = (ICASTCompositeTypeSpecifier) spec;
					definition = compTypeSpec.getName();
					definition.setBinding(this);
				}
			}

			if (definition == null && typeInIndex == null) {
				final IASTTranslationUnit translationUnit = declSpec.getTranslationUnit();
				IIndex index = translationUnit.getIndex();
				if (index != null) {
					typeInIndex = (ICompositeType) index.adaptBinding(this);
				}
			}
		}
		checked = true;
	}

	@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() throws DOMException {
		IASTDeclSpecifier declSpec = (IASTDeclSpecifier) ((definition != null) ?
				(IASTNode) definition.getParent() : declarations[0].getParent());
		IScope scope = CVisitor.getContainingScope(declSpec);
		while (scope instanceof ICCompositeTypeScope) {
			scope = scope.getParent();
		}
		return scope;
	}

	@Override
	public IField[] getFields() {
		checkForDefinition();
		if (definition == null) {
			return new IField[] { new CField.CFieldProblem(this, declarations[0],
					IProblemBinding.SEMANTIC_DEFINITION_NOT_FOUND, getNameCharArray()) };
		}
		ICASTCompositeTypeSpecifier compSpec = (ICASTCompositeTypeSpecifier) definition.getParent();
		IField[] fields = collectFields(compSpec, IField.EMPTY_FIELD_ARRAY);
		return ArrayUtil.trim(fields);
	}

	private IField[] collectFields(ICASTCompositeTypeSpecifier compSpec, IField[] fields) {
		IASTDeclaration[] members = compSpec.getMembers();
		if (members.length > 0) {
			if (fields == null)
				fields = new IField[members.length];
			for (IASTDeclaration node : members) {
				if (node instanceof IASTSimpleDeclaration) {
					IASTDeclarator[] declarators = ((IASTSimpleDeclaration) node).getDeclarators();
					if (declarators.length == 0) {
						IASTDeclSpecifier declspec = ((IASTSimpleDeclaration) node).getDeclSpecifier();
						if (declspec instanceof ICASTCompositeTypeSpecifier) {
							fields = collectFields((ICASTCompositeTypeSpecifier) declspec, fields);
						}
					} else {
						for (IASTDeclarator declarator : declarators) {
							IASTName name = ASTQueries.findInnermostDeclarator(declarator).getName();
							IBinding binding = name.resolveBinding();
							if (binding instanceof IField)
								fields = ArrayUtil.append(fields, (IField) binding);
						}
					}
				}
			}
		}
		return fields;
	}

	@Override
	public IField findField(String name) {
		IScope scope = getCompositeScope();
		if (scope == null) {
			return new CField.CFieldProblem(this, declarations[0],
					IProblemBinding.SEMANTIC_DEFINITION_NOT_FOUND, getNameCharArray());
		}

		final CASTName astName = new CASTName(name.toCharArray());
		astName.setPropertyInParent(CVisitor.STRING_LOOKUP_PROPERTY);
		IBinding binding = scope.getBinding(astName, true);
		if (binding instanceof IField)
			return (IField) binding;

		return null;
	}

	@Override
	public int getKey() {
		return definition != null ?
				((IASTCompositeTypeSpecifier) definition.getParent()).getKey() :
				((IASTElaboratedTypeSpecifier) declarations[0].getParent()).getKind();
	}

	@Override
	public IScope getCompositeScope() {
		checkForDefinition();
		if (definition != null) {
			return ((IASTCompositeTypeSpecifier) definition.getParent()).getScope();
		}
		// fwd-declarations must be backed up from the index
		if (typeInIndex != null) {
			IScope scope = typeInIndex.getCompositeScope();
			if (scope instanceof ICCompositeTypeScope)
				return scope;
		}
		return null;
	}

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

	public void addDefinition(ICASTCompositeTypeSpecifier compositeTypeSpec) {
		if (compositeTypeSpec.isActive()) {
			definition = compositeTypeSpec.getName();
			compositeTypeSpec.getName().setBinding(this);
		}
	}

	public void addDeclaration(IASTName decl) {
		if (!decl.isActive() || decl.getPropertyInParent() != IASTElaboratedTypeSpecifier.TYPE_NAME)
			return;

		decl.setBinding(this);
		if (declarations == null || declarations.length == 0) {
			declarations = new IASTName[] { decl };
			return;
		}
		IASTName first = declarations[0];
		if (((ASTNode) first).getOffset() > ((ASTNode) decl).getOffset()) {
			declarations[0] = decl;
			decl = first;
		}
		declarations = ArrayUtil.append(IASTName.class, declarations, decl);
	}

	@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) {
			if (declarations != null && declarations.length > 0) {
				node = declarations[0];
			}
		}
		IBinding result = CVisitor.findEnclosingFunction(node); // local or global
		if (result != null)
			return result;

		if (definition != null && isAnonymous()) {
			return CVisitor.findDeclarationOwner(definition, false);
		}
		return null;
	}

	@Override
	public boolean isAnonymous() {
		if (getNameCharArray().length > 0 || definition == null)
			return false;

		IASTCompositeTypeSpecifier spec = ((IASTCompositeTypeSpecifier) definition.getParent());
		if (spec != null) {
			IASTNode node = spec.getParent();
			if (node instanceof IASTSimpleDeclaration) {
				if (((IASTSimpleDeclaration) node).getDeclarators().length == 0) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * For debugging purposes, only.
	 */
	@Override
	public String toString() {
		return getName(); 
	}
}

Back to the top