Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c14ffb1d9b19dd86eff67be1ba15f7ec8d5b7fc4 (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
/*******************************************************************************
 *  Copyright (c) 2008, 2014 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *     Mike Kucera (IBM)
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp;

import java.util.Arrays;

import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeleteExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression;
import org.eclipse.cdt.core.parser.IToken;

/**
 * Enumeration of all the overloadable operators in C++.
 *
 * Note: toString() has not been overridden, use toCharArray() to get
 * a character representation of the operator.
 */
@SuppressWarnings("nls")
public enum OverloadableOperator {
	GT(">"),
	LT("<"),
	NOT("!"),
	BITCOMPLEMENT("~"),
	BITOR("|"),
	AMPER("&"),
	XOR("^"),
	MOD("%"),
	DIV("/"),
	STAR("*"),
	PLUS("+"),
	BRACKET("[]"),
	PAREN("()"),
	ARROW("->"),
	ARROWSTAR("->*"),
	COMMA(","),
	MINUS("-"),
	DECR("--"),
	INCR("++"),
	OR("||"),
	AND("&&"),
	ASSIGN("="),
	GTEQUAL(">="),
	LTEQUAL("<="),
	NOTEQUAL("!="),
	EQUAL("=="),
	SHIFTR(">>"),
	SHIFTL("<<"),
	SHIFTLASSIGN("<<="),
	SHIFTRASSIGN(">>="),
	BITORASSIGN("|="),
	AMPERASSIGN("&="),
	XORASSIGN("^="),
	MODASSIGN("%="),
	DIVASSIGN("/="),
	STARASSIGN("*="),
	MINUSASSIGN("-="),
	PLUSASSIGN("+="),
	NEW("new"),
	DELETE_ARRAY("delete[]"),
	DELETE("delete"),
	NEW_ARRAY("new[]"),

	/**
	 * Cannot be overloaded by the user, however overload resolution needs to be performed.
	 */
	CONDITIONAL_OPERATOR("?");

	private final char[] rep;

	private OverloadableOperator(String rep) {
		this.rep = ("operator " + rep).toCharArray();
	}

	public char[] toCharArray() {
		return rep;
	}

	/**
	 * Returns true if the given name equals 'operator new'
	 * or 'operator new[]'.
	 */
	public static boolean isNew(char[] name) {
		return Arrays.equals(name, NEW.rep) || Arrays.equals(name, NEW_ARRAY.rep);
	}

	/**
	 * Returns true if the given name equals 'operator delete'
	 * or 'operator delete[]'.
	 */
	public static boolean isDelete(char[] name) {
		return Arrays.equals(name, DELETE.rep) || Arrays.equals(name, DELETE_ARRAY.rep);
	}

	/**
	 * Returns the OverloadableOperator constant that corresponds to
	 * the given token. Does not work for new[] and delete[] operators.
	 *
	 * @throws NullPointerException if {@code token} is {@code null}.
	 */
	public static OverloadableOperator valueOf(IToken token) {
		switch (token.getType()) {
		case IToken.t_delete:       return DELETE;
		case IToken.t_new:          return NEW;
		case IToken.tAMPER:         return AMPER;
		case IToken.tAMPERASSIGN:   return AMPERASSIGN;
		case IToken.tARROW:         return ARROW;
		case IToken.tARROWSTAR:     return ARROWSTAR;
		case IToken.tBITOR:         return BITOR;
		case IToken.tBITORASSIGN:   return BITORASSIGN;
		case IToken.tBITCOMPLEMENT: return BITCOMPLEMENT;
		case IToken.tSHIFTL:        return SHIFTL;
		case IToken.tSHIFTLASSIGN:  return SHIFTLASSIGN;
		case IToken.tSHIFTR:        return SHIFTR;
		case IToken.tSHIFTRASSIGN:  return SHIFTRASSIGN;
		case IToken.tXOR:           return XOR;
		case IToken.tXORASSIGN:     return XORASSIGN;

        // logical operations
		case IToken.tNOT: return NOT;
		case IToken.tAND: return AND;
		case IToken.tOR:  return OR;

		// arithmetic
		case IToken.tDECR:        return DECR;
		case IToken.tINCR:        return INCR;
		case IToken.tDIV:         return DIV;
		case IToken.tDIVASSIGN:   return DIVASSIGN;
		case IToken.tMINUS:       return MINUS;
		case IToken.tMINUSASSIGN: return MINUSASSIGN;
		case IToken.tMOD:         return MOD;
		case IToken.tMODASSIGN:   return MODASSIGN;
		case IToken.tPLUS:        return PLUS;
		case IToken.tPLUSASSIGN:  return PLUSASSIGN;
		case IToken.tSTAR:        return STAR;
		case IToken.tSTARASSIGN:  return STARASSIGN;

		// comparison
		case IToken.tEQUAL:    return EQUAL;
		case IToken.tNOTEQUAL: return NOTEQUAL;
		case IToken.tGT:       return GT;
		case IToken.tGTEQUAL:  return GTEQUAL;
		case IToken.tLT:       return LT;
		case IToken.tLTEQUAL:  return LTEQUAL;

		// other
		case IToken.tASSIGN: return ASSIGN;
		case IToken.tCOMMA:  return COMMA;

		case IToken.tLBRACKET: return BRACKET;
		case IToken.tRBRACKET: return BRACKET;
		case IToken.tLPAREN: return PAREN;
		case IToken.tRPAREN: return PAREN;
		}

		return null;
	}

	/**
	 * Returns the OverloadableOperator constant that corresponds to
	 * the given binary expression.
	 *
	 * @throws NullPointerException if {@code expression} is {@code null}.
	 */
	public static OverloadableOperator fromBinaryExpression(int binaryOp) {
		switch (binaryOp) {
		case IASTBinaryExpression.op_binaryAnd:     	return AMPER;
		case IASTBinaryExpression.op_binaryAndAssign:   return AMPERASSIGN;
		case IASTBinaryExpression.op_pmarrow:         	return ARROWSTAR;
		case IASTBinaryExpression.op_binaryOr:			return BITOR;
		case IASTBinaryExpression.op_binaryOrAssign:	return BITORASSIGN;
		case IASTBinaryExpression.op_shiftLeft:         return SHIFTL;
		case IASTBinaryExpression.op_shiftLeftAssign:  	return SHIFTLASSIGN;
		case IASTBinaryExpression.op_shiftRight:        return SHIFTR;
		case IASTBinaryExpression.op_shiftRightAssign:  return SHIFTRASSIGN;
		case IASTBinaryExpression.op_binaryXor:         return XOR;
		case IASTBinaryExpression.op_binaryXorAssign:   return XORASSIGN;

        // logical operations
		case IASTBinaryExpression.op_logicalAnd: 		return AND;
		case IASTBinaryExpression.op_logicalOr:  		return OR;

		// arithmetic
		case IASTBinaryExpression.op_divide:        	return DIV;
		case IASTBinaryExpression.op_divideAssign:  	return DIVASSIGN;
		case IASTBinaryExpression.op_minus:       		return MINUS;
		case IASTBinaryExpression.op_minusAssign: 		return MINUSASSIGN;
		case IASTBinaryExpression.op_modulo:         	return MOD;
		case IASTBinaryExpression.op_moduloAssign:   	return MODASSIGN;
		case IASTBinaryExpression.op_plus:        		return PLUS;
		case IASTBinaryExpression.op_plusAssign:  		return PLUSASSIGN;
		case IASTBinaryExpression.op_multiply:        	return STAR;
		case IASTBinaryExpression.op_multiplyAssign:	return STARASSIGN;

		// comparison
		case IASTBinaryExpression.op_equals:    		return EQUAL;
		case IASTBinaryExpression.op_notequals: 		return NOTEQUAL;
		case IASTBinaryExpression.op_greaterThan:       return GT;
		case IASTBinaryExpression.op_greaterEqual:  	return GTEQUAL;
		case IASTBinaryExpression.op_lessThan:       	return LT;
		case IASTBinaryExpression.op_lessEqual:  		return LTEQUAL;

		// other
		case IASTBinaryExpression.op_assign: 			return ASSIGN;
		}

		return null;
	}

	public static OverloadableOperator fromUnaryExpression(int unaryOp) {
		switch (unaryOp) {
		case IASTUnaryExpression.op_prefixIncr:  return INCR;
		case IASTUnaryExpression.op_prefixDecr:  return DECR;
		case IASTUnaryExpression.op_plus:        return PLUS;
		case IASTUnaryExpression.op_minus:       return MINUS;
		case IASTUnaryExpression.op_star:        return STAR;
		case IASTUnaryExpression.op_amper:       return AMPER;
		case IASTUnaryExpression.op_tilde:       return BITCOMPLEMENT;
		case IASTUnaryExpression.op_not:         return NOT;
		case IASTUnaryExpression.op_postFixIncr: return INCR;
		case IASTUnaryExpression.op_postFixDecr: return DECR;
		}
		return null;
	}

	public static OverloadableOperator fromDeleteExpression(ICPPASTDeleteExpression expression) {
		return expression.isVectored() ? DELETE_ARRAY : DELETE;
	}

	public static OverloadableOperator fromNewExpression(ICPPASTNewExpression expression) {
		return expression.isArrayAllocation() ? NEW_ARRAY : NEW;
	}
}

Back to the top