Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 26f6feb5f00b82d996384d8b1b99eb73b25fcb37 (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
/*******************************************************************************
 * Copyright (c) 2007, 2016 Symbian 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:
 *     Andrew Ferguson (Symbian) - Initial implementation
 *     Bryan Wilkinson (QNX)
 *     Markus Schorn (Wind River Systems)
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.index.composite.cpp;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPPartialSpecialization;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPSpecialization;
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.ICPPTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameterMap;
import org.eclipse.cdt.core.index.IIndexBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPTemplateNonTypeArgument;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPTemplateParameterMap;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPTemplateTypeArgument;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluation;
import org.eclipse.cdt.internal.core.index.IIndexFragmentBinding;
import org.eclipse.cdt.internal.core.index.composite.ICompositesFactory;

/**
 * For implementation re-use in the absence of multiple inheritance.
 */
public class TemplateInstanceUtil {

	public static ICPPTemplateParameterMap getTemplateParameterMap(ICompositesFactory cf, ICPPTemplateInstance rbinding) {
		ICPPTemplateParameterMap preresult= rbinding.getTemplateParameterMap();
		Integer[] keys= preresult.getAllParameterPositions();
		CPPTemplateParameterMap result= new CPPTemplateParameterMap(keys.length);

		try {
			for (Integer key : keys) {
				ICPPTemplateArgument arg= preresult.getArgument(key);
				if (arg != null) {
					result.put(key, convert(cf, arg));
				} else {
					ICPPTemplateArgument[] pack= preresult.getPackExpansion(key);
					result.put(key, convert(cf, pack));
				}
			}
		} catch (DOMException e) {
			CCorePlugin.log(e);
		}
		return result;
	}

	public static ICPPTemplateArgument[] getTemplateArguments(ICompositesFactory cf, ICPPTemplateInstance rbinding) {
		return convert(cf, rbinding.getTemplateArguments());
	}

	public static ICPPTemplateArgument[] getTemplateArguments(ICompositesFactory cf, ICPPPartialSpecialization rbinding) {
		return convert(cf, rbinding.getTemplateArguments());
	}

	public static IBinding getSpecializedBinding(ICompositesFactory cf, IIndexBinding rbinding) {
		IBinding preresult= ((ICPPSpecialization) rbinding).getSpecializedBinding();
		return cf.getCompositeBinding((IIndexFragmentBinding) preresult);
	}

	public static ICPPTemplateDefinition getTemplateDefinition(ICompositesFactory cf, IIndexBinding rbinding) {
		ICPPTemplateDefinition preresult= ((ICPPTemplateInstance)rbinding).getTemplateDefinition();
		return (ICPPTemplateDefinition) cf.getCompositeBinding((IIndexFragmentBinding)preresult);
	}

	public static ICPPTemplateArgument[] convert(ICompositesFactory cf, ICPPTemplateArgument[] arguments) {
		if (arguments == null)
			return null;
		try {
			ICPPTemplateArgument[] result= new ICPPTemplateArgument[arguments.length];
			for (int i = 0; i < arguments.length; i++) {
				result[i]= convert(cf, arguments[i]);
			}
			return result;
		} catch (DOMException e) {
			CCorePlugin.log(e);
		}
		return ICPPTemplateArgument.EMPTY_ARGUMENTS;
	}

	static ICPPTemplateArgument convert(ICompositesFactory cf, ICPPTemplateArgument arg) throws DOMException {
		if (arg == null)
			return null;
		if (arg.isTypeValue()) {
			final IType typeValue = arg.getTypeValue();
			IType t= cf.getCompositeType(typeValue);
			if (t != typeValue) {
				return new CPPTemplateTypeArgument(t);
			}
		} else {
			ICPPEvaluation eval = arg.getNonTypeEvaluation();
			ICPPEvaluation eval2 = ((CPPCompositesFactory) cf).getCompositeEvaluation(eval);
			if (eval2 != eval) {
				return new CPPTemplateNonTypeArgument(eval2);
			}
		}
		return arg;
	}

	public static ICPPTemplateParameter[] convert(ICompositesFactory cf, ICPPTemplateParameter[] preResult) {
		ICPPTemplateParameter[] result= new ICPPTemplateParameter[preResult.length];
		for (int i= 0; i < result.length; i++) {
			result[i]= (ICPPTemplateParameter) cf.getCompositeBinding((IIndexFragmentBinding) preResult[i]);
		}
		return result;
	}
}

Back to the top