Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 76445da8287e3664ff285a0ce499ae4e7a9a00f6 (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
/*******************************************************************************
 * Copyright (c) 2004, 2014 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)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.c;

import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.EScopeKind;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
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.IBinding;
import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.ISemanticProblem;
import org.eclipse.cdt.core.dom.ast.c.ICASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICCompositeTypeScope;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.dom.parser.ASTInternal;
import org.eclipse.cdt.internal.core.dom.parser.ASTQueries;

/**
 * Implementation of scope for structs and unions.
 */
public class CCompositeTypeScope extends CScope implements ICCompositeTypeScope {
    public CCompositeTypeScope(ICASTCompositeTypeSpecifier compTypeSpec) {
        super(compTypeSpec, EScopeKind.eClassType);
    }

    @Override
	public IBinding getBinding(char[] name) {
        return super.getBinding(NAMESPACE_TYPE_OTHER, name);
    }

    @Override
	public IBinding[] find(String name) {
        CollectNamesAction action = new CollectNamesAction(name.toCharArray());
        getPhysicalNode().accept(action);
        
        IASTName[] names = action.getNames();
        IBinding[] result = null;
        for (IASTName astName : names) {
            IBinding b = astName.resolveBinding();
            if (b == null) continue;
            try {
                if (b.getScope() == this)
                    result = ArrayUtil.append(IBinding.class, result, b);
            } catch (DOMException e) {
            }
        }
            
        return ArrayUtil.trim(IBinding.class, result);
    }

	@Override
	public ICompositeType getCompositeType() {
		ICASTCompositeTypeSpecifier compSpec = (ICASTCompositeTypeSpecifier) getPhysicalNode();
		IBinding binding = compSpec.getName().resolveBinding();
		if (binding instanceof ICompositeType)
			return (ICompositeType) binding;

		return new CStructure.CStructureProblem(compSpec.getName(), ISemanticProblem.BINDING_NO_CLASS, compSpec.getName().toCharArray());
	}
	
	@Override
	protected void doPopulateCache() {
		ICASTCompositeTypeSpecifier compSpec = (ICASTCompositeTypeSpecifier) getPhysicalNode();
		ICASTCompositeTypeSpecifier[] specStack = null;
		int stackIdx = -1;
		IASTDeclaration[] members = compSpec.getMembers();
		while (members != null) {
			int size = members.length;
			for (int i = 0; i < size; i++) {
				IASTNode node = members[i];
				if (node instanceof IASTSimpleDeclaration) {
					IASTDeclarator[] declarators = ((IASTSimpleDeclaration) node).getDeclarators();
					for (IASTDeclarator declarator : declarators) {
						IASTName dtorName = ASTQueries.findInnermostDeclarator(declarator).getName();
						ASTInternal.addName(this, dtorName);
					}
					// anonymous structures and unions
					if (declarators.length == 0 && ((IASTSimpleDeclaration) node).getDeclSpecifier() instanceof IASTCompositeTypeSpecifier) {
						ICASTCompositeTypeSpecifier declSpec = (ICASTCompositeTypeSpecifier) ((IASTSimpleDeclaration) node).getDeclSpecifier();
						IASTName n = declSpec.getName();
						if (n.toCharArray().length == 0) {
							specStack = ArrayUtil.append(ICASTCompositeTypeSpecifier.class, specStack, declSpec);
						}
					}
				}
			}
			if (specStack != null && ++stackIdx < specStack.length && specStack[stackIdx] != null) {
				members = specStack[stackIdx].getMembers();
			} else {
				members = null;
			}
		}
	}
}

Back to the top