blob: d918b6ab499afef1402986105acb27900853e8c7 (
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
|
/*******************************************************************************
* Copyright (c) 2008, 2014 IBM 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
* Sergey Prigogin (Google)
* Thomas Corbat (IFS)
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTAttribute;
import org.eclipse.cdt.core.dom.ast.IASTAttributeSpecifier;
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.IScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclarator;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.dom.parser.ASTAmbiguousNode;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousDeclarator;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.core.runtime.Assert;
/**
* Handles ambiguities when parsing declarators.
* <br>
* Example: void f(int (D)); // is D a type?
*/
public class CPPASTAmbiguousDeclarator extends ASTAmbiguousNode
implements IASTAmbiguousDeclarator, ICPPASTDeclarator {
private IASTDeclarator[] dtors = new IASTDeclarator[2];
private int dtorPos= -1;
private IASTInitializer fInitializer;
public CPPASTAmbiguousDeclarator(IASTDeclarator... decls) {
for (IASTDeclarator d : decls) {
if (d != null) {
addDeclarator(d);
}
}
}
@Override
protected void beforeResolution() {
// populate containing scope, so that it will not be affected by the alternative branches.
IScope scope= CPPVisitor.getContainingNonTemplateScope(this);
if (scope instanceof ICPPASTInternalScope) {
((ICPPASTInternalScope) scope).populateCache();
}
}
@Override
protected void afterResolution(ASTVisitor resolver, IASTNode best) {
// if we have an initializer it needs to be added to the chosen alternative.
// we also need to resolve ambiguities in the initializer.
if (fInitializer != null) {
((IASTDeclarator) best).setInitializer(fInitializer);
fInitializer.accept(resolver);
}
}
@Override
public IASTDeclarator copy() {
throw new UnsupportedOperationException();
}
@Override
public IASTDeclarator copy(CopyStyle style) {
throw new UnsupportedOperationException();
}
@Override
public void addDeclarator(IASTDeclarator d) {
assertNotFrozen();
if (d != null) {
dtors = ArrayUtil.appendAt(IASTDeclarator.class, dtors, ++dtorPos, d);
d.setParent(this);
d.setPropertyInParent(SUBDECLARATOR);
}
}
@Override
public IASTDeclarator[] getDeclarators() {
dtors = ArrayUtil.trimAt(IASTDeclarator.class, dtors, dtorPos);
return dtors;
}
@Override
public IASTNode[] getNodes() {
return getDeclarators();
}
@Override
public IASTInitializer getInitializer() {
return fInitializer;
}
@Override
public IASTName getName() {
return dtors[0].getName();
}
@Override
public IASTDeclarator getNestedDeclarator() {
return dtors[0].getNestedDeclarator();
}
@Override
public IASTPointerOperator[] getPointerOperators() {
return dtors[0].getPointerOperators();
}
@Override
public void addPointerOperator(IASTPointerOperator operator) {
assertNotFrozen();
Assert.isLegal(false);
}
@Override
public IASTAttribute[] getAttributes() {
return dtors[0].getAttributes();
}
@Override
@Deprecated
public void addAttribute(IASTAttribute attribute) {
assertNotFrozen();
Assert.isLegal(false);
}
@Override
public IASTAttributeSpecifier[] getAttributeSpecifiers() {
return dtors[0].getAttributeSpecifiers();
}
@Override
public void addAttributeSpecifier(IASTAttributeSpecifier attributeSpecifier) {
assertNotFrozen();
Assert.isLegal(false);
}
@Override
public int getRoleForName(IASTName name) {
return dtors[0].getRoleForName(name);
}
@Override
public void setInitializer(IASTInitializer initializer) {
// store the initializer until the ambiguity is resolved
fInitializer= initializer;
}
@Override
public void setName(IASTName name) {
assertNotFrozen();
Assert.isLegal(false);
}
@Override
public void setNestedDeclarator(IASTDeclarator nested) {
assertNotFrozen();
Assert.isLegal(false);
}
@Override
public boolean declaresParameterPack() {
return false;
}
@Override
public void setDeclaresParameterPack(boolean val) {
assertNotFrozen();
Assert.isLegal(false);
}
}
|