1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Improve interface of IASTLiteralExpression, related to bug 253690.

This commit is contained in:
Markus Schorn 2008-11-07 09:58:18 +00:00
parent fa2d511bbd
commit 0087933aa5
8 changed files with 127 additions and 57 deletions

View file

@ -1,19 +1,20 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2004, 2005 IBM Corporation and others. * Copyright (c) 2004, 2008 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
* *
* Contributors: * Contributors:
* IBM - Initial API and implementation * Doug Schaefer (IBM) - Initial API and implementation
* Markus Schorn (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.core.dom.ast; package org.eclipse.cdt.core.dom.ast;
/** /**
* This expression represents a literal in the program. * This expression represents a literal in the program.
* *
* @author Doug Schaefer * @noimplement This interface is not intended to be implemented by clients.
*/ */
public interface IASTLiteralExpression extends IASTExpression { public interface IASTLiteralExpression extends IASTExpression {
@ -28,39 +29,70 @@ public interface IASTLiteralExpression extends IASTExpression {
public static final int lk_float_constant = 1; public static final int lk_float_constant = 1;
/** /**
* A char literal e.g. 'abc' * A char literal e.g. 'a'
*/ */
public static final int lk_char_constant = 2; public static final int lk_char_constant = 2;
/** /**
* A string literal e.g. "abcdefg" * A string literal e.g. "a literal"
*/ */
public static final int lk_string_literal = 3; public static final int lk_string_literal = 3;
/** /**
* A constant defined for subclasses to extend from. * A constant defined for subclasses to extend from.
* @deprecated all possible values must be defined in {@link IASTLiteralExpression}.
*/ */
@Deprecated
public static final int lk_last = lk_string_literal; public static final int lk_last = lk_string_literal;
/**
* <code>lk_this</code> represents the 'this' keyword for c++ only.
* @since 5.1
*/
public static final int lk_this = 4;
/**
* <code>lk_true</code> represents the 'true' keyword.
* @since 5.1
*/
public static final int lk_true = 5;
/**
* <code>lk_false</code> represents the 'false' keyword.
* @since 5.1
*/
public static final int lk_false = 6;
/** /**
* Get the literal expression kind. * Get the literal expression kind.
*
* @return int
*/ */
public int getKind(); public int getKind();
/**
* Returns the value of the literal as char-array.
* @since 5.1
*/
public char[] getValue();
/**
* Returns the value of the literal as string.
* @since 5.1
*/
public String toString();
/** /**
* Set the literal expression kind. * Set the literal expression kind.
*
* @param value
* int
*/ */
public void setKind(int value); public void setKind(int value);
/** /**
* Set the value of the literal expression. * Provide the value for the expression.
* * @since 5.1
* @param value */
public void setValue(char[] value);
/**
* @deprecated, use {@link #setValue(char[])}, instead.
*/ */
public void setValue(String value); public void setValue(String value);

View file

@ -1,12 +1,12 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2004, 2005 IBM Corporation and others. * Copyright (c) 2004, 2008 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
* *
* Contributors: * Contributors:
* IBM - Initial API and implementation * John Camelon (IBM) - Initial API and implementation
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.core.dom.ast.cpp; package org.eclipse.cdt.core.dom.ast.cpp;
@ -15,24 +15,24 @@ import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
/** /**
* C++ adds additional literal types to primary expression. * C++ adds additional literal types to primary expression.
* *
* @author jcamelon * @noimplement This interface is not intended to be implemented by clients.
*/ */
public interface ICPPASTLiteralExpression extends IASTLiteralExpression { public interface ICPPASTLiteralExpression extends IASTLiteralExpression {
/** /**
* <code>lk_this</code> represents the 'this' keyword. * <code>lk_this</code> represents the 'this' keyword.
*/ */
public static final int lk_this = IASTLiteralExpression.lk_last + 1; public static final int lk_this = IASTLiteralExpression.lk_this;
/** /**
* <code>lk_true</code> represents the 'true' keyword. * <code>lk_true</code> represents the 'true' keyword.
*/ */
public static final int lk_true = IASTLiteralExpression.lk_last + 2; public static final int lk_true = IASTLiteralExpression.lk_true;
/** /**
* <code>lk_false</code> represents the 'false' keyword. * <code>lk_false</code> represents the 'false' keyword.
*/ */
public static final int lk_false = IASTLiteralExpression.lk_last + 3; public static final int lk_false = IASTLiteralExpression.lk_false;
/** /**
* <code>lk_last</code> is maintained for future subinterfaces. * <code>lk_last</code> is maintained for future subinterfaces.

View file

@ -24,7 +24,6 @@ import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IEnumerator; import org.eclipse.cdt.core.dom.ast.IEnumerator;
import org.eclipse.cdt.core.dom.ast.IValue; import org.eclipse.cdt.core.dom.ast.IValue;
import org.eclipse.cdt.core.dom.ast.IVariable; import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding; import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateNonTypeParameter; import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateNonTypeParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter; import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter;
@ -229,19 +228,19 @@ public class Value implements IValue {
if (e instanceof IASTLiteralExpression) { if (e instanceof IASTLiteralExpression) {
IASTLiteralExpression litEx= (IASTLiteralExpression) e; IASTLiteralExpression litEx= (IASTLiteralExpression) e;
switch (litEx.getKind()) { switch (litEx.getKind()) {
case ICPPASTLiteralExpression.lk_false: case IASTLiteralExpression.lk_false:
return "0"; return "0";
case ICPPASTLiteralExpression.lk_true: case IASTLiteralExpression.lk_true:
return "1"; return "1";
case IASTLiteralExpression.lk_integer_constant: case IASTLiteralExpression.lk_integer_constant:
try { try {
return ExpressionEvaluator.getNumber(e.toString().toCharArray()); return ExpressionEvaluator.getNumber(litEx.getValue());
} catch (EvalException e1) { } catch (EvalException e1) {
throw UNKNOWN_EX; throw UNKNOWN_EX;
} }
case IASTLiteralExpression.lk_char_constant: case IASTLiteralExpression.lk_char_constant:
try { try {
final char[] image= e.toString().toCharArray(); final char[] image= litEx.getValue();
if (image.length > 1) if (image.length > 1)
if (image[0] == 'L') if (image[0] == 'L')
return ExpressionEvaluator.getChar(image, 2); return ExpressionEvaluator.getChar(image, 2);

View file

@ -6,30 +6,30 @@
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
* *
* Contributors: * Contributors:
* IBM Rational Software - Initial API and implementation * John Camelon (IBM Rational Software) - Initial API and implementation
* Yuan Zhang / Beth Tibbitts (IBM Research) * Yuan Zhang / Beth Tibbitts (IBM Research)
* Markus Schorn (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.c; package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression; import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode; import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
/** /**
* @author jcamelon * Represents a literal
*/ */
public class CASTLiteralExpression extends ASTNode implements IASTLiteralExpression { public class CASTLiteralExpression extends ASTNode implements IASTLiteralExpression {
private int kind; private int kind;
private String value = ""; //$NON-NLS-1$ private char[] value = CharArrayUtils.EMPTY;
public CASTLiteralExpression() { public CASTLiteralExpression() {
} }
public CASTLiteralExpression(int kind, String value) { public CASTLiteralExpression(int kind, char[] value) {
this.kind = kind; this.kind = kind;
this.value = value; this.value = value;
} }
@ -42,13 +42,17 @@ public class CASTLiteralExpression extends ASTNode implements IASTLiteralExpress
kind = value; kind = value;
} }
public void setValue(String value) { public char[] getValue() {
return value;
}
public void setValue(char[] value) {
this.value= value; this.value= value;
} }
@Override @Override
public String toString() { public String toString() {
return value; return new String(value);
} }
@Override @Override
@ -74,4 +78,19 @@ public class CASTLiteralExpression extends ASTNode implements IASTLiteralExpress
return CVisitor.getExpressionType(this); return CVisitor.getExpressionType(this);
} }
/**
* @deprecated, use {@link #setValue(char[])}, instead.
*/
@Deprecated
public void setValue(String value) {
this.value = value.toCharArray();
}
/**
* @deprecated use {@link #CASTLiteralExpression(int, char[])}, instead.
*/
@Deprecated
public CASTLiteralExpression(int kind, String value) {
this(kind, value.toCharArray());
}
} }

View file

@ -6,30 +6,30 @@
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
* *
* Contributors: * Contributors:
* IBM - Initial API and implementation * John Camelon (IBM) - Initial API and implementation
* Markus Schorn (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp; package org.eclipse.cdt.internal.core.dom.parser.cpp;
import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLiteralExpression; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLiteralExpression;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode; import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
/** /**
* @author jcamelon * Represents a c++ literal.
*/ */
public class CPPASTLiteralExpression extends ASTNode implements ICPPASTLiteralExpression { public class CPPASTLiteralExpression extends ASTNode implements ICPPASTLiteralExpression {
private int kind; private int kind;
private String value = ""; //$NON-NLS-1$ private char[] value = CharArrayUtils.EMPTY;
public CPPASTLiteralExpression() { public CPPASTLiteralExpression() {
} }
public CPPASTLiteralExpression(int kind, String value) { public CPPASTLiteralExpression(int kind, char[] value) {
this.kind = kind; this.kind = kind;
this.value = value; this.value = value;
} }
@ -39,16 +39,20 @@ public class CPPASTLiteralExpression extends ASTNode implements ICPPASTLiteralEx
} }
public void setKind(int value) { public void setKind(int value) {
this.kind = value; kind = value;
} }
public void setValue(String value) { public char[] getValue() {
return value;
}
public void setValue(char[] value) {
this.value= value; this.value= value;
} }
@Override @Override
public String toString() { public String toString() {
return value; return new String(value);
} }
@Override @Override
@ -74,4 +78,20 @@ public class CPPASTLiteralExpression extends ASTNode implements ICPPASTLiteralEx
return CPPVisitor.getExpressionType(this); return CPPVisitor.getExpressionType(this);
} }
/**
* @deprecated, use {@link #setValue(char[])}, instead.
*/
@Deprecated
public void setValue(String value) {
this.value = value.toCharArray();
}
/**
* @deprecated use {@link #CPPASTLiteralExpression(int, char[])}, instead.
*/
@Deprecated
public CPPASTLiteralExpression(int kind, String value) {
this(kind, value.toCharArray());
}
} }

View file

@ -43,7 +43,6 @@ import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.rewrite.ASTRewrite; import org.eclipse.cdt.core.dom.rewrite.ASTRewrite;
@ -328,12 +327,12 @@ public class ExtractConstantRefactoring extends CRefactoring {
case IASTLiteralExpression.lk_string_literal: case IASTLiteralExpression.lk_string_literal:
declSpec.setType(ICPPASTSimpleDeclSpecifier.t_wchar_t); declSpec.setType(ICPPASTSimpleDeclSpecifier.t_wchar_t);
break; break;
case ICPPASTLiteralExpression.lk_false: case IASTLiteralExpression.lk_false:
//Like lk_true a boolean type //Like lk_true a boolean type
case ICPPASTLiteralExpression.lk_true: case IASTLiteralExpression.lk_true:
declSpec.setType(ICPPASTSimpleDeclSpecifier.t_bool); declSpec.setType(ICPPASTSimpleDeclSpecifier.t_bool);
break; break;
case ICPPASTLiteralExpression.lk_this: case IASTLiteralExpression.lk_this:
break; break;
} }
@ -348,7 +347,7 @@ public class ExtractConstantRefactoring extends CRefactoring {
IASTUnaryExpression unary = (IASTUnaryExpression) target.getParent(); IASTUnaryExpression unary = (IASTUnaryExpression) target.getParent();
init.setExpression(unary); init.setExpression(unary);
} else { } else {
CPPASTLiteralExpression expression = new CPPASTLiteralExpression(target.getKind(), target.toString()); CPPASTLiteralExpression expression = new CPPASTLiteralExpression(target.getKind(), target.getValue());
init.setExpression(expression); init.setExpression(expression);
} }
decl.setInitializer(init); decl.setInitializer(init);

View file

@ -29,7 +29,6 @@ import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTBinaryExpression; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier;
@ -61,13 +60,14 @@ import org.eclipse.cdt.internal.ui.refactoring.NodeContainer.NameInformation;
* *
*/ */
public class ExtractExpression extends ExtractedFunctionConstructionHelper { public class ExtractExpression extends ExtractedFunctionConstructionHelper {
final static char[] ZERO= {'0'};
@Override @Override
public void constructMethodBody(IASTCompoundStatement compound, public void constructMethodBody(IASTCompoundStatement compound,
List<IASTNode> list, ASTRewrite rewrite, TextEditGroup group) { List<IASTNode> list, ASTRewrite rewrite, TextEditGroup group) {
CPPASTReturnStatement statement = new CPPASTReturnStatement(); CPPASTReturnStatement statement = new CPPASTReturnStatement();
IASTExpression nullReturnExp = new CPPASTLiteralExpression(IASTLiteralExpression.lk_integer_constant, "0"); //$NON-NLS-1$ IASTExpression nullReturnExp = new CPPASTLiteralExpression(IASTLiteralExpression.lk_integer_constant, ZERO);
statement.setReturnValue(nullReturnExp); statement.setReturnValue(nullReturnExp);
ASTRewrite nestedRewrite = rewrite.insertBefore(compound, null, statement, group); ASTRewrite nestedRewrite = rewrite.insertBefore(compound, null, statement, group);
@ -112,9 +112,9 @@ public class ExtractExpression extends ExtractedFunctionConstructionHelper {
return createSimpleDeclSpecifier(IASTSimpleDeclSpecifier.t_int); return createSimpleDeclSpecifier(IASTSimpleDeclSpecifier.t_int);
case IASTLiteralExpression.lk_string_literal: case IASTLiteralExpression.lk_string_literal:
return createSimpleDeclSpecifier(ICPPASTSimpleDeclSpecifier.t_wchar_t); return createSimpleDeclSpecifier(ICPPASTSimpleDeclSpecifier.t_wchar_t);
case ICPPASTLiteralExpression.lk_false: case IASTLiteralExpression.lk_false:
//Like lk_true a boolean type //Like lk_true a boolean type
case ICPPASTLiteralExpression.lk_true: case IASTLiteralExpression.lk_true:
return createSimpleDeclSpecifier(ICPPASTSimpleDeclSpecifier.t_bool); return createSimpleDeclSpecifier(ICPPASTSimpleDeclSpecifier.t_bool);
default: default:
return null; return null;

View file

@ -16,6 +16,7 @@ import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator; import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.parser.Keywords;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTBinaryExpression; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTBinaryExpression;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTCompoundStatement; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTCompoundStatement;
@ -89,7 +90,7 @@ public class FunctionFactory {
CPPASTBinaryExpression binExpr = new CPPASTBinaryExpression(); CPPASTBinaryExpression binExpr = new CPPASTBinaryExpression();
CPPASTFieldReference fieldRef = new CPPASTFieldReference(); CPPASTFieldReference fieldRef = new CPPASTFieldReference();
CPPASTLiteralExpression litExpr = new CPPASTLiteralExpression(); CPPASTLiteralExpression litExpr = new CPPASTLiteralExpression();
litExpr.setValue("this"); //$NON-NLS-1$ litExpr.setValue(Keywords.cTHIS);
fieldRef.setFieldOwner(litExpr); fieldRef.setFieldOwner(litExpr);
fieldRef.setFieldName(fieldDeclaration.getDeclarators()[0].getName()); fieldRef.setFieldName(fieldDeclaration.getDeclarators()[0].getName());
fieldRef.setIsPointerDereference(true); fieldRef.setIsPointerDereference(true);