Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 41b01aa6ef43328e841c77fe456890b430477c4d (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*******************************************************************************
 * Copyright (c) 2008, 2010 Wind River Systems, Inc. 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:
 *    Markus Schorn - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser;

import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTArraySubscriptExpression;
import org.eclipse.cdt.core.dom.ast.IASTCastExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpressionList;
import org.eclipse.cdt.core.dom.ast.IASTFieldReference;
import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression;
import org.eclipse.cdt.core.dom.ast.IASTInitializerClause;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
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.core.runtime.Assert;

/**
 * Handles the ambiguity between cast and function-call expressions: (type)(expr) versus (function)(expr);
 * It also handles the impact on the grouping of the sub-expressions.
 */
public abstract class ASTAmbiguousCastVsFunctionCallExpression extends ASTAmbiguousNode implements IASTAmbiguousExpression {

    private final IASTCastExpression fCastExpression;
    private final IASTFunctionCallExpression fFunctionCallExpression;
    
    /**
     * The operand of the cast expression must start with an expression in parenthesis (which could be read as the parameter
     * list of the function call).
     * The function-call must contain the name expression (corresponding to the type-id of the cast expression). The parameter
     * expression may be <code>null</code>, it will be computed from the cast expression.
     */
    public ASTAmbiguousCastVsFunctionCallExpression(IASTCastExpression castExpression, IASTFunctionCallExpression functionCall) {
    	fCastExpression= castExpression;
    	fFunctionCallExpression= functionCall;
    }
    
	@Override
	public final IASTNode[] getNodes() {
		return getExpressions();
	}

    public final IASTExpression copy() {
		throw new UnsupportedOperationException();
	}
    
    public void addExpression(IASTExpression e) {
		Assert.isLegal(false);
    }
    
    public IType getExpressionType() {
    	return fCastExpression.getExpressionType();
    }

	public IASTExpression[] getExpressions() {
		return new IASTExpression[] {fCastExpression, fFunctionCallExpression};
	}

	@Override
	public final IASTNode resolveAmbiguity(ASTVisitor visitor) {
		final IASTAmbiguityParent owner= (IASTAmbiguityParent) getParent();
		IASTNode nodeToReplace= this;

		// try cast-expression
		owner.replace(nodeToReplace, fCastExpression);
		nodeToReplace= fCastExpression;
		// resolve nested ambiguities
		fCastExpression.accept(visitor);

		// if the operand of the cast-expr is not suitable for a function call, we are done.
		final IASTUnaryExpression primaryWithParenthesis= findPrimaryExpressionInParenthesis(fCastExpression.getOperand());
		if (primaryWithParenthesis == null)
			return nodeToReplace;

		
		// find nested names
		final NameCollector nameCollector= new NameCollector();
		fCastExpression.getTypeId().accept(nameCollector);
		final IASTName[] names= nameCollector.getNames();

		// resolve names 
		boolean hasIssue= false;
		for (IASTName name : names) {
			try {
				IBinding b = name.resolveBinding();
				if (b instanceof IProblemBinding) {
					hasIssue= true;
					break;
				}
			} catch (Exception t) {
				hasIssue= true;
				break;
			}
		}
		if (!hasIssue) 
			return nodeToReplace;

		final IASTExpression operand = primaryWithParenthesis.getOperand();
		if (operand instanceof IASTExpressionList) {
			final IASTExpressionList list= (IASTExpressionList) operand;
			fFunctionCallExpression.setArguments(list.getExpressions());
		} else if (operand != null) {
			fFunctionCallExpression.setArguments(new IASTInitializerClause[] {operand});
		} else {
			fFunctionCallExpression.setArguments(IASTExpression.EMPTY_EXPRESSION_ARRAY);
		}
		setRange(fFunctionCallExpression, fCastExpression, primaryWithParenthesis);
		
		IASTExpression result= fFunctionCallExpression;
		IASTExpression postFix= fCastExpression.getOperand();
		if (postFix != primaryWithParenthesis) {
			result= postFix;
			while (true) {
				setStart(postFix, fFunctionCallExpression);
				if (postFix instanceof IASTArraySubscriptExpression) {
					final IASTArraySubscriptExpression ase = (IASTArraySubscriptExpression) postFix;
					postFix= ase.getArrayExpression();
					if (postFix == primaryWithParenthesis) {
						ase.setArrayExpression(fFunctionCallExpression);
						break;
					}
				} else if (postFix instanceof IASTFunctionCallExpression) {
					final IASTFunctionCallExpression fc = (IASTFunctionCallExpression) postFix;
					postFix= fc.getFunctionNameExpression();
					if (postFix == primaryWithParenthesis) {
						fc.setFunctionNameExpression(fFunctionCallExpression);
						break;
					}
				} else if (postFix instanceof IASTFieldReference) {
					final IASTFieldReference fr = (IASTFieldReference) postFix;
					postFix= fr.getFieldOwner();
					if (postFix == primaryWithParenthesis) {
						fr.setFieldOwner(fFunctionCallExpression);
						break;
					}
				} else {
					final IASTUnaryExpression ue = (IASTUnaryExpression) postFix;
					postFix= ue.getOperand();
					if (postFix == primaryWithParenthesis) {
						ue.setOperand(fFunctionCallExpression);
						break;
					}
				}
			}
		}
		
		owner.replace(nodeToReplace, result);
		// resolve ambiguities in the function-call expression
		fFunctionCallExpression.getFunctionNameExpression().accept(visitor);
		return result;
	}

	private IASTUnaryExpression findPrimaryExpressionInParenthesis(IASTExpression operand) {
		while (true) {
			if (operand instanceof IASTUnaryExpression) {
				final IASTUnaryExpression unary= (IASTUnaryExpression) operand;
				switch (unary.getOperator()) {
				case IASTUnaryExpression.op_bracketedPrimary:
					return unary;
				case IASTUnaryExpression.op_postFixDecr:
				case IASTUnaryExpression.op_postFixIncr:
					operand= unary.getOperand();
					break;
				default:
					return null;
				}
			}
			else if (operand instanceof IASTArraySubscriptExpression) {
				operand= ((IASTArraySubscriptExpression) operand).getArrayExpression();
			} else if (operand instanceof IASTFunctionCallExpression) {
				operand= ((IASTFunctionCallExpression) operand).getFunctionNameExpression();
			} else if (operand instanceof IASTFieldReference) {
				operand= ((IASTFieldReference) operand).getFieldOwner();
			} else {
				return null;
			}
		}
	}

	private void setStart(IASTNode node, IASTNode start) {
		final ASTNode target= (ASTNode) node;
		final int offset = ((ASTNode) start).getOffset();
		target.setOffsetAndLength(offset, target.getOffset() + target.getLength() - offset);
	}

	private void setRange(IASTNode node, IASTNode from, IASTNode to) {
		final int offset = ((ASTNode) from).getOffset();
		final ASTNode t= (ASTNode) to;
		((ASTNode) node).setOffsetAndLength(offset, t.getOffset()+t.getLength()-offset);
	}
}

Back to the top