Skip to main content
summaryrefslogtreecommitdiffstats
blob: d1d279b26283ea7a870856831a1af7802fe128ae (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*******************************************************************************
 * Copyright (c) 2012 Google, Inc 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:
 * 	   Sergey Prigogin (Google) - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp.semantics;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
import org.eclipse.cdt.core.dom.ast.DOMException;
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.ICPPBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassSpecialization;
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.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.parser.ISerializableEvaluation;
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.Value;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluation;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPUnknownBinding;
import org.eclipse.core.runtime.CoreException;

public abstract class CPPEvaluation implements ICPPEvaluation {

	private static class SignatureBuilder implements ITypeMarshalBuffer {
		private static final byte NULL_TYPE= 0;
		private static final byte UNSTORABLE_TYPE= (byte) -1;

		private final StringBuilder fBuffer;

		/**
		 * Constructor for input buffer.
		 */
		public SignatureBuilder() {
			fBuffer= new StringBuilder();
		}

		@Override
		public String toString() {
			return fBuffer.toString();
		}

		public char[] getSignature() {
			return CharArrayUtils.extractChars(fBuffer);
		}

		@Override
		public void marshalBinding(IBinding binding) throws CoreException {
			if (binding instanceof ISerializableType) {
				((ISerializableType) binding).marshal(this);
			} else if (binding == null) {
				putByte(NULL_TYPE);
			} else {
				appendSeparator();
				if (binding instanceof ICPPBinding) {
					if (binding instanceof ICPPTemplateParameter) {
						ICPPTemplateParameter param = (ICPPTemplateParameter) binding;
						fBuffer.append(param.isParameterPack() ? '*' : '#');
						fBuffer.append(param.getParameterID());
					} else {
						fBuffer.append(ASTTypeUtil.getQualifiedName((ICPPBinding) binding));
					}
				} else {
					fBuffer.append(binding.getNameCharArray());
				}
			}
		}

		@Override
		public void marshalType(IType type) throws CoreException {
			if (type instanceof ISerializableType) {
				((ISerializableType) type).marshal(this);
			} else if (type == null) {
				putByte(NULL_TYPE);
			} else if (type instanceof IBinding) {
				marshalBinding((IBinding) type);
			} else {
				assert false : "Cannot serialize " + ASTTypeUtil.getType(type) + " (" + type.getClass().getName() + ")"; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
				putByte(UNSTORABLE_TYPE);
			}
		}

		@Override
		public void marshalEvaluation(ISerializableEvaluation eval, boolean includeValues) throws CoreException {
			if (eval == null) {
				putByte(NULL_TYPE);
			} else {
				eval.marshal(this, includeValues);
			}
		}

		@Override
		public void marshalValue(IValue value) throws CoreException {
			if (value instanceof Value) {
				((Value) value).marshall(this);
			} else {
				putByte(NULL_TYPE);
			}
		}

		@Override
		public void marshalTemplateArgument(ICPPTemplateArgument arg) throws CoreException {
			if (arg.isNonTypeValue()) {
				putByte(VALUE);
				arg.getNonTypeEvaluation().marshal(this, true);
			} else {
				marshalType(arg.getTypeValue());
			}
		}

		@Override
		public void putByte(byte value) {
			appendSeparator();
			fBuffer.append(value);
		}

		@Override
		public void putFixedInt(int value) {
			appendSeparator();
			fBuffer.append(value);
		}

		@Override
		public void putInt(int value) {
			appendSeparator();
			fBuffer.append(value);
		}

		@Override
		public void putLong(long value) {
			appendSeparator();
			fBuffer.append(value);
		}

		@Override
		public void putCharArray(char[] chars) {
			appendSeparator();
			for (char c : chars) {
				fBuffer.append(c);
			}
		}

		private void appendSeparator() {
			if (fBuffer.length() != 0)
				fBuffer.append(' ');
		}

		@Override
		public IBinding unmarshalBinding() throws CoreException {
			throw new UnsupportedOperationException();
		}

		@Override
		public IType unmarshalType() throws CoreException {
			throw new UnsupportedOperationException();
		}

		@Override
		public ISerializableEvaluation unmarshalEvaluation() throws CoreException {
			throw new UnsupportedOperationException();
		}

		@Override
		public IValue unmarshalValue() throws CoreException {
			throw new UnsupportedOperationException();
		}

		@Override
		public ICPPTemplateArgument unmarshalTemplateArgument() throws CoreException {
			throw new UnsupportedOperationException();
		}

		@Override
		public int getByte() throws CoreException {
			throw new UnsupportedOperationException();
		}

		@Override
		public CoreException unmarshallingError() {
			throw new UnsupportedOperationException();
		}

		@Override
		public int getFixedInt() throws CoreException {
			throw new UnsupportedOperationException();
		}

		@Override
		public int getInt() throws CoreException {
			throw new UnsupportedOperationException();
		}

		@Override
		public long getLong() throws CoreException {
			throw new UnsupportedOperationException();
		}

		@Override
		public char[] getCharArray() throws CoreException {
			throw new UnsupportedOperationException();
		}
	}

	CPPEvaluation() {
	}

	@Override
	public char[] getSignature() {
		SignatureBuilder buf = new SignatureBuilder();
		try {
			marshal(buf, true);
		} catch (CoreException e) {
			CCorePlugin.log(e);
			return new char[] { '?' };
		}
		return buf.getSignature();
	}

	protected static IBinding resolveUnknown(ICPPUnknownBinding unknown, ICPPTemplateParameterMap tpMap,
			int packOffset, ICPPClassSpecialization within, IASTNode point) {
		try {
			return CPPTemplates.resolveUnknown(unknown, tpMap, packOffset, within, point);
		} catch (DOMException e) {
			CCorePlugin.log(e);
		}
		return unknown;
	}

	protected static ICPPTemplateArgument[] instantiateArguments(ICPPTemplateArgument[] args,
			ICPPTemplateParameterMap tpMap, int packOffset, ICPPClassSpecialization within, IASTNode point) {
		try {
			return CPPTemplates.instantiateArguments(args, tpMap, packOffset, within, point, false);
		} catch (DOMException e) {
			CCorePlugin.log(e);
		}
		return args;
	}
}

Back to the top