Skip to main content
summaryrefslogtreecommitdiffstats
blob: 264924a2f77692f9efb5db74e220508d9a1e8f4b (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
/*******************************************************************************
 * 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)
 *     Bryan Wilkinson (QNX)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.cdt.core.dom.IName;
import org.eclipse.cdt.core.dom.ast.EScopeKind;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ILabel;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionScope;
import org.eclipse.cdt.core.parser.util.CharArrayObjectMap;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;

/**
 * Scope of a function, containing labels.
 */
public class CPPFunctionScope extends CPPScope implements ICPPFunctionScope {
    private CharArrayObjectMap<ILabel> labels = CharArrayObjectMap.emptyMap();
    
	/**
	 * @param physicalNode
	 */
	public CPPFunctionScope(IASTFunctionDeclarator physicalNode) {
		super(physicalNode);
	}
	
	@Override
	public EScopeKind getKind() {
		return EScopeKind.eLocal;
	}

	@Override
	public void addBinding(IBinding binding) {
	    //3.3.4 only labels have function scope
	    if (!(binding instanceof ILabel))
	        return;

	    if (labels == CharArrayObjectMap.EMPTY_MAP)
	        labels = new CharArrayObjectMap<ILabel>(2);

	    labels.put(binding.getNameCharArray(), (ILabel) binding);
	}

	@Override
	public IBinding[] find(String name) {
	    char[] n = name.toCharArray();
	    List<IBinding> bindings = new ArrayList<IBinding>();

	    for (int i = 0; i < labels.size(); i++) {
	    	char[] key = labels.keyAt(i);
	    	if (CharArrayUtils.equals(key, n)) {
	    		bindings.add(labels.get(key));
	    	}
	    }
	    
	    IBinding[] additional = super.find(name);
	    for (IBinding element : additional) {
	    	bindings.add(element);
	    }
	    
	    return bindings.toArray(new IBinding[bindings.size()]);
	}
	
	@Override
	public IScope getParent() {
	    //we can't just resolve the function and get its parent scope, since there are cases where that 
	    //could loop since resolving functions requires resolving their parameter types
	    IASTFunctionDeclarator fdtor = (IASTFunctionDeclarator) getPhysicalNode();
	    IASTName name = fdtor.getName().getLastName();
	    return CPPVisitor.getContainingNonTemplateScope(name);
	}

    @Override
	public IScope getBodyScope() {
        IASTFunctionDeclarator fnDtor = (IASTFunctionDeclarator) getPhysicalNode();
        IASTNode parent = fnDtor.getParent();
        if (parent instanceof IASTFunctionDefinition) {
            IASTStatement body = ((IASTFunctionDefinition)parent).getBody();
            if (body instanceof IASTCompoundStatement)
                return ((IASTCompoundStatement)body).getScope();
        }
        return null;
    }

    @Override
	public IName getScopeName() {
        IASTNode node = getPhysicalNode();
        if (node instanceof IASTDeclarator) {
            return ((IASTDeclarator)node).getName();
        }
        return null;
    }
}

Back to the top