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
* 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 - Initial API and implementation
* Doug Schaefer (IBM) - Initial API and implementation
* Markus Schorn (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.core.dom.ast;
/**
* 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 {
@ -28,39 +29,70 @@ public interface IASTLiteralExpression extends IASTExpression {
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;
/**
* A string literal e.g. "abcdefg"
* A string literal e.g. "a literal"
*/
public static final int lk_string_literal = 3;
/**
* 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;
/**
* <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.
*
* @return int
*/
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.
*
* @param value
* int
*/
public void setKind(int value);
/**
* Set the value of the literal expression.
*
* @param value
* Provide the value for the expression.
* @since 5.1
*/
public void setValue(char[] value);
/**
* @deprecated, use {@link #setValue(char[])}, instead.
*/
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
* 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 - Initial API and implementation
* John Camelon (IBM) - Initial API and implementation
*******************************************************************************/
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.
*
* @author jcamelon
* @noimplement This interface is not intended to be implemented by clients.
*/
public interface ICPPASTLiteralExpression extends IASTLiteralExpression {
/**
* <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.
*/
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.
*/
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.

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

View file

@ -6,30 +6,30 @@
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
* Yuan Zhang / Beth Tibbitts (IBM Research)
* John Camelon (IBM Rational Software) - Initial API and implementation
* Yuan Zhang / Beth Tibbitts (IBM Research)
* Markus Schorn (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
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;
/**
* @author jcamelon
* Represents a literal
*/
public class CASTLiteralExpression extends ASTNode implements IASTLiteralExpression {
private int kind;
private String value = ""; //$NON-NLS-1$
private char[] value = CharArrayUtils.EMPTY;
public CASTLiteralExpression() {
}
public CASTLiteralExpression(int kind, String value) {
public CASTLiteralExpression(int kind, char[] value) {
this.kind = kind;
this.value = value;
}
@ -42,13 +42,17 @@ public class CASTLiteralExpression extends ASTNode implements IASTLiteralExpress
kind = value;
}
public void setValue(String value) {
this.value = value;
public char[] getValue() {
return value;
}
public void setValue(char[] value) {
this.value= value;
}
@Override
public String toString() {
return value;
return new String(value);
}
@Override
@ -74,4 +78,19 @@ public class CASTLiteralExpression extends ASTNode implements IASTLiteralExpress
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
*
* 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;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IType;
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.cpp.semantics.CPPVisitor;
/**
* @author jcamelon
* Represents a c++ literal.
*/
public class CPPASTLiteralExpression extends ASTNode implements ICPPASTLiteralExpression {
private int kind;
private String value = ""; //$NON-NLS-1$
private char[] value = CharArrayUtils.EMPTY;
public CPPASTLiteralExpression() {
}
public CPPASTLiteralExpression(int kind, String value) {
public CPPASTLiteralExpression(int kind, char[] value) {
this.kind = kind;
this.value = value;
}
@ -39,16 +39,20 @@ public class CPPASTLiteralExpression extends ASTNode implements ICPPASTLiteralEx
}
public void setKind(int value) {
this.kind = value;
kind = value;
}
public void setValue(String value) {
this.value = value;
public char[] getValue() {
return value;
}
public void setValue(char[] value) {
this.value= value;
}
@Override
public String toString() {
return value;
return new String(value);
}
@Override
@ -74,4 +78,20 @@ public class CPPASTLiteralExpression extends ASTNode implements ICPPASTLiteralEx
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

@ -7,7 +7,7 @@
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Institute for Software - initial API and implementation
* Institute for Software - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.extractconstant;
@ -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.cpp.CPPASTVisitor;
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.ICPPASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.rewrite.ASTRewrite;
@ -328,12 +327,12 @@ public class ExtractConstantRefactoring extends CRefactoring {
case IASTLiteralExpression.lk_string_literal:
declSpec.setType(ICPPASTSimpleDeclSpecifier.t_wchar_t);
break;
case ICPPASTLiteralExpression.lk_false:
case IASTLiteralExpression.lk_false:
//Like lk_true a boolean type
case ICPPASTLiteralExpression.lk_true:
case IASTLiteralExpression.lk_true:
declSpec.setType(ICPPASTSimpleDeclSpecifier.t_bool);
break;
case ICPPASTLiteralExpression.lk_this:
case IASTLiteralExpression.lk_this:
break;
}
@ -348,7 +347,7 @@ public class ExtractConstantRefactoring extends CRefactoring {
IASTUnaryExpression unary = (IASTUnaryExpression) target.getParent();
init.setExpression(unary);
} else {
CPPASTLiteralExpression expression = new CPPASTLiteralExpression(target.getKind(), target.toString());
CPPASTLiteralExpression expression = new CPPASTLiteralExpression(target.getKind(), target.getValue());
init.setExpression(expression);
}
decl.setInitializer(init);

View file

@ -7,7 +7,7 @@
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Institute for Software - initial API and implementation
* Institute for Software - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.extractfunction;
@ -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.IType;
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.ICPPASTQualifiedName;
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 {
final static char[] ZERO= {'0'};
@Override
public void constructMethodBody(IASTCompoundStatement compound,
List<IASTNode> list, ASTRewrite rewrite, TextEditGroup group) {
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);
ASTRewrite nestedRewrite = rewrite.insertBefore(compound, null, statement, group);
@ -112,9 +112,9 @@ public class ExtractExpression extends ExtractedFunctionConstructionHelper {
return createSimpleDeclSpecifier(IASTSimpleDeclSpecifier.t_int);
case IASTLiteralExpression.lk_string_literal:
return createSimpleDeclSpecifier(ICPPASTSimpleDeclSpecifier.t_wchar_t);
case ICPPASTLiteralExpression.lk_false:
case IASTLiteralExpression.lk_false:
//Like lk_true a boolean type
case ICPPASTLiteralExpression.lk_true:
case IASTLiteralExpression.lk_true:
return createSimpleDeclSpecifier(ICPPASTSimpleDeclSpecifier.t_bool);
default:
return null;

View file

@ -7,7 +7,7 @@
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Institute for Software - initial API and implementation
* Institute for Software - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.gettersandsetters;
@ -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.IASTSimpleDeclSpecifier;
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.CPPASTCompoundStatement;
@ -89,7 +90,7 @@ public class FunctionFactory {
CPPASTBinaryExpression binExpr = new CPPASTBinaryExpression();
CPPASTFieldReference fieldRef = new CPPASTFieldReference();
CPPASTLiteralExpression litExpr = new CPPASTLiteralExpression();
litExpr.setValue("this"); //$NON-NLS-1$
litExpr.setValue(Keywords.cTHIS);
fieldRef.setFieldOwner(litExpr);
fieldRef.setFieldName(fieldDeclaration.getDeclarators()[0].getName());
fieldRef.setIsPointerDereference(true);