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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
/*******************************************************************************
* Copyright (c) 2004, 2015 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:
* Andrew Niefer (IBM Corporation) - initial API and implementation
* Markus Schorn (Wind River Systems)
* Bryan Wilkinson (QNX)
* Andrew Ferguson (Symbian)
* Sergey Prigogin (Google)
* Jens Elmenthaler - http://bugs.eclipse.org/173458 (camel case completion)
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp;
import static org.eclipse.cdt.core.dom.ast.cpp.ICPPParameter.EMPTY_CPPPARAMETER_ARRAY;
import static org.eclipse.cdt.core.parser.util.ArrayUtil.addAll;
import static org.eclipse.cdt.core.parser.util.ArrayUtil.appendAt;
import static org.eclipse.cdt.core.parser.util.ArrayUtil.trim;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.CPPBasicType.UNSPECIFIED_TYPE;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.TDEF;
import java.util.Arrays;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IName;
import org.eclipse.cdt.core.dom.ast.EScopeKind;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFieldReference;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
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.core.dom.ast.ISemanticProblem;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNameSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateId;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBase;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassTemplate;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPParameter;
import org.eclipse.cdt.core.index.IIndexFileSet;
import org.eclipse.cdt.core.parser.util.CharArrayObjectMap;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.core.parser.util.ObjectSet;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPSemantics;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil;
import org.eclipse.cdt.internal.core.parser.util.ContentAssistMatcherFactory;
import org.eclipse.core.runtime.IStatus;
/**
* Base implementation for C++ scopes.
*/
public class CPPClassScope extends CPPScope implements ICPPClassScope {
private static final ICPPFunctionType DESTRUCTOR_FUNCTION_TYPE =
CPPVisitor.createImplicitFunctionType(UNSPECIFIED_TYPE, EMPTY_CPPPARAMETER_ARRAY, false, false);
private ICPPMethod[] implicits;
public CPPClassScope(ICPPASTCompositeTypeSpecifier physicalNode) {
super(physicalNode);
}
@Override
public EScopeKind getKind() {
return EScopeKind.eClassType;
}
/**
* Adds in default constructor, copy constructor, copy assignment operator and destructor,
* if appropriate. The method will be called after ambiguity resolution.
*/
public void createImplicitMembers() {
// Create bindings for the implicit members, if the user declared them then those
// declarations will resolve to these bindings.
ICPPASTCompositeTypeSpecifier compTypeSpec = (ICPPASTCompositeTypeSpecifier) getPhysicalNode();
IASTName name = compTypeSpec.getName().getLastName();
IBinding binding = name.resolveBinding();
if (!(binding instanceof ICPPClassType))
return;
ICPPClassType classType = (ICPPClassType) binding;
if (classType instanceof ICPPClassTemplate) {
classType= (ICPPClassType) ((ICPPClassTemplate) classType).asDeferredInstance();
}
char[] className = name.getLookupKey();
IType pType = new CPPReferenceType(SemanticUtil.constQualify(classType), false);
ICPPParameter[] params = new ICPPParameter[] { new CPPParameter(pType, 0) };
int i= 0;
ImplicitsAnalysis ia= new ImplicitsAnalysis(compTypeSpec, classType);
implicits= new ICPPMethod[ia.getImplicitsToDeclareCount()];
if (!ia.hasUserDeclaredConstructor()) {
// Default constructor: A(void)
ICPPMethod m = new CPPImplicitConstructor(this, className, EMPTY_CPPPARAMETER_ARRAY,
compTypeSpec);
implicits[i++] = m;
addBinding(m);
}
if (!ia.hasUserDeclaredCopyConstructor()) {
// Copy constructor: A(const A &)
ICPPMethod m = new CPPImplicitConstructor(this, className, params, compTypeSpec);
implicits[i++] = m;
addBinding(m);
}
if (!ia.hasUserDeclaredCopyAssignmentOperator()) {
// Copy assignment operator: A& operator = (const A &)
IType refType = new CPPReferenceType(classType, false);
ICPPFunctionType ft= CPPVisitor.createImplicitFunctionType(refType, params, false, false);
ICPPMethod m =
new CPPImplicitMethod(this, OverloadableOperator.ASSIGN.toCharArray(), ft, params, false);
implicits[i++] = m;
addBinding(m);
}
if (!ia.hasUserDeclaredDestructor()) {
// Destructor: ~A()
char[] dtorName = CharArrayUtils.concat("~".toCharArray(), className); //$NON-NLS-1$
ICPPMethod m = new CPPImplicitMethod(this, dtorName, DESTRUCTOR_FUNCTION_TYPE, EMPTY_CPPPARAMETER_ARRAY,
false);
implicits[i++] = m;
addBinding(m);
}
markInheritedConstructorsSourceBases(compTypeSpec);
}
/**
* Marks bases that serve as sources of inherited constructors.
*/
private void markInheritedConstructorsSourceBases(ICPPASTCompositeTypeSpecifier compositeTypeSpec) {
ICPPBase[] bases = ClassTypeHelper.getBases(getClassType(), compositeTypeSpec);
if (bases.length == 0)
return;
IASTDeclaration[] members = compositeTypeSpec.getMembers();
for (IASTDeclaration member : members) {
if (member instanceof ICPPASTUsingDeclaration) {
IASTName name = ((ICPPASTUsingDeclaration) member).getName();
if (!(name instanceof ICPPASTQualifiedName))
continue;
ICPPASTQualifiedName qName = (ICPPASTQualifiedName) name;
ICPPASTNameSpecifier[] qualifier = qName.getQualifier();
IBinding parent = qualifier[qualifier.length - 1].resolveBinding();
if (!(parent instanceof IType) || parent instanceof IProblemBinding)
continue;
if (isConstructorNameForType(qName.getLastName().getSimpleID(), (IType) parent)) {
IType type = SemanticUtil.getNestedType((IType) parent, TDEF);
for (ICPPBase base : bases) {
IType baseClass = base.getBaseClassType();
if (type.isSameType(baseClass)) {
if (base instanceof CPPBaseClause) {
((CPPBaseClause) base).setInheritedConstructorsSource(true);
} else {
CCorePlugin.log(IStatus.ERROR, "Unexpected type of base (" //$NON-NLS-1$
+ base.getClass().getSimpleName() + ") for '" //$NON-NLS-1$
+ compositeTypeSpec.getRawSignature() + "'"); //$NON-NLS-1$
}
}
}
}
}
}
}
private static boolean isConstructorNameForType(char[] lastName, IType type) {
while (type instanceof IBinding) {
if (Arrays.equals(((IBinding) type).getNameCharArray(), lastName))
return true;
if (!(type instanceof ITypedef))
break;
type = ((ITypedef) type).getType();
}
return false;
}
@Override
public IScope getParent() {
ICPPASTCompositeTypeSpecifier compType = (ICPPASTCompositeTypeSpecifier) getPhysicalNode();
IASTName compName = compType.getName().getLastName();
return CPPVisitor.getContainingNonTemplateScope(compName);
}
@Override
public void addBinding(IBinding binding) {
if (binding instanceof ICPPConstructor) {
addConstructor(binding);
return;
}
super.addBinding(binding);
}
@Override
public void addName(IASTName name, boolean adlOnly) {
// Don't add names from inactive code branches.
if (!name.isActive())
return;
if (name instanceof ICPPASTQualifiedName) {
// Check whether the qualification matches.
IBinding b= getClassType();
final ICPPASTQualifiedName qname = (ICPPASTQualifiedName) name;
final ICPPASTNameSpecifier[] qualifier = qname.getQualifier();
for (int i = qualifier.length; --i >= 0;) {
if (b == null)
return;
char[] segmentName;
if (qualifier[i] instanceof IASTName) {
segmentName = ((IASTName) qualifier[i]).getLookupKey();
} else {
IBinding segmentBinding = qualifier[i].resolveBinding();
if (segmentBinding == null)
return;
segmentName = segmentBinding.getNameCharArray();
}
if (!CharArrayUtils.equals(segmentName, b.getNameCharArray()))
return;
b= b.getOwner();
}
if (qname.isFullyQualified() && b != null)
return;
}
IASTNode parent = name.getParent();
if (parent instanceof IASTDeclarator) {
if (CPPVisitor.isConstructor(this, (IASTDeclarator) parent)) {
addConstructor(name);
return;
}
}
super.addName(name, adlOnly);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private void addConstructor(Object constructor) {
if (bindings == null)
bindings = new CharArrayObjectMap(1);
Object o = bindings.get(CONSTRUCTOR_KEY);
if (o != null) {
if (o instanceof ObjectSet) {
((ObjectSet) o).put(constructor);
} else {
ObjectSet set = new ObjectSet(2);
set.put(o);
set.put(constructor);
bindings.put(CONSTRUCTOR_KEY, set);
}
} else {
bindings.put(CONSTRUCTOR_KEY, constructor);
}
}
@Override
public IBinding getBinding(IASTName name, boolean resolve, IIndexFileSet fileSet) {
char[] c = name.getLookupKey();
ICPPASTCompositeTypeSpecifier compType = (ICPPASTCompositeTypeSpecifier) getPhysicalNode();
IASTName compName = compType.getName().getLastName();
if (compName instanceof ICPPASTTemplateId) {
compName= ((ICPPASTTemplateId) compName).getTemplateName();
}
if (CharArrayUtils.equals(c, compName.getLookupKey())) {
// 9.2 ... The class-name is also inserted into the scope of the class itself.
return compName.resolveBinding();
}
return super.getBinding(name, resolve, fileSet);
}
@Override
public IBinding[] getBindings(ScopeLookupData lookup) {
char[] c = lookup.getLookupKey();
final boolean prefixLookup= lookup.isPrefixLookup();
ICPPASTCompositeTypeSpecifier compType = (ICPPASTCompositeTypeSpecifier) getPhysicalNode();
IASTName compName = compType.getName().getLastName();
if (compName instanceof ICPPASTTemplateId) {
compName= ((ICPPASTTemplateId) compName).getTemplateName();
}
IBinding[] result = IBinding.EMPTY_BINDING_ARRAY;
int n = 0;
if ((!prefixLookup && CharArrayUtils.equals(c, compName.getLookupKey()))
|| (prefixLookup && ContentAssistMatcherFactory.getInstance().match(c, compName.getLookupKey()))) {
final IASTName lookupName = lookup.getLookupName();
if (shallReturnConstructors(lookupName, prefixLookup)) {
ICPPConstructor[] constructors = getConstructors(lookupName, lookup.isResolve());
result = addAll(result, constructors);
n += constructors.length;
}
// 9.2 ... The class-name is also inserted into the scope of the class itself.
result = appendAt(result, n++, compName.resolveBinding());
}
IBinding[] bindings = super.getBindings(lookup);
result = addAll(result, bindings);
n += bindings.length;
return trim(result, n);
}
static protected boolean shouldResolve(boolean force, IASTName candidate, IASTName forName) {
if (!force || candidate == forName)
return false;
if (forName == null)
return true;
if (!forName.isReference() && !CPPSemantics.declaredBefore(candidate, forName, false))
return false;
return true;
}
@Override
public ICPPConstructor[] getConstructors() {
return getConstructors(null, true);
}
private ICPPConstructor[] getConstructors(IASTName forName, boolean forceResolve) {
populateCache();
final CharArrayObjectMap<Object> nameMap = bindings;
if (nameMap == null)
return ICPPConstructor.EMPTY_CONSTRUCTOR_ARRAY;
Object o = nameMap.get(CONSTRUCTOR_KEY);
if (o != null) {
IBinding binding = null;
if (o instanceof ObjectSet<?>) {
ObjectSet<?> set = (ObjectSet<?>) o;
ICPPConstructor[] bs = ICPPConstructor.EMPTY_CONSTRUCTOR_ARRAY;
int n = 0;
for (int i = 0; i < set.size(); i++) {
Object obj = set.keyAt(i);
if (obj instanceof IASTName) {
IASTName name = (IASTName) obj;
binding = shouldResolve(forceResolve, name, forName) ?
name.resolveBinding() : name.getBinding();
if (binding instanceof ICPPConstructor) {
bs = appendAt(bs, n++, (ICPPConstructor) binding);
}
} else if (obj instanceof ICPPConstructor) {
bs = appendAt(bs, n++, (ICPPConstructor) obj);
}
}
return trim(bs, n);
} else if (o instanceof IASTName) {
if (shouldResolve(forceResolve, (IASTName) o, forName) || ((IASTName) o).getBinding() != null) {
// Always store the name, rather than the binding, so that we can properly flush the scope.
nameMap.put(CONSTRUCTOR_KEY, o);
binding = ((IASTName) o).resolveBinding();
}
} else if (o instanceof IBinding) {
binding = (IBinding) o;
}
if (binding != null && binding instanceof ICPPConstructor) {
return new ICPPConstructor[] { (ICPPConstructor) binding };
}
}
return ICPPConstructor.EMPTY_CONSTRUCTOR_ARRAY;
}
@Override
public IBinding[] find(String name, IASTTranslationUnit tu) {
return find(name);
}
@Override
public IBinding[] find(String name) {
char[] n = name.toCharArray();
ICPPASTCompositeTypeSpecifier compType = (ICPPASTCompositeTypeSpecifier) getPhysicalNode();
IASTName compName = compType.getName().getLastName();
if (compName instanceof ICPPASTTemplateId) {
compName= ((ICPPASTTemplateId) compName).getTemplateName();
}
if (CharArrayUtils.equals(compName.getLookupKey(), n)) {
return new IBinding[] { compName.resolveBinding() };
}
return super.find(name);
}
public static boolean shallReturnConstructors(IASTName name, boolean isPrefixLookup) {
if (name == null)
return false;
if (!isPrefixLookup) {
return CPPVisitor.isConstructorDeclaration(name)
|| CPPVisitor.isLastNameInUsingDeclaration(name);
}
IASTNode node = name.getParent();
if (node instanceof ICPPASTTemplateId)
return false;
if (node instanceof ICPPASTQualifiedName) {
if (((ICPPASTQualifiedName) node).getLastName() == name) {
node = node.getParent();
} else {
return false;
}
}
if (node instanceof IASTDeclSpecifier) {
IASTNode parent = node.getParent();
if (parent instanceof IASTTypeId && parent.getParent() instanceof ICPPASTNewExpression)
return true;
return false;
} else if (node instanceof IASTFieldReference) {
return false;
}
return true;
}
@Override
public ICPPClassType getClassType() {
ICPPASTCompositeTypeSpecifier compSpec = (ICPPASTCompositeTypeSpecifier) getPhysicalNode();
final IASTName name = compSpec.getName();
IBinding binding = name.resolveBinding();
if (binding instanceof ICPPClassType)
return (ICPPClassType) binding;
return new CPPClassType.CPPClassTypeProblem(name, ISemanticProblem.BINDING_NO_CLASS, name.toCharArray());
}
@Override
public ICPPMethod[] getImplicitMethods() {
if (implicits == null)
return ICPPMethod.EMPTY_CPPMETHOD_ARRAY;
return implicits;
}
@Override
public IName getScopeName() {
IASTNode node = getPhysicalNode();
if (node instanceof ICPPASTCompositeTypeSpecifier) {
return ((ICPPASTCompositeTypeSpecifier) node).getName();
}
return null;
}
}
|