1
/*******************************************************************************
2
* Copyright (c) 2007, 2011 QNX Software Systems 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
* Bryan Wilkinson (QNX) - Initial API and implementation
10
* Markus Schorn (Wind River Systems)
11
*******************************************************************************/
12
package org.eclipse.cdt.internal.core.pdom.dom.cpp;
14
import org.eclipse.cdt.core.dom.ast.DOMException;
15
import org.eclipse.cdt.core.dom.ast.IBinding;
16
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
17
import org.eclipse.cdt.core.dom.ast.IType;
18
import org.eclipse.cdt.core.dom.ast.ITypedef;
19
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassSpecialization;
20
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassTemplate;
21
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassTemplatePartialSpecialization;
22
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
23
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateArgument;
24
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateDefinition;
25
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateInstance;
26
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateNonTypeParameter;
27
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter;
28
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
29
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPDeferredClassInstance;
30
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPDeferredClassInstance;
31
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInstanceCache;
32
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPTemplates;
33
import org.eclipse.cdt.internal.core.index.IIndexCPPBindingConstants;
34
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding;
35
import org.eclipse.cdt.internal.core.pdom.dom.PDOMLinkage;
36
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNode;
37
import org.eclipse.core.runtime.CoreException;
40
* Specialization of a class template.
42
class PDOMCPPClassTemplateSpecialization extends PDOMCPPClassSpecialization
43
implements ICPPClassTemplate, ICPPInstanceCache {
45
@SuppressWarnings("hiding")
46
protected static final int RECORD_SIZE = PDOMCPPClassSpecialization.RECORD_SIZE;
48
public PDOMCPPClassTemplateSpecialization(PDOMLinkage linkage, PDOMNode parent, ICPPClassTemplate template, PDOMBinding specialized)
49
throws CoreException {
50
super(linkage, parent, template, specialized);
53
public PDOMCPPClassTemplateSpecialization(PDOMLinkage linkage, long bindingRecord) {
54
super(linkage, bindingRecord);
58
protected int getRecordSize() {
63
public int getNodeType() {
64
return IIndexCPPBindingConstants.CPP_CLASS_TEMPLATE_SPECIALIZATION;
67
public ICPPTemplateParameter[] getTemplateParameters() {
68
ICPPClassTemplate template = (ICPPClassTemplate) getSpecializedBinding();
69
return template.getTemplateParameters();
72
public ICPPTemplateInstance getInstance(ICPPTemplateArgument[] arguments) {
73
return PDOMInstanceCache.getCache(this).getInstance(arguments);
76
public void addInstance(ICPPTemplateArgument[] arguments, ICPPTemplateInstance instance) {
77
PDOMInstanceCache.getCache(this).addInstance(arguments, instance);
80
public ICPPTemplateInstance[] getAllInstances() {
81
return PDOMInstanceCache.getCache(this).getAllInstances();
85
public boolean isSameType(IType type) {
89
if( type instanceof ITypedef )
90
return type.isSameType( this );
92
if (type instanceof PDOMNode) {
93
PDOMNode node= (PDOMNode) type;
94
if (node.getPDOM() == getPDOM()) {
95
return node.getRecord() == getRecord();
99
// require a class template specialization
100
if (type instanceof ICPPClassSpecialization == false ||
101
type instanceof ICPPTemplateDefinition == false || type instanceof IProblemBinding)
105
final ICPPClassSpecialization classSpec2 = (ICPPClassSpecialization) type;
106
if (getKey() != classSpec2.getKey())
109
if (!CharArrayUtils.equals(getNameCharArray(), classSpec2.getNameCharArray()))
112
ICPPTemplateParameter[] params1= getTemplateParameters();
113
ICPPTemplateParameter[] params2= ((ICPPClassTemplate) type).getTemplateParameters();
115
if (params1 == params2)
118
if (params1 == null || params2 == null)
121
if (params1.length != params2.length)
124
for (int i = 0; i < params1.length; i++) {
125
ICPPTemplateParameter p1= params1[i];
126
ICPPTemplateParameter p2= params2[i];
127
if (p1 instanceof IType && p2 instanceof IType) {
128
IType t1= (IType) p1;
129
IType t2= (IType) p2;
130
if (!t1.isSameType(t2)) {
133
} else if (p1 instanceof ICPPTemplateNonTypeParameter
134
&& p2 instanceof ICPPTemplateNonTypeParameter) {
135
IType t1= ((ICPPTemplateNonTypeParameter)p1).getType();
136
IType t2= ((ICPPTemplateNonTypeParameter)p2).getType();
138
if (t1 == null || t2 == null || !t1.isSameType(t2)) {
147
final IBinding owner1= getOwner();
148
final IBinding owner2= classSpec2.getOwner();
149
// for a specialization that is not an instance the owner has to be a class-type
150
if (owner1 instanceof ICPPClassType == false || owner2 instanceof ICPPClassType == false)
153
return ((ICPPClassType) owner1).isSameType((ICPPClassType) owner2);
156
public ICPPClassTemplatePartialSpecialization[] getPartialSpecializations() throws DOMException {
157
ICPPClassTemplate origTemplate= (ICPPClassTemplate) getSpecializedBinding();
158
ICPPClassTemplatePartialSpecialization[] orig = origTemplate.getPartialSpecializations();
159
ICPPClassTemplatePartialSpecialization[] spec = new ICPPClassTemplatePartialSpecialization[orig.length];
160
for (int i = 0; i < orig.length; i++) {
161
spec[i]= (ICPPClassTemplatePartialSpecialization) specializeMember(orig[i]);
166
public ICPPDeferredClassInstance asDeferredInstance() throws DOMException {
167
PDOMInstanceCache cache= PDOMInstanceCache.getCache(this);
168
synchronized (cache) {
169
ICPPDeferredClassInstance dci= cache.getDeferredInstance();
171
dci= createDeferredInstance();
172
cache.putDeferredInstance(dci);
178
protected ICPPDeferredClassInstance createDeferredInstance() throws DOMException {
179
ICPPTemplateArgument[] args = CPPTemplates.templateParametersAsArguments(getTemplateParameters());
180
return new CPPDeferredClassInstance(this, args, getCompositeScope());