Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 69bf58513b958e1d1008fb026e13830cd34c9b60 (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
/*******************************************************************************
 * Copyright (c) 2009, 2011 Wind River Systems, Inc. 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:
 *    Markus Schorn - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.c;

import org.eclipse.cdt.core.dom.ast.ASTVisitor;
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.IASTName;
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.parser.util.CharArrayMap;

/**
 * Utility to map index bindings to ast bindings.
 */
public class CStructMapper {
	private class Visitor extends ASTVisitor {
		Visitor() {
			shouldVisitDeclarations = true;
		}

		@Override
		public int visit(IASTDeclaration declaration) {
			if (declaration instanceof IASTSimpleDeclaration) {
				IASTDeclSpecifier declspec = ((IASTSimpleDeclaration) declaration).getDeclSpecifier();
				if (declspec instanceof IASTCompositeTypeSpecifier) {
					IASTCompositeTypeSpecifier cts = (IASTCompositeTypeSpecifier) declspec;
					final IASTName name = cts.getName();
					final char[] nameChars = name.toCharArray();
					if (nameChars.length > 0) {
						fStructs.put(nameChars, name);
					}
					return PROCESS_CONTINUE;
				}
			}
			return PROCESS_SKIP;
		}
	}

	private final IASTTranslationUnit fTranslationUnit;
	protected CharArrayMap<IASTName> fStructs;

	public CStructMapper(IASTTranslationUnit tu) {
		fTranslationUnit = tu;
	}

	public ICompositeType mapToAST(ICompositeType type) {
		if (fStructs == null) {
			fStructs = new CharArrayMap<IASTName>();
			fTranslationUnit.accept(new Visitor());
		}
		IASTName name = fStructs.get(type.getNameCharArray());
		if (name != null) {
			IBinding b = name.resolveBinding();
			if (b instanceof ICompositeType) {
				final ICompositeType mapped = (ICompositeType) b;
				if (mapped.isSameType(type)) {
					return mapped;
				}
			}
		}
		return type;
	}
}

Back to the top