Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3513329beeb210cb4758f57202b6c68236480299 (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*******************************************************************************
 * Copyright (c) 2006, 2013 IBM Corporation.
 * 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:
 *     IBM Corporation - initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *     Thomas Corbat (IFS)
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.pdom.dom.cpp;

import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPField;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMember;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable;
import org.eclipse.cdt.internal.core.dom.parser.ASTInternal;

class PDOMCPPAnnotations {
	private static final int VISIBILITY_OFFSET = 0;
	private static final int VISIBILITY_MASK = 0x03;
	private static final int EXTERN_OFFSET = 2;
	private static final int MUTABLE_OFFSET = 3;
	private static final int STATIC_OFFSET = 4;
	private static final int CONSTEXPR_OFFSET = 5;

	// "Inline" shares the same offset as "mutable" because
	// only fields can be mutable and only functions can be inline.
	private static final int INLINE_OFFSET = MUTABLE_OFFSET;
	// "extern C" shares the same offset as visibility because
	// only members have visibility and they cannot be extern C.
	private static final int EXTERN_C_OFFSET = VISIBILITY_OFFSET;

	// Function annotations start here.
	private static final int VARARGS_OFFSET = 6;
	private static final int PARAMETER_PACK_OFFSET = 7;
	private static final int DELETED_OFFSET = 8;
	private static final int NO_RETURN_OFFSET = 9;

	// Method annotations that don't fit on the first 16 bits of annotations.
	private static final int VIRTUAL_OFFSET = 0;
	private static final int DESTRUCTOR_OFFSET = 1;
	private static final int IMPLICIT_OFFSET = 2;
	private static final int EXPLICIT_OFFSET = 3;
	private static final int PURE_VIRTUAL_OFFSET = 4;
	private static final int OVERRIDE_OFFSET = 5;
	private static final int FINAL_OFFSET = 6;

	/**
	 * Encodes annotations applicable to C++ functions.
	 * 
	 * @param function the function whose annotations will be encoded
	 * @return a bit vector of the annotations
	 */	
	public static short encodeFunctionAnnotations(ICPPFunction function) {
		short annotation = encodeVisibility(function);

		if (function.isExtern())
			annotation |= 1 << EXTERN_OFFSET;
		if (ASTInternal.isStatic(function, false))
			annotation |= 1 << STATIC_OFFSET;
		if (function.isInline())
			annotation |= 1 << INLINE_OFFSET;
		if (function.takesVarArgs())
			annotation |= 1 << VARARGS_OFFSET;
		if (function.isNoReturn())
			annotation |= 1 << NO_RETURN_OFFSET;
		if (function.isExternC())
			annotation |= 1 << EXTERN_C_OFFSET;
		if (function.isConstexpr())
			annotation |= 1 << CONSTEXPR_OFFSET;
		if (function.hasParameterPack())
			annotation |= 1 << PARAMETER_PACK_OFFSET;
		if (function.isDeleted())
			annotation |= 1 << DELETED_OFFSET;

		return annotation;
	}

	/**
	 * Encodes annotations applicable to C++ variables.
	 * 
	 * @param variable the IBinding whose annotations will be encoded
	 * @return a bit vector of the annotations
	 */	
	public static byte encodeVariableAnnotations(ICPPVariable variable) {
		byte annotation = encodeVisibility(variable);

		if (variable.isExtern())
			annotation |= 1 << EXTERN_OFFSET;
		if (variable.isStatic())
			annotation |= 1 << STATIC_OFFSET;
		if (variable.isExternC())
			annotation |= 1 << EXTERN_C_OFFSET;
		if (variable.isConstexpr())
			annotation |= 1 << CONSTEXPR_OFFSET;
		if (variable instanceof ICPPField && ((ICPPField) variable).isMutable())
			annotation |= 1 << MUTABLE_OFFSET;

		return annotation;
	}

	private static byte encodeVisibility(ICPPBinding binding) {
		byte annotation = 0;
		if (binding instanceof ICPPMember) {
			ICPPMember member = (ICPPMember) binding;
			annotation = (byte) ((member.getVisibility() & VISIBILITY_MASK) << VISIBILITY_OFFSET);
		}
		return annotation;
	}

	/**
	 * Encodes extra annotations applicable to C++ methods.
	 * 
	 * @param binding the IBinding whose annotations will be encoded
	 * @return a bit vector of the annotation
	 */	
	public static byte encodeExtraMethodAnnotations(IBinding binding) {
		byte annotation = 0;
		if (binding instanceof ICPPMethod) {
			ICPPMethod method = (ICPPMethod) binding;
			if (method.isVirtual())
				annotation |= 1 << VIRTUAL_OFFSET;
			if (method.isDestructor())
				annotation |= 1 << DESTRUCTOR_OFFSET;
			if (method.isImplicit())
				annotation |= 1 << IMPLICIT_OFFSET;
			if (method.isPureVirtual())
				annotation |= 1 << PURE_VIRTUAL_OFFSET;
			if (method.isExplicit())
				annotation |= 1 << EXPLICIT_OFFSET;
			if (method.isOverride())
				annotation |= 1 << OVERRIDE_OFFSET;
			if (method.isFinal())
				annotation |= 1 << FINAL_OFFSET;
		}
		return annotation;
	}

	/**
	 * Unpacks visibility information from a bit vector of annotation.
	 *
	 * @param annotation the annotation containing visibility information.
	 * @return the visibility component of the annotation.
	 */
	public static int getVisibility(short annotation) {
		return (annotation >> VISIBILITY_OFFSET) & VISIBILITY_MASK;
	}

	/**
	 * Checks if the "extern" annotation is set.
	 */
	public static boolean isExtern(short annotation) {
		return (annotation & (1 << EXTERN_OFFSET)) != 0;
	}

	/**
	 * Checks if the "mutable" annotation is set.
	 */
	public static boolean isMutable(short annotation) {
		return (annotation & (1 << MUTABLE_OFFSET)) != 0;
	}

	/**
	 * Checks if the "static" annotation is set.
	 */
	public static boolean isStatic(short annotation) {
		return (annotation & (1 << STATIC_OFFSET)) != 0;
	}

	/**
	 * Checks if the "constexpr" annotation is set.
	 */
	public static boolean isConstexpr(short annotation) {
		return (annotation & (1 << CONSTEXPR_OFFSET)) != 0;
	}

	/**
	 * Checks if the "inline" annotation is set.
	 */
	public static boolean isInline(short annotation) {
		return (annotation & (1 << INLINE_OFFSET)) != 0;
	}

	/**
	 * Checks if the "extern C" annotation is set.
	 */
	public static boolean isExternC(short annotation) {
		return (annotation & (1 << EXTERN_C_OFFSET)) != 0;
	}

	/**
	 * Checks if the "varargs" annotation is set.
	 */
	public static boolean isVarargsFunction(short annotation) {
		return (annotation & (1 << VARARGS_OFFSET)) != 0;
	}

	/**
	 * Checks if the "has parameter pack" annotation is set.
	 */
	public static boolean hasParameterPack(short annotation) {
		return (annotation & (1 << PARAMETER_PACK_OFFSET)) != 0;
	}

	/**
	 * Checks if the "deleted" annotation is set.
	 */
	public static boolean isDeletedFunction(short annotation) {
		return (annotation & (1 << DELETED_OFFSET)) != 0;
	}

	/**
	 * Checks if the "no return" annotation is set.
	 */
	public static boolean isNoReturnFunction(short annotation) {
		return (annotation & (1 << NO_RETURN_OFFSET)) != 0;
	}

	/**
	 * Checks if the "virtual" annotation is set.
	 */
	public static boolean isVirtualMethod(byte annotation) {
		return (annotation & (1 << VIRTUAL_OFFSET)) != 0;
	}

	/**
	 * Checks if the "destructor" annotation is set.
	 */
	public static boolean isDestructor(byte annotation) {
		return (annotation & (1 << DESTRUCTOR_OFFSET)) != 0;
	}

	/**
	 * Checks if the "implicit" annotation is set.
	 */
	public static boolean isImplicitMethod(byte annotation) {
		return (annotation & (1 << IMPLICIT_OFFSET)) != 0;
	}

	/**
	 * Checks if the "explicit" annotation is set.
	 */
	public static boolean isExplicitMethod(byte annotation) {
		return (annotation & (1 << EXPLICIT_OFFSET)) != 0;
	}

	/**
	 * Checks if the "pure virtual " annotation is set.
	 */
	public static boolean isPureVirtualMethod(byte annotation) {
		return (annotation & (1 << PURE_VIRTUAL_OFFSET)) != 0;
	}

	/**
	 * Checks if the "override" annotation is set.
	 */
	public static boolean isOverrideMethod(byte annotation) {
		return (annotation & (1 << OVERRIDE_OFFSET)) != 0;
	}

	/**
	 * Checks if the "final" annotation is set.
	 */
	public static boolean isFinalMethod(byte annotation) {
		return (annotation & (1 << FINAL_OFFSET)) != 0;
	}

	/**
	 * Checks if the "explicit" annotation is set.
	 */
	public static byte clearImplicitMethodFlag(byte annotation) {
		return (byte) (annotation & ~(1 << IMPLICIT_OFFSET));
	}
}

Back to the top