Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0a234b3541092e67fe6aab147cceb987771e05d3 (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
/*******************************************************************************
 * 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:
 *     IBM - Initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser;

import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluation;

/**
 * Base implementation for all ambiguous nodes.
 */
public abstract class ASTAmbiguousNode extends ASTNode  {

    public static class NameCollector extends ASTVisitor {
		private IASTName[] names = new IASTName[2];
		private int namesPos = -1;

		public NameCollector() {
			shouldVisitNames = true;
		}

		@Override
		public int visit(IASTName name) {
			if (name != null) {
				namesPos++;
				names = ArrayUtil.append(IASTName.class, names, name);
			}
			return PROCESS_CONTINUE;
		}

		public IASTName[] getNames() {
			names = ArrayUtil.trimAt(IASTName.class, names, namesPos);
			return names;
		}
	}
    
	private IASTNode fResolution;

    /**
     * Return the alternative nodes for this ambiguity.
     */
    public abstract IASTNode[] getNodes();
    
    @Override
	public final boolean accept(ASTVisitor visitor) {
    	if (visitor.shouldVisitAmbiguousNodes && visitor.visit(this) == ASTVisitor.PROCESS_ABORT)
    		return false;
    		
    	// alternatives are not visited on purpose.
    	return true;
    }
    
	protected void beforeResolution() {
	}

	protected void beforeAlternative(IASTNode alternative) {
	}

	protected void afterResolution(ASTVisitor resolver, IASTNode best) {
	}

	public IASTNode resolveAmbiguity(ASTVisitor resolver) {
		return fResolution= doResolveAmbiguity(resolver);
	}
	
    protected IASTNode doResolveAmbiguity(ASTVisitor resolver) {
    	beforeResolution();
		final IASTAmbiguityParent owner= (IASTAmbiguityParent) getParent();
		IASTNode nodeToReplace= this;

		final IASTNode[] alternatives= getNodes();
		IASTNode bestAlternative= null;
		
		int minIssues = Integer.MAX_VALUE;
		for (IASTNode alternative : alternatives) {
			// Setup the ast to use the alternative
			owner.replace(nodeToReplace, alternative);

			beforeAlternative(alternative);
			
			// Handle nested ambiguities
			alternative= resolveNestedAmbiguities(alternative, resolver);
			nodeToReplace= alternative;

			// Find nested names
			final NameCollector nameCollector= new NameCollector();
			alternative.accept(nameCollector);
			final IASTName[] names= nameCollector.getNames();
			
			// Resolve names and count issues
			int issues= 0;
			for (IASTName name : names) {
				try {
					// Avoid resolution of parameters (can always be resolved), 
					// it can triggers resolution of declaration it belongs to, 
					// while the declarator is still ambiguous. Could be solved by introducing an
					// intermediate binding for parameters, similar to template parameters.
					if (name.getPropertyInParent() == IASTDeclarator.DECLARATOR_NAME) {
						IASTNode parent= name.getParent();
						if (parent instanceof IASTDeclarator) {
							parent= ASTQueries.findOutermostDeclarator((IASTDeclarator) parent);
							if (parent.getPropertyInParent() == IASTParameterDeclaration.DECLARATOR)
								continue;
						}
					}
					IBinding b= name.resolvePreBinding();
					if (b instanceof IProblemBinding) {
						issues++;
					} 
				} catch (Exception t) {
					issues++;
				}
				if (issues == minIssues) {
					break;
				}
			}
			if (issues < minIssues) {
				minIssues= issues;
				bestAlternative= alternative;
				if (issues == 0) {
					break;
				}
			}
		}
		
		// Switch back to the best alternative, if necessary.
		if (nodeToReplace != bestAlternative) {
			owner.replace(nodeToReplace, bestAlternative);
		}
		afterResolution(resolver, bestAlternative);
		return bestAlternative;
	}
    
	protected IASTNode resolveNestedAmbiguities(IASTNode alternative, ASTVisitor resolver) {
		alternative.accept(resolver);
		if (alternative instanceof ASTAmbiguousNode)
			return ((ASTAmbiguousNode) alternative).fResolution;
		return alternative;
	}

	public final IType getExpressionType() {
		throw new UnsupportedOperationException();
    }
    public final ValueCategory getValueCategory() {
		throw new UnsupportedOperationException();
    }
	public final boolean isLValue() {
		throw new UnsupportedOperationException();
    }
	public final ICPPEvaluation getEvaluation() {
		throw new UnsupportedOperationException();
	}
}

Back to the top