Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bbbc784d742282f1ada5dde78d7fe20146f8e015 (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
package org.eclipse.cdt.core.dom.lrparser.action.cpp;

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

import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.internal.core.dom.parser.ASTInternal;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTAmbiguity;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;


public class CPPASTAmbiguousDeclarator extends CPPASTAmbiguity implements IASTDeclarator {

	private List<IASTDeclarator> declarators = new ArrayList<IASTDeclarator>(2);
	
	public CPPASTAmbiguousDeclarator(IASTDeclarator ... ds) {
		for(IASTDeclarator declarator : ds)
			addDeclarator(declarator);
	}
	
	@Override
	protected IASTNode[] getNodes() {
		return declarators.toArray(new IASTDeclarator[declarators.size()]);
	}

	
	
	@Override
	public boolean accept(ASTVisitor visitor) {
		// this code was copied from CPPASTAmbiguity.accept() and slightly modified.
		IASTNode nodeToReplace = this;
    	IASTAmbiguityParent owner = (IASTAmbiguityParent) getParent();
    	
        IASTNode[] nodez = getNodes();
        int[] problems = new int[nodez.length];
        
        for(int i = 0; i < nodez.length; ++i) {
            IASTNode node = nodez[i];
            owner.replace(nodeToReplace, node);
            nodeToReplace = node;
            
            node.accept(visitor);
            CPPASTNameCollector nameCollector = new CPPASTNameCollector();
            node.accept(nameCollector);
            IASTName[] names = nameCollector.getNames();
            for(IASTName name : names) {
                try {
                    IBinding b = name.resolveBinding();
                    if(b == null || b instanceof IProblemBinding)
                        ++problems[i];
                } catch (Exception t) {
                	t.printStackTrace();
                    ++problems[i];
                }
            }
            if(names.length > 0) {
                IScope scope = CPPVisitor.getContainingScope(names[0]);
                
                if( scope != null ) {
                    try {
                        ASTInternal.flushCache(scope);
                        IScope parentScope = scope.getParent(); // needed to fix bugs
                        if(parentScope != null) {
                        	ASTInternal.flushCache(parentScope);
                        }
                    } catch (DOMException de) {}
                }
            }
        }
        int bestIndex = 0;
        int bestValue = problems[0];
        for (int i = 1; i < problems.length; ++i) {
            if (problems[i] < bestValue) {
                bestIndex = i;
                bestValue = problems[i];
            }
        }

        //IASTAmbiguityParent owner = (IASTAmbiguityParent) getParent();
        owner.replace(nodeToReplace, nodez[bestIndex]);
        return true;
	}

	public void addDeclarator(IASTDeclarator declarator) {
		if(declarator != null) {
			declarators.add(declarator);
			declarator.setParent(this);
			declarator.setPropertyInParent(null); // it really doesn't matter
		}
	}

	private IASTDeclarator getDefaultDeclarator() {
		return declarators.get(0);
	}
	
	public void addPointerOperator(IASTPointerOperator operator) {
		getDefaultDeclarator().addPointerOperator(operator);
	}
	
	public void setInitializer(IASTInitializer initializer) {
		getDefaultDeclarator().setInitializer(initializer);
	}

	public void setName(IASTName name) {
		getDefaultDeclarator().setName(name);
	}

	public void setNestedDeclarator(IASTDeclarator nested) {
		getDefaultDeclarator().setNestedDeclarator(nested);
	}
	
	public IASTInitializer getInitializer() {
		return getDefaultDeclarator().getInitializer();
	}

	public IASTName getName() {
		return getDefaultDeclarator().getName();
	}

	public IASTDeclarator getNestedDeclarator() {
		return getDefaultDeclarator().getNestedDeclarator();
	}

	public IASTPointerOperator[] getPointerOperators() {
		return getDefaultDeclarator().getPointerOperators();
	}

	public int getRoleForName(IASTName n) {
		return getDefaultDeclarator().getRoleForName(n);
	}


}

Back to the top