1
/*******************************************************************************
2
* Copyright (c) 2005, 2010 IBM Corporation and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Rational Software - initial implementation
10
* Markus Schorn (Wind River Systems)
11
* Sergey Prigogin (Google)
12
*******************************************************************************/
13
package org.eclipse.cdt.core.dom.ast;
15
import org.eclipse.cdt.core.dom.ast.c.ICASTArrayDesignator;
16
import org.eclipse.cdt.core.dom.ast.c.ICASTArrayModifier;
17
import org.eclipse.cdt.core.dom.ast.c.ICASTDesignatedInitializer;
18
import org.eclipse.cdt.core.dom.ast.c.ICASTDesignator;
19
import org.eclipse.cdt.core.dom.ast.c.ICASTFieldDesignator;
20
import org.eclipse.cdt.core.dom.ast.c.ICASTSimpleDeclSpecifier;
21
import org.eclipse.cdt.core.dom.ast.c.ICASTTypeIdInitializerExpression;
22
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCastExpression;
23
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorInitializer;
24
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclSpecifier;
25
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeleteExpression;
26
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression;
27
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTPackExpansionExpression;
28
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTReferenceOperator;
29
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeConstructorExpression;
30
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTypeIdExpression;
31
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUnaryExpression;
32
import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTCompoundStatementExpression;
33
import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTTypeIdExpression;
34
import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator;
35
import org.eclipse.cdt.core.dom.ast.gnu.c.IGCCASTArrayRangeDesignator;
36
import org.eclipse.cdt.core.parser.Keywords;
37
import org.eclipse.cdt.internal.core.dom.parser.ASTProblem;
38
import org.eclipse.cdt.internal.core.model.ASTStringUtil;
41
* This is a utility class to help convert AST elements to Strings corresponding to the AST element's
44
* @noextend This interface is not intended to be extended by clients.
45
* @noinstantiate This class is not intended to be instantiated by clients.
46
* @deprecated The class is provided for testing purposes, only. It should not be used by clients.
47
* Within CDT it is recommended to use {@link ASTStringUtil}, instead.
50
public class ASTSignatureUtil {
52
private static final String COMMA_SPACE = ", "; //$NON-NLS-1$
53
private static final String EMPTY_STRING = ""; //$NON-NLS-1$
54
private static final String SPACE = " "; //$NON-NLS-1$
55
private static final String[] EMPTY_STRING_ARRAY = new String[0];
58
* Return's the String representation of a node's type (if available). This is currently only being used
61
* TODO Remove this function when done testing if it is no longer needed
65
public static String getNodeSignature(IASTNode node) {
66
if (node instanceof IASTDeclarator)
67
return getSignature((IASTDeclarator) node);
68
if (node instanceof IASTDeclSpecifier)
69
return getSignature((IASTDeclSpecifier) node);
70
if (node instanceof IASTTypeId)
71
return getSignature((IASTTypeId) node);
72
if (node instanceof IASTSimpleDeclaration) {
73
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) node;
74
StringBuffer buffer = new StringBuffer(getSignature(decl.getDeclSpecifier()));
76
IASTDeclarator[] declarators = decl.getDeclarators();
77
for (int i = 0; i < declarators.length; ++i) {
79
buffer.append(getSignature(declarators[i]));
80
if (declarators[i].getInitializer() != null
81
&& declarators[i].getInitializer() instanceof ICPPASTConstructorInitializer) {
82
buffer.append(getInitializerString(declarators[i].getInitializer()));
85
buffer.append(";"); //$NON-NLS-1$
86
return buffer.toString();
88
if (node instanceof IASTExpression) {
89
return getExpressionString((IASTExpression) node);
96
* Returns the parameter signature for an IASTDeclarator as a comma separated list wrapped in parenthesis.
98
* This method uses ASTSignatureUtil#getParametersSignatureArray(IASTArray) to build a comma separated
99
* list of the parameter's signatures and then wraps them in parenthesis.
102
* @return the parameter signature for an IASTDeclarator as a comma separated list wrapped in parenthesis
104
// if only function declarator's have parameters then change this to function declarator... should check
105
// before starting.. make my life easier!
106
public static String getParameterSignature(IASTDeclarator decltor) {
107
// should only be working with decltor that has parms
108
if (!(decltor instanceof IASTStandardFunctionDeclarator || decltor instanceof ICASTKnRFunctionDeclarator))
111
StringBuffer result = new StringBuffer();
113
String[] parms = getParameterSignatureArray(decltor);
115
result.append(Keywords.cpLPAREN);
116
for (int i = 0; i < parms.length; i++) {
117
if (parms[i] != null) {
118
result.append(parms[i]);
119
if (i < parms.length - 1)
120
result.append(COMMA_SPACE);
123
result.append(Keywords.cpRPAREN);
125
return result.toString();
129
* Returns a String[] corresponding to the signatures of individual parameters for an IASTDeclarator.
132
* @return a String[] corresponding to the signatures of individual parameters for an IASTDeclarator
134
public static String[] getParameterSignatureArray(IASTDeclarator decltor) {
135
// should only be working with decltor that has parms
136
if (!(decltor instanceof IASTStandardFunctionDeclarator || decltor instanceof ICASTKnRFunctionDeclarator))
137
return EMPTY_STRING_ARRAY;
139
String[] result = EMPTY_STRING_ARRAY;
141
if (decltor instanceof IASTStandardFunctionDeclarator) {
142
IASTParameterDeclaration[] parms = null;
143
parms = ((IASTStandardFunctionDeclarator) decltor).getParameters();
145
if (((IASTStandardFunctionDeclarator) decltor).takesVarArgs()) {
146
result = new String[parms.length + 1];
147
result[parms.length] = "..."; //$NON-NLS-1$
149
result = new String[parms.length];
152
for (int i = 0; i < parms.length; i++) {
153
if (parms[i] != null) {
154
result[i] = getSignature(parms[i].getDeclarator());
157
} else if (decltor instanceof ICASTKnRFunctionDeclarator) {
158
IASTName[] names = null;
159
names = ((ICASTKnRFunctionDeclarator) decltor).getParameterNames(); // required to get the order
160
// the parameters are used
162
result = new String[names.length];
164
for (int i = 0; i < names.length; i++) {
165
if (names[i] != null) {
166
final IASTDeclarator declaratorForParameterName = ((ICASTKnRFunctionDeclarator) decltor)
167
.getDeclaratorForParameterName(names[i]);
168
if (declaratorForParameterName != null)
169
result[i] = getSignature(declaratorForParameterName);
177
private static String getDeclaratorSpecificSignature(IASTDeclarator declarator) {
178
StringBuffer result = new StringBuffer();
179
IASTPointerOperator[] ops = declarator.getPointerOperators();
180
boolean needSpace = false;
182
if (ops != null && ops.length > 0) {
183
for (IASTPointerOperator op : ops) {
186
result.append(SPACE);
188
if (op instanceof IASTPointer) {
189
final IASTPointer ptr = (IASTPointer) op;
190
result.append(Keywords.cpSTAR); // want to have this before keywords on the pointer
194
result.append(SPACE);
196
result.append(Keywords.CONST);
199
if (ptr.isVolatile()) {
201
result.append(SPACE);
203
result.append(Keywords.VOLATILE);
206
if (ptr.isRestrict()) {
208
result.append(SPACE);
210
result.append(Keywords.RESTRICT);
215
if (op instanceof ICPPASTReferenceOperator) {
217
result.append(SPACE);
219
result.append(Keywords.cpAMPER);
226
if (declarator instanceof IASTArrayDeclarator) {
227
IASTArrayModifier[] mods = ((IASTArrayDeclarator) declarator).getArrayModifiers();
229
for (IASTArrayModifier mod : mods) {
232
result.append(SPACE);
235
result.append(Keywords.cpLBRACKET);
236
if (mod instanceof ICASTArrayModifier) {
237
if (((ICASTArrayModifier) mod).isConst()) {
239
result.append(SPACE);
242
result.append(Keywords.CONST);
245
if (((ICASTArrayModifier) mod).isRestrict()) {
247
result.append(SPACE);
250
result.append(Keywords.RESTRICT);
253
if (((ICASTArrayModifier) mod).isStatic()) {
255
result.append(SPACE);
258
result.append(Keywords.STATIC);
261
if (((ICASTArrayModifier) mod).isVolatile()) {
263
result.append(SPACE);
266
result.append(Keywords.VOLATILE);
269
result.append(Keywords.cpRBRACKET);
274
return result.toString();
277
private static String getDeclaratorSignature(IASTDeclarator declarator) {
278
if (declarator == null)
281
StringBuffer result = new StringBuffer();
283
result.append(getDeclaratorSpecificSignature(declarator));
285
if (declarator.getNestedDeclarator() != null) {
286
result.append(SPACE);
287
result.append(Keywords.cpLPAREN);
288
result.append(getDeclaratorSignature(declarator.getNestedDeclarator()));
289
result.append(Keywords.cpRPAREN);
292
// append the parameter's signatures
293
result.append(getParameterSignature(declarator));
295
return result.toString();
299
* This function is used to return the signature of an IASTInitializer.
301
* TODO this function is used for testing and probably should not public once this Utility class has been
302
* finalized as it will likely never be used publicly except for testing
305
* @return the signature of an IASTInitializer
307
public static String getInitializerString(IASTInitializer init) {
308
StringBuffer result = new StringBuffer();
310
if (init instanceof IASTEqualsInitializer) {
311
result.append(Keywords.cpASSIGN);
312
result.append(getInitializerClauseString(((IASTEqualsInitializer) init).getInitializerClause()));
313
} else if (init instanceof IASTInitializerList) {
314
result.append(Keywords.cpLBRACE);
315
appendExpressionList(result, ((IASTInitializerList) init).getClauses());
316
result.append(Keywords.cpRBRACE);
317
} else if (init instanceof ICASTDesignatedInitializer) {
318
ICASTDesignator[] designators = ((ICASTDesignatedInitializer) init).getDesignators();
319
for (int i = 0; i < designators.length; i++) {
320
result.append(getDesignatorSignature(designators[i]));
321
if (i < designators.length - 1)
322
result.append(COMMA_SPACE);
324
result.append(Keywords.cpASSIGN);
325
result.append(getInitializerClauseString(((ICASTDesignatedInitializer) init).getOperand()));
326
} else if (init instanceof ICPPASTConstructorInitializer) {
327
result.append("("); //$NON-NLS-1$
328
appendExpressionList(result, ((ICPPASTConstructorInitializer) init).getArguments());
329
result.append(")"); //$NON-NLS-1$
332
return result.toString();
335
private static void appendExpressionList(StringBuffer result, IASTInitializerClause[] inits) {
336
for (int i = 0; i < inits.length; i++) {
337
result.append(getInitializerClauseString(inits[i]));
338
if (i < inits.length - 1)
339
result.append(COMMA_SPACE);
343
private static String getInitializerClauseString(IASTInitializerClause initializerClause) {
344
if (initializerClause instanceof IASTExpression) {
345
return getExpressionString((IASTExpression) initializerClause);
347
if (initializerClause instanceof IASTInitializer) {
348
return getInitializerString((IASTInitializer) initializerClause);
350
return ""; //$NON-NLS-1$
353
private static String getDesignatorSignature(ICASTDesignator designator) {
354
StringBuffer result = new StringBuffer();
356
if (designator instanceof ICASTArrayDesignator) {
357
result.append(Keywords.cpLBRACKET);
358
result.append(getExpressionString(((ICASTArrayDesignator) designator).getSubscriptExpression()));
359
result.append(Keywords.cpRBRACKET);
360
} else if (designator instanceof ICASTFieldDesignator) {
361
result.append(Keywords.cpDOT);
362
result.append(((ICASTFieldDesignator) designator).getName().toString());
363
} else if (designator instanceof IGCCASTArrayRangeDesignator) {
364
result.append(Keywords.cpLBRACKET);
365
result.append(getExpressionString(((IGCCASTArrayRangeDesignator) designator).getRangeFloor()));
366
result.append(SPACE);
367
result.append(Keywords.cpELLIPSIS);
368
result.append(SPACE);
369
result.append(getExpressionString(((IGCCASTArrayRangeDesignator) designator).getRangeCeiling()));
370
result.append(Keywords.cpRBRACKET);
373
return result.toString();
377
* Returns the String signature corresponding to an IASTDeclarator. This includes the signature of the
378
* parameters which is built via ASTSignatureUtil#getParameterSignature(IASTDeclarator) if the declarator
382
* @return the String signature corresponding to an IASTDeclarator
384
public static String getSignature(IASTDeclarator declarator) {
385
StringBuffer result = new StringBuffer();
388
IASTDeclSpecifier declSpec = null;
390
IASTNode node = declarator.getParent();
391
while (node instanceof IASTDeclarator) {
392
declarator = (IASTDeclarator) node;
393
node = node.getParent();
396
if (node instanceof IASTParameterDeclaration)
397
declSpec = ((IASTParameterDeclaration) node).getDeclSpecifier();
398
else if (node instanceof IASTSimpleDeclaration)
399
declSpec = ((IASTSimpleDeclaration) node).getDeclSpecifier();
400
else if (node instanceof IASTFunctionDefinition)
401
declSpec = ((IASTFunctionDefinition) node).getDeclSpecifier();
402
else if (node instanceof IASTTypeId)
403
declSpec = ((IASTTypeId) node).getDeclSpecifier();
405
// append the declSpec's signature to the signature
406
String specString = getSignature(declSpec);
407
if (specString != null && !specString.equals(EMPTY_STRING))
408
result.append(specString);
410
// append the declarator's signature (without specifier)
411
String decltorString = getDeclaratorSignature(declarator);
412
if (specString != null && specString.length() > 0 && decltorString != null
413
&& decltorString.length() > 0) {
414
result.append(SPACE);
416
result.append(decltorString);
418
return result.toString();
422
* Returns the String representation of the signature for the IASTDeclSpecifier.
425
* @return the String representation of the signature for the IASTDeclSpecifier
427
public static String getSignature(IASTDeclSpecifier declSpec) {
428
if (declSpec == null)
430
boolean needSpace = false;
432
StringBuffer result = new StringBuffer();
434
if (declSpec.getStorageClass() == IASTDeclSpecifier.sc_mutable) {
435
result.append(Keywords.MUTABLE);
438
if (declSpec.getStorageClass() == IASTDeclSpecifier.sc_auto) {
440
result.append(SPACE);
443
result.append(Keywords.AUTO);
446
if (declSpec.getStorageClass() == IASTDeclSpecifier.sc_extern) {
448
result.append(SPACE);
451
result.append(Keywords.EXTERN);
454
if (declSpec.getStorageClass() == IASTDeclSpecifier.sc_register) {
456
result.append(SPACE);
459
result.append(Keywords.REGISTER);
462
if (declSpec.getStorageClass() == IASTDeclSpecifier.sc_static) {
464
result.append(SPACE);
467
result.append(Keywords.STATIC);
470
if (declSpec.getStorageClass() == IASTDeclSpecifier.sc_typedef) {
472
result.append(SPACE);
475
result.append(Keywords.TYPEDEF);
479
if (declSpec.isConst()) {
481
result.append(SPACE);
484
result.append(Keywords.CONST);
487
if (declSpec.isInline()) {
489
result.append(SPACE);
492
result.append(Keywords.INLINE);
495
if (declSpec.isRestrict()) {
497
result.append(SPACE);
500
result.append(Keywords.RESTRICT);
503
if (declSpec.isVolatile()) {
505
result.append(SPACE);
508
result.append(Keywords.VOLATILE);
512
if (declSpec instanceof ICPPASTDeclSpecifier) {
513
if (((ICPPASTDeclSpecifier) declSpec).isExplicit()) {
515
result.append(SPACE);
518
result.append(Keywords.EXPLICIT);
521
if (((ICPPASTDeclSpecifier) declSpec).isFriend()) {
523
result.append(SPACE);
526
result.append(Keywords.FRIEND);
529
if (((ICPPASTDeclSpecifier) declSpec).isVirtual()) {
531
result.append(SPACE);
534
result.append(Keywords.VIRTUAL);
539
// handle complex cases
540
if (declSpec instanceof IASTCompositeTypeSpecifier) {
541
// 101114 fix, do not display class, and for consistency don't display struct/union as well
542
// if (declSpec instanceof ICPPASTCompositeTypeSpecifier) {
543
// switch(((ICPPASTCompositeTypeSpecifier)declSpec).getKey()) {
544
// case ICPPASTCompositeTypeSpecifier.k_class:
545
// if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.CLASS);
548
// case IASTCompositeTypeSpecifier.k_struct:
549
// if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.STRUCT);
552
// case IASTCompositeTypeSpecifier.k_union:
553
// if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.UNION);
557
// } else if (declSpec instanceof ICASTCompositeTypeSpecifier) {
558
// switch(((ICASTCompositeTypeSpecifier)declSpec).getKey()) {
559
// case IASTCompositeTypeSpecifier.k_struct:
560
// if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.STRUCT);
563
// case IASTCompositeTypeSpecifier.k_union:
564
// if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.UNION);
570
result.append(((IASTCompositeTypeSpecifier) declSpec).getName());
571
} else if (declSpec instanceof IASTElaboratedTypeSpecifier) {
572
// 101114 fix, do not display class, and for consistency don't display struct/union as well
573
// switch(((IASTElaboratedTypeSpecifier)declSpec).getKind()) {
574
// case ICPPASTElaboratedTypeSpecifier.k_class:
575
// if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.CLASS);
578
// case IASTElaboratedTypeSpecifier.k_enum:
579
// if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.ENUM);
582
// case IASTElaboratedTypeSpecifier.k_struct:
583
// if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.STRUCT);
586
// case IASTElaboratedTypeSpecifier.k_union:
587
// if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.UNION);
592
result.append(SPACE);
595
result.append(((IASTElaboratedTypeSpecifier) declSpec).getName());
596
} else if (declSpec instanceof IASTEnumerationSpecifier) {
598
result.append(SPACE);
601
result.append(Keywords.ENUM);
603
} else if (declSpec instanceof IASTNamedTypeSpecifier) {
605
result.append(SPACE);
608
result.append(((IASTNamedTypeSpecifier) declSpec).getName().toString());
610
} else if (declSpec instanceof IASTSimpleDeclSpecifier) {
611
final IASTSimpleDeclSpecifier sds = (IASTSimpleDeclSpecifier) declSpec;
612
if (sds.isLongLong()) {
614
result.append(SPACE);
617
result.append(Keywords.LONG_LONG);
620
if (sds.isComplex()) {
622
result.append(SPACE);
625
result.append(Keywords.c_COMPLEX);
628
if (sds.isImaginary()) {
630
result.append(SPACE);
633
result.append(Keywords.c_IMAGINARY);
638
result.append(SPACE);
641
result.append(Keywords.LONG);
646
result.append(SPACE);
649
result.append(Keywords.SHORT);
652
if (sds.isSigned()) {
654
result.append(SPACE);
657
result.append(Keywords.SIGNED);
660
if (sds.isUnsigned()) {
662
result.append(SPACE);
665
result.append(Keywords.UNSIGNED);
669
switch (sds.getType()) {
670
case IASTSimpleDeclSpecifier.t_typeof:
672
result.append(SPACE);
675
result.append(Keywords.TYPEOF);
678
case IASTSimpleDeclSpecifier.t_decltype:
680
result.append(SPACE);
683
result.append(Keywords.cDECLTYPE);
686
case IASTSimpleDeclSpecifier.t_auto:
688
result.append(SPACE);
691
result.append(Keywords.cAUTO);
694
case IASTSimpleDeclSpecifier.t_bool:
696
result.append(SPACE);
699
if (declSpec instanceof ICASTSimpleDeclSpecifier) {
700
result.append(Keywords.c_BOOL);
702
result.append(Keywords.BOOL);
706
case IASTSimpleDeclSpecifier.t_char:
708
result.append(SPACE);
711
result.append(Keywords.CHAR);
714
case IASTSimpleDeclSpecifier.t_wchar_t:
716
result.append(SPACE);
719
result.append(Keywords.WCHAR_T);
722
case IASTSimpleDeclSpecifier.t_char16_t:
724
result.append(SPACE);
727
result.append(Keywords.CHAR16_T);
730
case IASTSimpleDeclSpecifier.t_char32_t:
732
result.append(SPACE);
735
result.append(Keywords.CHAR32_T);
738
case IASTSimpleDeclSpecifier.t_double:
740
result.append(SPACE);
743
result.append(Keywords.DOUBLE);
746
case IASTSimpleDeclSpecifier.t_float:
748
result.append(SPACE);
751
result.append(Keywords.FLOAT);
754
case IASTSimpleDeclSpecifier.t_int:
756
result.append(SPACE);
759
result.append(Keywords.INT);
762
case IASTSimpleDeclSpecifier.t_void:
764
result.append(SPACE);
767
result.append(Keywords.VOID);
773
return result.toString();
777
* Returns the String representation of the signature for the IASTTypeId.
780
* @return the String representation of the signature for the IASTTypeId
782
public static String getSignature(IASTTypeId typeId) {
783
return getSignature(typeId.getAbstractDeclarator());
787
* Return a string representation for the given IASTExpression. Expressions having an extension kind
788
* should provide their own toString method which will be called by this.
791
* @return a string representation for the given IASTExpression
793
public static String getExpressionString( IASTExpression expression ){
794
if (expression instanceof IASTArraySubscriptExpression)
795
return getArraySubscriptExpression((IASTArraySubscriptExpression)expression);
796
else if (expression instanceof IASTBinaryExpression)
797
return getBinaryExpression( (IASTBinaryExpression)expression );
798
else if (expression instanceof IASTCastExpression)
799
return getCastExpression((IASTCastExpression)expression);
800
else if (expression instanceof IASTConditionalExpression)
801
return getConditionalExpression((IASTConditionalExpression)expression);
802
else if (expression instanceof IASTExpressionList)
803
return getExpressionList((IASTExpressionList)expression);
804
else if (expression instanceof IASTFieldReference)
805
return getFieldReference((IASTFieldReference)expression);
806
else if (expression instanceof IASTFunctionCallExpression)
807
return getFunctionCallExpression((IASTFunctionCallExpression)expression);
808
else if (expression instanceof IASTIdExpression)
809
return getIdExpression((IASTIdExpression)expression);
810
else if (expression instanceof IASTLiteralExpression)
811
return getLiteralExpression((IASTLiteralExpression)expression);
812
else if (expression instanceof IASTTypeIdExpression)
813
return getTypeIdExpression( (IASTTypeIdExpression)expression );
814
else if (expression instanceof IASTUnaryExpression)
815
return getUnaryExpression( (IASTUnaryExpression)expression );
816
else if (expression instanceof ICASTTypeIdInitializerExpression)
817
return getTypeIdInitializerExpression((ICASTTypeIdInitializerExpression)expression);
818
else if (expression instanceof ICPPASTDeleteExpression)
819
return getDeleteExpression((ICPPASTDeleteExpression)expression);
820
else if (expression instanceof ICPPASTNewExpression)
821
return getNewExpression((ICPPASTNewExpression)expression);
822
else if (expression instanceof ICPPASTSimpleTypeConstructorExpression)
823
return getSimpleTypeConstructorExpression((ICPPASTSimpleTypeConstructorExpression)expression);
824
else if (expression instanceof IGNUASTCompoundStatementExpression)
825
return getCompoundStatementExpression((IGNUASTCompoundStatementExpression)expression);
826
else if (expression instanceof ICPPASTPackExpansionExpression)
827
return getPackExpansionExpression((ICPPASTPackExpansionExpression) expression);
829
return getEmptyExpression( expression );
832
private static String getArraySubscriptExpression(IASTArraySubscriptExpression expression) {
833
StringBuffer result = new StringBuffer();
834
result.append(getExpressionString(expression.getArrayExpression()));
835
result.append(Keywords.cpLBRACKET);
836
result.append(getInitializerClauseString(expression.getArgument()));
837
result.append(Keywords.cpRBRACKET);
838
return result.toString();
841
private static String getCastExpression(IASTCastExpression expression) {
842
StringBuffer result = new StringBuffer();
843
boolean normalCast = false;
845
if (expression.getOperator() == IASTCastExpression.op_cast)
849
result.append(Keywords.cpLPAREN);
850
result.append(getSignature(expression.getTypeId()));
851
result.append(Keywords.cpRPAREN);
852
result.append(getExpressionString(expression.getOperand()));
854
result.append(getCastOperatorString(expression));
855
result.append(Keywords.cpLT);
856
result.append(getSignature(expression.getTypeId()));
857
result.append(Keywords.cpGT);
858
result.append(Keywords.cpLPAREN);
859
result.append(getExpressionString(expression.getOperand()));
860
result.append(Keywords.cpRPAREN);
863
return result.toString();
866
private static String getFieldReference(IASTFieldReference expression) {
867
StringBuffer result = new StringBuffer();
868
result.append(getExpressionString(expression.getFieldOwner()));
869
if (expression.isPointerDereference())
870
result.append(Keywords.cpARROW);
872
result.append(Keywords.cpDOT);
874
result.append(expression.getFieldName().toString());
875
return result.toString();
878
private static String getFunctionCallExpression(IASTFunctionCallExpression expression) {
879
StringBuffer result = new StringBuffer();
880
result.append(getExpressionString(expression.getFunctionNameExpression()));
881
result.append(Keywords.cpLPAREN);
882
IASTInitializerClause[] clauses = expression.getArguments();
883
for (int i= 0; i < clauses.length; i++) {
885
result.append(COMMA_SPACE);
887
result.append(getInitializerClauseString(clauses[i]));
889
result.append(Keywords.cpRPAREN);
890
return result.toString();
893
private static String getTypeIdInitializerExpression(ICASTTypeIdInitializerExpression expression) {
894
StringBuffer result = new StringBuffer();
895
result.append(Keywords.cpLPAREN);
896
result.append(getSignature(expression.getTypeId()));
897
result.append(Keywords.cpRPAREN);
898
result.append(getInitializerString(expression.getInitializer()));
899
return result.toString();
902
private static String getDeleteExpression(ICPPASTDeleteExpression expression) {
903
StringBuffer result = new StringBuffer();
904
result.append(Keywords.DELETE);
905
result.append(SPACE);
906
if (expression.getOperand() != null)
907
result.append(getExpressionString(expression.getOperand()));
908
return result.toString();
911
private static String getSimpleTypeConstructorExpression(ICPPASTSimpleTypeConstructorExpression expression) {
912
StringBuffer result = new StringBuffer();
913
result.append(getSignature(expression.getDeclSpecifier()));
914
result.append(getInitializerString(expression.getInitializer()));
915
return result.toString();
918
private static String getCompoundStatementExpression(IGNUASTCompoundStatementExpression expression) {
919
return String.valueOf(Keywords.cpELLIPSIS); // TODO might need to getSignature(IASTStatement) in the
923
private static String getTypeIdExpression(IASTTypeIdExpression expression) {
924
StringBuffer result = new StringBuffer();
925
String operator = getTypeIdExpressionOperator(expression);
926
if (operator != null && !operator.equals(EMPTY_STRING))
927
result.append(operator);
929
if (operator != null && !operator.equals(EMPTY_STRING)) {
930
result.append(SPACE);
931
result.append(Keywords.cpLPAREN);
933
result.append(getSignature(expression.getTypeId()));
934
if (operator != null && !operator.equals(EMPTY_STRING))
935
result.append(Keywords.cpRPAREN);
936
return result.toString();
939
private static String getExpressionList(IASTExpressionList expression) {
940
StringBuffer result = new StringBuffer();
941
IASTExpression[] exps = expression.getExpressions();
942
if (exps != null && exps.length > 0) {
943
for (int i = 0; i < exps.length; i++) {
944
result.append(getExpressionString(exps[i]));
945
if (i < exps.length - 1) {
946
result.append(COMMA_SPACE);
950
return result.toString();
953
private static String getEmptyExpression(IASTExpression expression) {
957
private static String getLiteralExpression(IASTLiteralExpression expression) {
958
return expression.toString();
961
private static String getIdExpression(IASTIdExpression expression) {
962
return expression.getName().toString();
965
private static String getConditionalExpression(IASTConditionalExpression expression) {
966
StringBuffer result = new StringBuffer();
967
result.append(getExpressionString(expression.getLogicalConditionExpression()));
968
result.append(SPACE);
969
result.append(Keywords.cpQUESTION);
970
result.append(SPACE);
971
final IASTExpression positiveExpression = expression.getPositiveResultExpression();
972
if (positiveExpression != null) {
973
result.append(getExpressionString(positiveExpression));
974
result.append(SPACE);
976
result.append(Keywords.cpCOLON);
977
result.append(SPACE);
978
result.append(getExpressionString(expression.getNegativeResultExpression()));
979
return result.toString();
982
private static String getNewExpression(ICPPASTNewExpression expression) {
983
StringBuffer result = new StringBuffer();
984
result.append(Keywords.NEW);
985
result.append(SPACE);
986
final IASTInitializerClause[] args = expression.getPlacementArguments();
988
result.append("("); //$NON-NLS-1$
989
appendExpressionList(result, args);
990
result.append(")"); //$NON-NLS-1$
992
result.append(getSignature(expression.getTypeId()));
993
final IASTInitializer initializer = expression.getInitializer();
994
if (initializer != null)
995
result.append(getInitializerString(initializer));
996
return result.toString();
999
private static String getBinaryExpression(IASTBinaryExpression expression) {
1000
StringBuffer buffer = new StringBuffer();
1001
buffer.append(getExpressionString(expression.getOperand1()));
1002
buffer.append(SPACE);
1003
buffer.append(getBinaryOperatorString(expression));
1004
buffer.append(SPACE);
1005
buffer.append(getExpressionString(expression.getOperand2()));
1006
return buffer.toString();
1009
private static String getUnaryExpression(IASTUnaryExpression expression) {
1010
StringBuffer buffer = new StringBuffer();
1011
boolean postOperator = false;
1012
boolean primaryBracketed = false;
1014
switch (expression.getOperator()) {
1015
case IASTUnaryExpression.op_postFixDecr:
1016
case IASTUnaryExpression.op_postFixIncr:
1017
postOperator = true;
1019
case IASTUnaryExpression.op_bracketedPrimary:
1020
primaryBracketed = true;
1023
postOperator = false;
1027
if (!postOperator && !primaryBracketed)
1028
buffer.append(getUnaryOperatorString(expression));
1030
// need to add a space to the unary expression if it is a specific operator
1031
switch (expression.getOperator()) {
1032
case IASTUnaryExpression.op_sizeof:
1033
case ICPPASTUnaryExpression.op_throw:
1034
case ICPPASTUnaryExpression.op_typeid:
1035
buffer.append(SPACE);
1039
if (primaryBracketed)
1040
buffer.append(Keywords.cpLPAREN);
1041
buffer.append(getExpressionString(expression.getOperand()));
1042
if (primaryBracketed)
1043
buffer.append(Keywords.cpRPAREN);
1044
if (postOperator && !primaryBracketed)
1045
buffer.append(getUnaryOperatorString(expression));
1047
return buffer.toString();
1051
* Returns the String representation of the IASTCastExpression's operator.
1054
* @return the String representation of the IASTCastExpression's operator
1056
public static String getCastOperatorString(IASTCastExpression expression) {
1057
int op = expression.getOperator();
1058
String opString = EMPTY_STRING;
1060
if (expression instanceof ICPPASTCastExpression) {
1062
case ICPPASTCastExpression.op_const_cast:
1063
opString = Keywords.CONST_CAST;
1065
case ICPPASTCastExpression.op_dynamic_cast:
1066
opString = Keywords.DYNAMIC_CAST;
1068
case ICPPASTCastExpression.op_reinterpret_cast:
1069
opString = Keywords.REINTERPRET_CAST;
1071
case ICPPASTCastExpression.op_static_cast:
1072
opString = Keywords.STATIC_CAST;
1079
if (!opString.equals(EMPTY_STRING))
1083
case IASTCastExpression.op_cast:
1084
opString = Keywords.CAST;
1092
* Returns the String representation of the IASTUnaryExpression's operator.
1095
* @return the String representation of the IASTUnaryExpression's operator
1097
public static String getUnaryOperatorString(IASTUnaryExpression ue) {
1098
int op = ue.getOperator();
1099
String opString = EMPTY_STRING;
1101
if (ue instanceof ICPPASTUnaryExpression) {
1103
case ICPPASTUnaryExpression.op_throw:
1104
opString = Keywords.THROW;
1106
case ICPPASTUnaryExpression.op_typeid:
1107
opString = Keywords.TYPEID;
1111
if (!opString.equals(EMPTY_STRING))
1115
case IASTUnaryExpression.op_alignOf:
1116
opString = Keywords.ALIGNOF;
1118
case IASTUnaryExpression.op_amper:
1119
opString = String.valueOf(Keywords.cpAMPER);
1121
case IASTUnaryExpression.op_minus:
1122
opString = String.valueOf(Keywords.cpMINUS);
1124
case IASTUnaryExpression.op_not:
1125
opString = String.valueOf(Keywords.cpNOT);
1127
case IASTUnaryExpression.op_plus:
1128
opString = String.valueOf(Keywords.cpPLUS);
1130
case IASTUnaryExpression.op_postFixDecr:
1131
opString = String.valueOf(Keywords.cpDECR);
1133
case IASTUnaryExpression.op_postFixIncr:
1134
opString = String.valueOf(Keywords.cpINCR);
1136
case IASTUnaryExpression.op_prefixDecr:
1137
opString = String.valueOf(Keywords.cpDECR);
1139
case IASTUnaryExpression.op_prefixIncr:
1140
opString = String.valueOf(Keywords.cpINCR);
1142
case IASTUnaryExpression.op_sizeof:
1143
opString = Keywords.SIZEOF;
1145
case IASTUnaryExpression.op_sizeofParameterPack:
1146
opString = Keywords.SIZEOF + new String(Keywords.cpELLIPSIS);
1148
case IASTUnaryExpression.op_star:
1149
opString = String.valueOf(Keywords.cpSTAR);
1151
case IASTUnaryExpression.op_tilde:
1152
opString = String.valueOf(Keywords.cpCOMPL);
1160
* Returns the String representation of the IASTBinaryExpression's operator.
1163
* @return the String representation of the IASTBinaryExpression's operator
1165
public static String getBinaryOperatorString(IASTBinaryExpression be) {
1166
int op = be.getOperator();
1167
String opString = EMPTY_STRING;
1169
case IASTBinaryExpression.op_multiply:
1170
opString = String.valueOf(Keywords.cpSTAR);
1172
case IASTBinaryExpression.op_divide:
1173
opString = String.valueOf(Keywords.cpDIV);
1175
case IASTBinaryExpression.op_modulo:
1176
opString = String.valueOf(Keywords.cpMOD);
1178
case IASTBinaryExpression.op_plus:
1179
opString = String.valueOf(Keywords.cpPLUS);
1181
case IASTBinaryExpression.op_minus:
1182
opString = String.valueOf(Keywords.cpMINUS);
1184
case IASTBinaryExpression.op_shiftLeft:
1185
opString = String.valueOf(Keywords.cpSHIFTL);
1187
case IASTBinaryExpression.op_shiftRight:
1188
opString = String.valueOf(Keywords.cpSHIFTR);
1190
case IASTBinaryExpression.op_lessThan:
1191
opString = String.valueOf(Keywords.cpLT);
1193
case IASTBinaryExpression.op_greaterThan:
1194
opString = String.valueOf(Keywords.cpGT);
1196
case IASTBinaryExpression.op_lessEqual:
1197
opString = String.valueOf(Keywords.cpLTEQUAL);
1199
case IASTBinaryExpression.op_greaterEqual:
1200
opString = String.valueOf(Keywords.cpGTEQUAL);
1202
case IASTBinaryExpression.op_binaryAnd:
1203
opString = String.valueOf(Keywords.cpAMPER);
1205
case IASTBinaryExpression.op_binaryXor:
1206
opString = String.valueOf(Keywords.cpXOR);
1208
case IASTBinaryExpression.op_binaryOr:
1209
opString = String.valueOf(Keywords.cpBITOR);
1211
case IASTBinaryExpression.op_logicalAnd:
1212
opString = String.valueOf(Keywords.cpAND);
1214
case IASTBinaryExpression.op_logicalOr:
1215
opString = String.valueOf(Keywords.cpOR);
1217
case IASTBinaryExpression.op_assign:
1218
opString = String.valueOf(Keywords.cpASSIGN);
1220
case IASTBinaryExpression.op_multiplyAssign:
1221
opString = String.valueOf(Keywords.cpSTARASSIGN);
1223
case IASTBinaryExpression.op_divideAssign:
1224
opString = String.valueOf(Keywords.cpDIVASSIGN);
1226
case IASTBinaryExpression.op_moduloAssign:
1227
opString = String.valueOf(Keywords.cpMODASSIGN);
1229
case IASTBinaryExpression.op_plusAssign:
1230
opString = String.valueOf(Keywords.cpPLUSASSIGN);
1232
case IASTBinaryExpression.op_minusAssign:
1233
opString = String.valueOf(Keywords.cpMINUSASSIGN);
1235
case IASTBinaryExpression.op_shiftLeftAssign:
1236
opString = String.valueOf(Keywords.cpSHIFTLASSIGN);
1238
case IASTBinaryExpression.op_shiftRightAssign:
1239
opString = String.valueOf(Keywords.cpSHIFTRASSIGN);
1241
case IASTBinaryExpression.op_binaryAndAssign:
1242
opString = String.valueOf(Keywords.cpAMPERASSIGN);
1244
case IASTBinaryExpression.op_binaryXorAssign:
1245
opString = String.valueOf(Keywords.cpXORASSIGN);
1247
case IASTBinaryExpression.op_binaryOrAssign:
1248
opString = String.valueOf(Keywords.cpBITORASSIGN);
1250
case IASTBinaryExpression.op_equals:
1251
opString = String.valueOf(Keywords.cpEQUAL);
1253
case IASTBinaryExpression.op_notequals:
1254
opString = String.valueOf(Keywords.cpNOTEQUAL);
1256
case IASTBinaryExpression.op_max:
1257
opString = String.valueOf(Keywords.cpMAX);
1259
case IASTBinaryExpression.op_min:
1260
opString = String.valueOf(Keywords.cpMIN);
1262
case IASTBinaryExpression.op_pmarrow:
1263
opString = String.valueOf(Keywords.cpARROW);
1265
case IASTBinaryExpression.op_pmdot:
1266
opString = String.valueOf(Keywords.cpDOT);
1274
* Returns the String representation of the IASTTypeIdExpression's operator.
1277
* @return the String representation of the IASTTypeIdExpression's operator
1279
private static String getTypeIdExpressionOperator(IASTTypeIdExpression expression) {
1280
String result = EMPTY_STRING;
1282
if (expression instanceof IGNUASTTypeIdExpression) {
1283
switch (expression.getOperator()) {
1284
case IGNUASTTypeIdExpression.op_alignof:
1285
result = Keywords.ALIGNOF;
1287
case IGNUASTTypeIdExpression.op_typeof:
1288
result = Keywords.TYPEOF;
1293
if (expression instanceof ICPPASTTypeIdExpression) {
1294
switch (expression.getOperator()) {
1295
case ICPPASTTypeIdExpression.op_typeid:
1296
result = Keywords.TYPEID;
1301
if (expression.getOperator() == IASTTypeIdExpression.op_sizeof)
1302
result = Keywords.SIZEOF;
1308
* Returns the String representation of the pack expansion expression.
1310
private static String getPackExpansionExpression(ICPPASTPackExpansionExpression expression) {
1311
return new StringBuilder().append(getExpressionString(expression.getPattern())).append(
1312
Keywords.cpELLIPSIS).toString();
1315
public static String getProblemMessage(int problemID, String detail) {
1316
return ASTProblem.getMessage(problemID, detail);