1
/*******************************************************************************
2
* Copyright (c) 2008, 2009 IBM Wind River Systems, Inc. and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Markus Schorn - Initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.internal.core.dom.parser.c;
13
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
14
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
15
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
16
import org.eclipse.cdt.core.dom.ast.IASTName;
17
import org.eclipse.cdt.core.dom.ast.IASTNode;
18
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
19
import org.eclipse.cdt.core.dom.ast.IBinding;
20
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
21
import org.eclipse.cdt.core.dom.ast.IScope;
22
import org.eclipse.cdt.internal.core.dom.parser.ASTAmbiguousNode;
23
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent;
24
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousSimpleDeclaration;
25
import org.eclipse.cdt.internal.core.dom.parser.IASTInternalScope;
28
* Handles ambiguities for parameter declarations.
30
* void function(const D*); // is D a type?
33
public class CASTAmbiguousSimpleDeclaration extends ASTAmbiguousNode implements IASTAmbiguousSimpleDeclaration {
35
private IASTSimpleDeclaration fSimpleDecl;
36
private IASTDeclSpecifier fAltDeclSpec;
37
private IASTDeclarator fAltDtor;
39
public CASTAmbiguousSimpleDeclaration(IASTSimpleDeclaration decl, IASTDeclSpecifier declSpec, IASTDeclarator dtor) {
41
fAltDeclSpec= declSpec;
46
protected void beforeResolution() {
47
// populate containing scope, so that it will not be affected by the alternative branches.
48
IScope scope= CVisitor.getContainingScope(this);
49
if (scope instanceof IASTInternalScope) {
50
((IASTInternalScope) scope).populateCache();
55
public IASTNode[] getNodes() {
56
return new IASTNode[] {fSimpleDecl, fAltDeclSpec, fAltDtor};
59
public IASTSimpleDeclaration copy() {
60
throw new UnsupportedOperationException();
63
public void addDeclarator(IASTDeclarator declarator) {
64
fSimpleDecl.addDeclarator(declarator);
67
public IASTDeclSpecifier getDeclSpecifier() {
68
return fSimpleDecl.getDeclSpecifier();
71
public IASTDeclarator[] getDeclarators() {
72
return fSimpleDecl.getDeclarators();
75
public void setDeclSpecifier(IASTDeclSpecifier declSpec) {
76
fSimpleDecl.setDeclSpecifier(declSpec);
80
public final IASTNode resolveAmbiguity(ASTVisitor visitor) {
81
final IASTAmbiguityParent owner= (IASTAmbiguityParent) getParent();
82
IASTNode nodeToReplace= this;
84
// handle nested ambiguities first
85
owner.replace(nodeToReplace, fSimpleDecl);
86
IASTDeclSpecifier declSpec= fSimpleDecl.getDeclSpecifier();
87
declSpec.accept(visitor);
91
final NameCollector nameCollector= new NameCollector();
92
declSpec.accept(nameCollector);
93
final IASTName[] names= nameCollector.getNames();
96
boolean hasIssue= false;
97
for (IASTName name : names) {
99
IBinding b = name.resolveBinding();
100
if (b instanceof IProblemBinding) {
104
} catch (Exception t) {
110
// use the alternate version
111
final IASTAmbiguityParent parent = (IASTAmbiguityParent) fSimpleDecl;
112
parent.replace(declSpec, fAltDeclSpec);
113
parent.replace(fSimpleDecl.getDeclarators()[0], fAltDtor);
116
// resolve further nested ambiguities
117
fSimpleDecl.accept(visitor);