Skip to main content
summaryrefslogtreecommitdiffstats
blob: 66eef8c9c2878d9f0e68df62c5a7d495e1635d9c (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
184
185
/*******************************************************************************
* Copyright (c) 2016 Institute for Software, HSR Hochschule fuer Technik
* Rapperswil, University of applied sciences 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
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp.semantics;

import org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory;
import org.eclipse.cdt.core.dom.ast.IASTNode;
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.ICPPTemplateParameterMap;
import org.eclipse.cdt.internal.core.dom.parser.ITypeMarshalBuffer;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluation;
import org.eclipse.cdt.internal.core.dom.parser.cpp.InstantiationContext;
import org.eclipse.core.runtime.CoreException;

public class EvalReference extends CPPDependentEvaluation {
	protected final ActivationRecord owningRecord;

	// The following invariant must be true for instances of this class:
	// (referredBinding == null) != (referredSubValue == null)
	protected IBinding referredBinding;
	protected EvalCompositeAccess referredSubValue;

	public EvalReference(ActivationRecord owningRecord, IBinding referredBinding, IBinding templateDefinition) {
		super(templateDefinition);
		this.owningRecord = owningRecord;
		this.referredBinding = referredBinding;
	}

	EvalReference(ActivationRecord owningRecord, IBinding referredBinding, IASTNode point) {
		this(owningRecord, referredBinding, findEnclosingTemplate(point));
	}

	public EvalReference(ActivationRecord owningRecord, EvalCompositeAccess referredSubValue, IBinding templateDefinition) {
		super(templateDefinition);
		this.owningRecord = owningRecord;
		this.referredSubValue = referredSubValue;
		this.referredBinding = null;
	}

	EvalReference(ActivationRecord owningRecord, EvalCompositeAccess referredSubValue, IASTNode point) {
		this(owningRecord, referredSubValue, findEnclosingTemplate(point));
	}
	
	public ActivationRecord getOwningRecord() {
		return owningRecord;
	}
	
	public EvalCompositeAccess getReferredSubValue() {
		return referredSubValue;
	}

	@Override
	public final boolean isInitializerList() {
		return getTargetEvaluation().isInitializerList();
	}

	@Override
	public final boolean isFunctionSet() {
		return getTargetEvaluation().isFunctionSet();
	}

	@Override
	public final boolean isTypeDependent() {
		return getTargetEvaluation().isTypeDependent();
	}

	@Override
	public final boolean isValueDependent() {
		return getTargetEvaluation().isValueDependent();
	}

	@Override
	public final boolean isConstantExpression() {
		return getTargetEvaluation().isConstantExpression();
	}

	@Override
	public IType getType() {
		return getTargetEvaluation().getType();
	}

	@Override
	public IValue getValue() {
		return getTargetEvaluation().getValue();
	}

	public final ICPPEvaluation getTargetEvaluation() {
		if (referredSubValue != null) {
			return referredSubValue;
		}
		ICPPEvaluation targetValue = owningRecord.getVariable(referredBinding);
		return targetValue == null ? EvalFixed.INCOMPLETE : targetValue;
	}

	public void update(ICPPEvaluation eval) {
		if (referredBinding != null) {
			ICPPEvaluation oldValue = owningRecord.getVariable(referredBinding);
			if (oldValue instanceof EvalReference) {
				((EvalReference) oldValue).update(eval);
			} else {
				owningRecord.update(referredBinding, eval);
			}
		} else {
			referredSubValue.update(eval);
		}
	}

	public final IBinding getReferredBinding() {
		return referredBinding;
	}

	@Override
	public ValueCategory getValueCategory() {
		// An EvalReference always refers to a named variable, so its value category is lvalue.
		return ValueCategory.LVALUE;
	}

	@Override
	public ICPPEvaluation computeForFunctionCall(ActivationRecord record, ConstexprEvaluationContext context) {
		if (referredSubValue != null) {
			return referredSubValue.computeForFunctionCall(record, context);
		}
		ICPPEvaluation referredEval = owningRecord.getVariable(referredBinding);
		if (referredEval instanceof EvalReference) {
			// TODO(nathanridge): Why are we doing this for EvalReference only?
			return referredEval.computeForFunctionCall(record, context);
		}
		return referredEval;
	}

	@Override
	public int determinePackSize(ICPPTemplateParameterMap tpMap) {
		return getTargetEvaluation().determinePackSize(tpMap);
	}

	@Override
	public boolean referencesTemplateParameter() {
		return getTargetEvaluation().referencesTemplateParameter();
	}

	@Override
	public ICPPEvaluation instantiate(InstantiationContext context, int maxDepth) {
		// TODO(nathanridge): Why are we losing the EvalReference wrapper here?
		return getTargetEvaluation().instantiate(context, maxDepth);
	}

	@Override
	public void marshal(ITypeMarshalBuffer buffer, boolean includeValue) throws CoreException {
		short firstBytes = ITypeMarshalBuffer.EVAL_REFERENCE;
		if (referredSubValue != null) {
			firstBytes |= ITypeMarshalBuffer.FLAG1;
		}
		buffer.putShort(firstBytes);
		if (referredSubValue != null) {
			buffer.marshalEvaluation(referredSubValue, includeValue);
		} else {
			buffer.marshalBinding(referredBinding);
			buffer.marshalEvaluation(owningRecord.getVariable(referredBinding), includeValue);
		}
		marshalTemplateDefinition(buffer);
	}

	public static ICPPEvaluation unmarshal(short firstBytes, ITypeMarshalBuffer buffer) throws CoreException {
		boolean subValue = (firstBytes & ITypeMarshalBuffer.FLAG1) != 0;
		if (subValue) {
			EvalCompositeAccess referredSubValue = (EvalCompositeAccess) buffer.unmarshalEvaluation();
			IBinding templateDefinition = buffer.unmarshalBinding();
			return new EvalReference(new ActivationRecord(), referredSubValue, templateDefinition);
		} else {
			IBinding referredBinding = buffer.unmarshalBinding();
			ICPPEvaluation value = buffer.unmarshalEvaluation();
			ActivationRecord record = new ActivationRecord();
			record.update(referredBinding, value);
			IBinding templateDefinition = buffer.unmarshalBinding();
			return new EvalReference(record, referredBinding, templateDefinition);
		}
	}
}

Back to the top