Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 33a1d504d3345f3fe33d800abecedea7f0a6e7f8 (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
/*******************************************************************************
 * Copyright (c) 2007, 2011 QNX Software Systems 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:
 *     Bryan Wilkinson (QNX) - Initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.pdom.dom.cpp;

import org.eclipse.cdt.core.dom.ast.IASTNode;
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.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassSpecialization;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassTemplate;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassTemplatePartialSpecialization;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateArgument;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateInstance;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateNonTypeParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPDeferredClassInstance;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInstanceCache;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPTemplates;
import org.eclipse.cdt.internal.core.index.IIndexCPPBindingConstants;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMLinkage;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNode;
import org.eclipse.core.runtime.CoreException;

/**
 * Specialization of a class template.
 */
class PDOMCPPClassTemplateSpecialization extends PDOMCPPClassSpecialization 
		implements ICPPClassTemplate, ICPPInstanceCache {
	@SuppressWarnings("hiding")
	protected static final int RECORD_SIZE = PDOMCPPClassSpecialization.RECORD_SIZE;

	public PDOMCPPClassTemplateSpecialization(PDOMLinkage linkage, PDOMNode parent, ICPPClassTemplate template, PDOMBinding specialized)
			throws CoreException {
		super(linkage, parent, template, specialized);
	}

	public PDOMCPPClassTemplateSpecialization(PDOMLinkage linkage, long bindingRecord) {
		super(linkage, bindingRecord);
	}
	
	@Override
	protected int getRecordSize() {
		return RECORD_SIZE;
	}

	@Override
	public int getNodeType() {
		return IIndexCPPBindingConstants.CPP_CLASS_TEMPLATE_SPECIALIZATION;
	}
		
	@Override
	public ICPPTemplateParameter[] getTemplateParameters() {
		ICPPClassTemplate template = (ICPPClassTemplate) getSpecializedBinding();
		return template.getTemplateParameters();
	}

	@Override
	public ICPPTemplateInstance getInstance(ICPPTemplateArgument[] arguments) {
		return PDOMInstanceCache.getCache(this).getInstance(arguments);	
	}

	@Override
	public void addInstance(ICPPTemplateArgument[] arguments, ICPPTemplateInstance instance) {
		PDOMInstanceCache.getCache(this).addInstance(arguments, instance);	
	}

	@Override
	public ICPPTemplateInstance[] getAllInstances() {
		return PDOMInstanceCache.getCache(this).getAllInstances();	
	}
	
	@Override
	public boolean isSameType(IType type) {
		if( type == this )
			return true;

		if( type instanceof ITypedef )
			return type.isSameType( this );

		if (type instanceof PDOMNode) {
			PDOMNode node= (PDOMNode) type;
			if (node.getPDOM() == getPDOM()) {
				return node.getRecord() == getRecord();
			}
		}

		// require a class template specialization
		if (type instanceof ICPPClassSpecialization == false || 
				type instanceof ICPPTemplateDefinition == false || type instanceof IProblemBinding)
			return false;
		
		
		final ICPPClassSpecialization classSpec2 = (ICPPClassSpecialization) type;
		if (getKey() != classSpec2.getKey()) 
			return false;
		
		if (!CharArrayUtils.equals(getNameCharArray(), classSpec2.getNameCharArray()))
			return false;

		ICPPTemplateParameter[] params1= getTemplateParameters();
		ICPPTemplateParameter[] params2= ((ICPPClassTemplate) type).getTemplateParameters();

		if (params1 == params2)
			return true;

		if (params1 == null || params2 == null)
			return false;

		if (params1.length != params2.length)
			return false;

		for (int i = 0; i < params1.length; i++) {
			ICPPTemplateParameter p1= params1[i];
			ICPPTemplateParameter p2= params2[i];
			if (p1 instanceof IType && p2 instanceof IType) {
				IType t1= (IType) p1;
				IType t2= (IType) p2;
				if (!t1.isSameType(t2)) {
					return false;
				}
			} else if (p1 instanceof ICPPTemplateNonTypeParameter
					&& p2 instanceof ICPPTemplateNonTypeParameter) {
				IType t1= ((ICPPTemplateNonTypeParameter)p1).getType();
				IType t2= ((ICPPTemplateNonTypeParameter)p2).getType();
				if (t1 != t2) {
					if (t1 == null || t2 == null || !t1.isSameType(t2)) {
						return false;
					}
				}
			} else {
				return false;
			}
		}

		final IBinding owner1= getOwner();
		final IBinding owner2= classSpec2.getOwner();
		// for a specialization that is not an instance the owner has to be a class-type
		if (owner1 instanceof ICPPClassType == false || owner2 instanceof ICPPClassType == false)
			return false;

		return ((ICPPClassType) owner1).isSameType((ICPPClassType) owner2);
	}
	
	@Override
	public ICPPClassTemplatePartialSpecialization[] getPartialSpecializations() {
		IASTNode point= null; // Instantiation of dependent expressions may not work.
		ICPPClassTemplate origTemplate= (ICPPClassTemplate) getSpecializedBinding();
		ICPPClassTemplatePartialSpecialization[] orig = origTemplate.getPartialSpecializations();
		ICPPClassTemplatePartialSpecialization[] spec = new ICPPClassTemplatePartialSpecialization[orig.length];
		ICPPClassSpecialization owner = (ICPPClassSpecialization) getOwner();
		for (int i = 0; i < orig.length; i++) {
			spec[i]= (ICPPClassTemplatePartialSpecialization) owner.specializeMember(orig[i], point);
		}
		return spec;
	}
	
	@Override
	public final ICPPDeferredClassInstance asDeferredInstance() {
		PDOMInstanceCache cache= PDOMInstanceCache.getCache(this);
		synchronized (cache) {
			ICPPDeferredClassInstance dci= cache.getDeferredInstance();
			if (dci == null) {
				dci= CPPTemplates.createDeferredInstance(this);
				cache.putDeferredInstance(dci);
			}
			return dci;
		}
	}
}

Back to the top