Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d57ef1a8a19db4976db2c120240227fd0cf9d3e3 (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
/*******************************************************************************
 * Copyright (c) 2017 Nathan Ridge.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp;

import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.IValue;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateArgument;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameterMap;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariableTemplate;
import org.eclipse.cdt.internal.core.dom.parser.ISerializableType;
import org.eclipse.cdt.internal.core.dom.parser.ITypeMarshalBuffer;
import org.eclipse.cdt.internal.core.dom.parser.IntegralValue;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPTemplates;
import org.eclipse.cdt.internal.core.index.IIndexFragment;
import org.eclipse.cdt.internal.core.pdom.dom.cpp.PDOMCPPDeferredVariableInstance;
import org.eclipse.core.runtime.CoreException;

/**
 * AST implementation of ICPPDeferredVariableInstance.
 */
public class CPPDeferredVariableInstance extends CPPUnknownBinding implements ICPPDeferredVariableInstance, 
		ISerializableType {
	private final ICPPVariableTemplate fTemplate;
	private final ICPPTemplateArgument[] fArguments;
	
	public CPPDeferredVariableInstance(ICPPVariableTemplate template, ICPPTemplateArgument[] arguments) {
		super(template.getNameCharArray());
		fTemplate = template;
		fArguments = arguments;
	}
	
	@Override
	public IBinding getOwner() {
		return fTemplate.getOwner();
	}

	@Override
	public ICPPVariableTemplate getSpecializedBinding() {
		return fTemplate;
	}

	@Override
	public ICPPTemplateParameterMap getTemplateParameterMap() {
		ICPPTemplateParameter[] params = fTemplate.getTemplateParameters();
		int size = Math.min(fArguments.length, params.length);
		CPPTemplateParameterMap map = new CPPTemplateParameterMap(size);
		for (int i = 0; i < size; i++) {
			map.put(params[i], fArguments[i]);
		}
		return map;
	}

	@Override
	public IType getType() {
		// The type cannot vary in partial specializations, so it's safe to use the primary template's type.
		InstantiationContext context = new InstantiationContext(getTemplateParameterMap(), null);
		return CPPTemplates.instantiateType(fTemplate.getType(), context);
	}

	@Override
	public IValue getInitialValue() {
		// Partial specializations can have different initial values, so we can't compute the initial value
		// until we have concrete template arguments and can select a partial specialization or the
		// primary template.
		return IntegralValue.UNKNOWN;
	}

	@Override
	public boolean isStatic() {
		return fTemplate.isStatic();
	}

	@Override
	public boolean isExtern() {
		return fTemplate.isExtern();
	}

	@Override
	public boolean isAuto() {
		return fTemplate.isAuto();
	}

	@Override
	public boolean isRegister() {
		return fTemplate.isRegister();
	}

	@Override
	public boolean isMutable() {
		return false;
	}

	@Override
	public boolean isConstexpr() {
		return fTemplate.isConstexpr();
	}

	@Override
	public boolean isExternC() {
		return fTemplate.isExternC();
	}

	@Override
	public ICPPVariableTemplate getTemplateDefinition() {
		return fTemplate;
	}

	@Override
	public ICPPTemplateArgument[] getTemplateArguments() {
		return fArguments;
	}

	@Override
	public boolean isExplicitSpecialization() {
		return false;
	}
	
	@Override
	public void marshal(ITypeMarshalBuffer buffer) throws CoreException {
		short firstBytes = ITypeMarshalBuffer.DEFERRED_VARIABLE_INSTANCE;
		buffer.putShort(firstBytes);
		buffer.marshalBinding(fTemplate);
		buffer.putInt(fArguments.length);
		for (ICPPTemplateArgument arg : fArguments) {
			buffer.marshalTemplateArgument(arg);
		}
	}
	
	public static ICPPDeferredVariableInstance unmarshal(IIndexFragment fragment, short firstBytes,
			ITypeMarshalBuffer buffer) throws CoreException {
		IBinding template= buffer.unmarshalBinding();
		int argcount= buffer.getInt();
		ICPPTemplateArgument[] args = new ICPPTemplateArgument[argcount];
		for (int i = 0; i < argcount; i++) {
			args[i]= buffer.unmarshalTemplateArgument();
		}
		return new PDOMCPPDeferredVariableInstance(fragment, (ICPPVariableTemplate) template, args);
	}
}

Back to the top