/** * Copyright 2015 JogAmp Community. All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are * permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, this list * of conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * The views and conclusions contained in the software and documentation are those of the * authors and should not be interpreted as representing official policies, either expressed * or implied, of JogAmp Community. */ package com.jogamp.gluegen; import java.math.BigInteger; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.jogamp.gluegen.ASTLocusTag.ASTLocusTagProvider; import com.jogamp.gluegen.cgram.types.AliasedSymbol.AliasedSymbolImpl; import com.jogamp.gluegen.cgram.types.TypeComparator.AliasedSemanticSymbol; import com.jogamp.gluegen.cgram.types.TypeComparator.SemanticEqualityOp; /** * Represents a [native] constant expression, * comprises the [native] expression, see {@link #getNativeExpr()} * and the optional {@link CNumber} representation, see {@link #getNumber()}. *
* The representation of the equivalent java expression including * the result type is covered by {@link JavaExpr}, * which can be computed via {@link #computeJavaExpr(Map)}. *
** This class and its sub-classes define and convert all native expressions * to Java space. *
*/ public class ConstantDefinition extends AliasedSymbolImpl implements AliasedSemanticSymbol, ASTLocusTagProvider { public static final long UNSIGNED_INT_MAX_VALUE = 0xffffffffL; public static final BigInteger UNSIGNED_LONG_MAX_VALUE = new BigInteger("ffffffffffffffff", 16); /** * A Number, either integer, optionally [long, unsigned], * or floating point, optionally [double]. */ public static class CNumber { /** * {@code true} if number is integer and value stored in {@link #i}, * otherwise {@code false} for floating point and value stored in {@link #f}. */ public final boolean isInteger; /** {@code true} if number is a {@code long} {@link #isInteger}. */ public final boolean isLong; /** {@code true} if number is an {@code unsigned} {@link #isInteger}. */ public final boolean isUnsigned; /** The value if {@link #isInteger} */ public final long i; /** {@code true} if number is a {@code double precision} {@code floating point}, i.e. !{@link #isInteger}. */ public final boolean isDouble; /** The value if !{@link #isInteger} */ public final double f; /** ctor for integer number */ public CNumber(final boolean isLong, final boolean isUnsigned, final long value) { this.isInteger = true; this.isLong = isLong; this.isUnsigned = isUnsigned; this.i = value; this.isDouble = false; this.f = 0.0; } /** ctor for floating point number */ public CNumber(final boolean isDouble, final double value) { this.isInteger = false; this.isLong = false; this.isUnsigned = false; this.i = 0; this.isDouble = isDouble; this.f = value; } @Override public int hashCode() { return isInteger ? Long.valueOf(i).hashCode() : Double.valueOf(f).hashCode(); } @Override public boolean equals(final Object arg) { if (arg == this) { return true; } else if ( !(arg instanceof CNumber) ) { return false; } final CNumber t = (CNumber) arg; return isInteger == t.isInteger && ( isInteger ? i == t.i : f == t.f ); } public final String toJavaString() { if( isInteger ) { if( i >= 0 || isUnsigned ) { if( isLong ) { return "0x"+Long.toHexString(i)+"L"; } else { return "0x"+Integer.toHexString((int)i); } } else { if( isLong ) { return String.valueOf(i)+"L"; } else { return String.valueOf((int)i); } } } else { return String.valueOf(f) + ( !isDouble ? "f" : ""); } } @Override public final String toString() { final StringBuilder sb = new StringBuilder(); sb.append("["); if( isInteger ) { if( isUnsigned ) { sb.append("unsigned "); } if( isLong) { sb.append("long: "); } else { sb.append("int: "); } sb.append(i); } else { if( isDouble ) { sb.append("double: "); } else { sb.append("float: "); } sb.append(f); } sb.append("]"); return sb.toString(); } } /** * A valid java expression, including its result type, * usually generated from a native [C] expression, * see {@link JavaExpr#create(ConstantDefinition)}. */ public static class JavaExpr { public final String javaExpression; public final CNumber resultType; public final Number resultJavaType; public final String resultJavaTypeName; public JavaExpr(final String javaExpression, final CNumber resultType) { this.javaExpression = javaExpression; this.resultType = resultType; if( resultType.isDouble ) { resultJavaTypeName = "double"; resultJavaType = Double.valueOf(resultType.f); } else if( !resultType.isInteger ) { resultJavaTypeName = "float"; resultJavaType = Double.valueOf(resultType.f).floatValue(); } else if( resultType.isLong ) { resultJavaTypeName = "long"; resultJavaType = Long.valueOf(resultType.i); } else /* if( resultType.isInteger ) */ { resultJavaTypeName = "int"; resultJavaType = Long.valueOf(resultType.i).intValue(); } } /** * Computes a valid {@link JavaExpr java expression} based on the given {@link ConstantDefinition}, * which may either be a single {@link CNumber}, see {@link ConstantDefinition#getNumber()}, * or represents a native expression, see {@link ConstantDefinition#getExpr()}. */ public static JavaExpr compute(final ConstantDefinition constDef, final Map* Method strips off sign prefix {@code +} * and integer modifier suffixes {@code [uUlL]} * before utilizing {@link Long#decode(String)}. *
* @param v */ public static CNumber decodeIntegerNumber(final String v) { if( null == v || !isIntegerNumber(v) ) { return null; } String s0 = v.trim(); if( 0 == s0.length() ) { return null; } if (s0.startsWith("+")) { s0 = s0.substring(1, s0.length()).trim(); if( 0 == s0.length() ) { return null; } } final boolean neg; if (s0.startsWith("-")) { s0 = s0.substring(1, s0.length()).trim(); if( 0 == s0.length() ) { return null; } neg = true; } else { neg = false; } // Test last two chars for [lL] and [uU] modifiers! boolean isUnsigned = false; boolean isLong = false; final int j = s0.length() - 2; for(int i = s0.length() - 1; i >= 0 && i >= j; i--) { final char lastChar = s0.charAt(s0.length()-1); if( lastChar == 'u' || lastChar == 'U' ) { s0 = s0.substring(0, s0.length()-1); isUnsigned = true; } else if( lastChar == 'l' || lastChar == 'L' ) { s0 = s0.substring(0, s0.length()-1); isLong = true; } else { // early out, no modifier match! break; } } if( 0 == s0.length() ) { return null; } final long res; if( isLong && isUnsigned ) { res = decodeULong(s0, neg); } else { if( neg ) { s0 = "-" + s0; } res = Long.decode(s0).longValue(); } final boolean isLong2 = isLong || ( !isUnsigned && ( Integer.MIN_VALUE > res || res > Integer.MAX_VALUE ) ) || ( isUnsigned && res > UNSIGNED_INT_MAX_VALUE ); return new CNumber(isLong2, isUnsigned, res); } private static long decodeULong(final String v, final boolean neg) throws NumberFormatException { final int radix; final int idx; if (v.startsWith("0x") || v.startsWith("0X")) { idx = 2; radix = 16; } else if (v.startsWith("#")) { idx = 1; radix = 16; } else if (v.startsWith("0") && v.length() > 1) { idx = 1; radix = 8; } else { idx = 0; radix = 10; } final String s0 = ( neg ? "-" : "" ) + v.substring(idx); final BigInteger res = new BigInteger(s0, radix); if( res.compareTo(UNSIGNED_LONG_MAX_VALUE) > 0 ) { throw new NumberFormatException("Value \""+v+"\" is > UNSIGNED_LONG_MAX"); } return res.longValue(); } /** * If the given string {@link #isDecimalNumber(String)}, * return the decoded floating-point value, represented as a {@code ANumber} object, * otherwise returns {@code null}. ** Method utilizes {@link Double#valueOf(String)}. *
* @param v * @param isDouble return value for {@code double} flag */ public static CNumber decodeDecimalNumber(final String v) { if( null == v || !isDecimalNumber(v) ) { return null; } final String s0 = v.trim(); if( 0 == s0.length() ) { return null; } boolean _isDouble = false; final char lastChar = s0.charAt(s0.length()-1); if( lastChar == 'd' || lastChar == 'D' ) { _isDouble = true; } final double res = Double.valueOf(s0).doubleValue(); final double ares = Math.abs(res); return new CNumber(_isDouble || Float.MIN_VALUE > ares || ares > Float.MAX_VALUE, res); } /** * Matches {@link #isHexNumber(String)} or {@link #isDecimalOrIntNumber(String)}. */ public static boolean isNumber(final String s) { if( isHexNumber(s) ) { return true; } else { return isDecimalOrIntNumber(s); } } /** * Matches {@link #isHexNumber(String)} or {@link #patternIntegerNumber}. */ public static boolean isIntegerNumber(final String s) { if( isHexNumber(s) ) { return true; } else { return patternIntegerNumber.matcher(s).matches(); } } /** * Matches {@link #patternHexNumber}. */ public static boolean isHexNumber(final String s) { return patternHexNumber.matcher(s).matches(); } /** * Matches pattern forfloating point
number,
* compatible and described in {@link Double#valueOf(String)}.
*/
public static boolean isDecimalNumber(final String s) {
return patternDecimalNumber.matcher(s).matches();
}
/**
* Complete pattern for floating point
and integer
number,
* covering {@link #patternDecimalNumber} and {@link #patternIntegerNumber}.
*/
public static boolean isDecimalOrIntNumber(final String s) {
return patternDecimalOrIntNumber.matcher(s).matches();
}
/**
* Matches pattern for valid CPP operands, see {@link #patternCPPOperand}.
*/
public static boolean isCPPOperand(final String s) {
return patternCPPOperand.matcher(s).matches();
}
/**
* Complete pattern for hexadecimal
number,
* including an optional sign {@code [+-]} and optional suffixes {@code [uUlL]}.
*/
public static Pattern patternHexNumber;
/**
* Complete pattern for floating point
number,
* compatible and described in {@link Double#valueOf(String)}.
*/
public final static Pattern patternDecimalNumber;
/**
* Complete pattern for floating point
and integer
number,
* covering {@link #patternDecimalNumber} and {@link #patternIntegerNumber}.
*/
public final static Pattern patternDecimalOrIntNumber;
/**
* Complete pattern for integer
number,
* including an optional sign {@code [+-]} and optional suffixes {@code [uUlL]}.
*/
public final static Pattern patternIntegerNumber;
/**
* One of: {@code +} {@code -} {@code *} {@code /} {@code |} {@code &} {@code (} {@code )} {@code <<} {@code >>} {@code ~}
* * Expression excludes {@link #patternDecimalOrIntNumber}. *
*/ public static Pattern patternCPPOperand; static { final String WhiteSpace = "[\\x00-\\x20]*"; final String Digits = "(\\p{Digit}+)"; final String HexDigits = "(\\p{XDigit}+)"; final String IntTypeSuffix = "(" + "[uU]|" + "([uU][lL])|" + "[lL]|" + "([lL][uU])" + ")"; final String hexRegex = WhiteSpace + // Optional leading "whitespace" "[+-]?" + // Optional sign character // HexDigits IntTypeSuffix_opt "0[xX]" + HexDigits + IntTypeSuffix + "?" + WhiteSpace // Optional trailing "whitespace" ; patternHexNumber = Pattern.compile(hexRegex); final String intRegex = WhiteSpace + // Optional leading "whitespace" "[+-]?" + // Optional sign character // Digits IntTypeSuffix_opt Digits + IntTypeSuffix + "?" + WhiteSpace // Optional trailing "whitespace" ; patternIntegerNumber = Pattern.compile(intRegex); // an exponent is 'e' or 'E' followed by an optionally // signed decimal integer. final String Exp = "[eE][+-]?"+Digits; final String fpRegex = WhiteSpace + // Optional leading "whitespace" "[+-]?" + // Optional sign character "("+ "NaN|" + // "NaN" string "Infinity|" + // "Infinity" string // A decimal floating-point string representing a finite positive // number without a leading sign has at most five basic pieces: // Digits . Digits ExponentPart FloatTypeSuffix // // Since this method allows integer-only strings as input // in addition to strings of floating-point literals, the // two sub-patterns below are simplifications of the grammar // productions from the Java Language Specification, 2nd // edition, section 3.10.2. "("+ "("+ // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt "("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+ // . Digits ExponentPart_opt FloatTypeSuffix_opt "(\\.("+Digits+")("+Exp+")?)|"+ // Hexadecimal w/ binary exponent "(" + "(" + // Hexadecimal strings // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt "(0[xX]" + HexDigits + "(\\.)?)|" + // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" + ")" + // binary exponent "[pP][+-]?" + Digits + ")" + ")" + "[fFdD]?"+ ")"+ ")" + WhiteSpace // Optional trailing "whitespace" ; patternDecimalNumber = Pattern.compile(fpRegex); final String fpOrIntRegex = WhiteSpace + // Optional leading "whitespace" "[+-]?" + // Optional sign character "("+ "NaN|" + // "NaN" string "Infinity|" + // "Infinity" string // Matching integers w/ IntTypeSuffix, // which are otherwise not matched by the below floating point matcher! // Digits IntTypeSuffix "(" + Digits + IntTypeSuffix +")|" + // A decimal floating-point string representing a finite positive // number without a leading sign has at most five basic pieces: // Digits . Digits ExponentPart FloatTypeSuffix // // Since this method allows integer-only strings as input // in addition to strings of floating-point literals, the // two sub-patterns below are simplifications of the grammar // productions from the Java Language Specification, 2nd // edition, section 3.10.2. "("+ "("+ // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt "(" + Digits + "(\\.)?(" + Digits + "?)(" + Exp + ")?)|" + // . Digits ExponentPart_opt FloatTypeSuffix_opt "(\\.(" + Digits + ")(" + Exp + ")?)|" + // Hexadecimal w/ binary exponent "(" + "(" + // Hexadecimal strings // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt "(0[xX]" + HexDigits + "(\\.)?)|" + // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" + ")" + // binary exponent "[pP][+-]?" + Digits + ")" + ")" + "[fFdD]?"+ ")"+ ")" + WhiteSpace // Optional trailing "whitespace" ; patternDecimalOrIntNumber = Pattern.compile(fpOrIntRegex); final String fpOrIntRegex2 = WhiteSpace + // Optional leading "whitespace" // "[+-]?" + // Optional sign character "("+ "NaN|" + // "NaN" string "Infinity|" + // "Infinity" string // Matching integers w/ IntTypeSuffix, // which are otherwise not matched by the below floating point matcher! // Digits IntTypeSuffix "(" + Digits + IntTypeSuffix +")|" + // A decimal floating-point string representing a finite positive // number without a leading sign has at most five basic pieces: // Digits . Digits ExponentPart FloatTypeSuffix // // Since this method allows integer-only strings as input // in addition to strings of floating-point literals, the // two sub-patterns below are simplifications of the grammar // productions from the Java Language Specification, 2nd // edition, section 3.10.2. "("+ "("+ // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt "(" + Digits + "(\\.)?(" + Digits + "?)(" + Exp + ")?)|" + // . Digits ExponentPart_opt FloatTypeSuffix_opt "(\\.(" + Digits + ")(" + Exp + ")?)|" + // Hexadecimal w/ binary exponent "(" + "(" + // Hexadecimal strings // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt "(0[xX]" + HexDigits + "(\\.)?)|" + // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" + ")" + // binary exponent "[pP][+-]?" + Digits + ")" + ")" + "[fFdD]?"+ ")"+ ")" + WhiteSpace // Optional trailing "whitespace" ; patternCPPOperand = Pattern.compile("(?!"+fpOrIntRegex2+")[\\+\\-\\*\\/\\|\\&\\(\\)]|(\\<\\<)|(\\>\\>)|(\\~)"); } }