summaryrefslogtreecommitdiffstats
path: root/src/java/com/jogamp/gluegen/ConstantDefinition.java
blob: 675c6d7c8905aef8f51fb8703d9563a40d3f2c7d (plain)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
/**
 * 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()}.
 * <p>
 * The representation of the equivalent java expression including
 * the result type is covered by {@link JavaExpr},
 * which can be computed via {@link #computeJavaExpr(Map)}.
 * </p>
 * <p>
 * This class and its sub-classes define and convert all native expressions
 * to Java space.
 * </p>
 */
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" : "");
            }
        }
        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<String, ConstantDefinition.JavaExpr> constMap) {
              final boolean debug = GlueGen.debug();
              if( debug ) {
                  System.err.println("ConstJavaExpr.create: "+constDef);
              }
              if( constDef.hasNumber() ) {
                  // Already parsed as CNumber completely!
                  if( debug ) {
                      System.err.printf("V %s (isCNumber)%n", constDef);
                  }
                  return new JavaExpr(constDef.getNumber().toJavaString(), constDef.getNumber());
              }
              final StringBuilder javaExpr = new StringBuilder();
              final String nativeExpr = constDef.getNativeExpr();

              // "calculates" the result type of a simple expression
              // example: (2+3)-(2.0f-3.0) -> Double
              // example: (1 << 2) -> Integer
              CNumber resultType = null;
              final Matcher matcher = patternCPPOperand.matcher(nativeExpr);
              int preStartIdx = 0;
              int opEndIdx = 0;
              while ( matcher.find() ) {
                  final int opStartIdx = matcher.start();
                  if( opStartIdx > preStartIdx ) {
                      final String sValue = nativeExpr.substring(preStartIdx, opStartIdx).trim();
                      if( sValue.length() > 0 ) {
                          if( debug ) {
                              System.err.printf("V %03d-%03d: %s%n", preStartIdx, opStartIdx, sValue);
                          }
                          resultType = processValue(constDef, sValue, constMap, resultType, javaExpr);
                          javaExpr.append(" ");
                      }
                  }
                  opEndIdx = matcher.end();
                  final String op  =  nativeExpr.substring(opStartIdx, opEndIdx);
                  if( debug ) {
                      System.err.printf("O %03d-%03d: %s%n", opStartIdx, opEndIdx, op);
                  }
                  javaExpr.append(op).append(" ");
                  preStartIdx = opEndIdx;
              }
              if( opEndIdx < nativeExpr.length() ) {
                  // tail ..
                  final String sValue = nativeExpr.substring(opEndIdx).trim();
                  if( sValue.length() > 0 ) {
                      if( debug ) {
                          System.err.printf("V %03d %03d-%03d: %s (tail)%n", preStartIdx, opEndIdx, nativeExpr.length(), sValue);
                      }
                      resultType = processValue(constDef, sValue, constMap, resultType, javaExpr);
                  }
              }
              final String javaExprS = javaExpr.toString().trim();
              if( null == resultType ) {
                  throw new GlueGenException("Cannot emit const \""+constDef.getName()+"\": value \""+nativeExpr+
                      "\", parsed \""+javaExprS+"\" does not contain a constant number", constDef.getASTLocusTag());
              }
              return new JavaExpr(javaExprS, resultType);
          }
          private static CNumber processValue(final ConstantDefinition constDef,
                                              final String sValue,
                                              final Map<String, ConstantDefinition.JavaExpr> constMap,
                                              CNumber resultType,
                                              final StringBuilder javaExpr) {
              final CNumber nValue = getANumber(constDef, sValue);
              if( null != nValue ) {
                  resultType = evalType(resultType , nValue);
                  javaExpr.append(nValue.toJavaString());
              } else {
                  // Lookup CNumber type in const-map, to evaluate this result type
                  final JavaExpr cje = constMap.get(sValue);
                  if( null != cje ) {
                      resultType = evalType(resultType , cje.resultType);
                  }
                  javaExpr.append(sValue);
              }
              return resultType;
          }
          private static CNumber getANumber(final ConstantDefinition constDef, final String value) {
              try {
                  final CNumber number = decodeANumber(value);
                  if( null != number ) {
                      return number;
                  }
              } catch( final Throwable _t ) {
                  final String msg = "Cannot emit const \""+constDef.getName()+"\": value \""+value+
                          "\" cannot be assigned to a int, long, float, or double";
                  throw new GlueGenException(msg, constDef.getASTLocusTag(), _t);
              }
              return null;
          }
          private static CNumber evalType(final CNumber resultType, final CNumber type) {
              //fast path
              if( type.isDouble ) {
                  return type;
              }
              if( null != resultType ) {
                  if( resultType.isInteger ) {
                      if( resultType.isLong ) {
                          /* resultType is Long */
                          if( !type.isInteger ) {
                              /* resultType: Long -> [ Float || Double ] */
                              return type;
                          }
                      } else if( type.isLong || !type.isInteger ) {
                          /* resultType: Integer -> [ Long || Float || Double ] */
                          return type;
                      }
                  } else if( !resultType.isInteger && !resultType.isDouble ) {
                      if( type.isDouble ) {
                          /* resultType: Float -> Double */
                          return type;
                      }
                  }
              } else {
                  return type;
              }
              return resultType;
          }
      }

    private final boolean relaxedEqSem;
    private final String nativeExpr;
    private final CNumber number;
    private final boolean isEnum;
    private final String enumName;
    private final ASTLocusTag astLocus;

    /**
     * Constructor for plain const-values, non-enumerates.
     * @param name unique name of this constant expression
     * @param nativeExpr original [native] expression
     * @param number optional {@link CNumber} representing this constant.
     *               If {@code null}, implementation attempts to derive a {@link CNumber}
     *               of the given {@code nativeExpr}.
     * @param astLocus AST location of the represented constant.
     */
    public ConstantDefinition(final String name,
                              final String nativeExpr,
                              final CNumber number,
                              final ASTLocusTag astLocus) {
        this(name, nativeExpr, number, false, null, astLocus);
    }
    /**
     * Constructor for enumerates
     * @param name unique name of this constant expression
     * @param nativeExpr original [native] expression
     * @param number optional {@link CNumber} representing this constant.
     *               If {@code null}, implementation attempts to derive a {@link CNumber}
     *               of the given {@code nativeExpr}.
     * @param enumName optional name of the represented enumeration
     * @param astLocus AST location of the represented constant.
     */
    public ConstantDefinition(final String name,
                              final String nativeExpr,
                              final CNumber number,
                              final String enumName, final ASTLocusTag astLocus) {
        this(name, nativeExpr, number, true, enumName, astLocus);
    }
    /**
     * @param name unique name of this constant expression
     * @param nativeExpr original [native] expression
     * @param number optional {@link CNumber} representing this constant.
     *               If {@code null}, implementation attempts to derive a {@link CNumber}
     *               of the given {@code nativeExpr}.
     * @param isEnum {@code true} if this constant is an enumerate, otherwise {@code false}.
     * @param enumName optional name of the represented enumeration
     * @param astLocus AST location of the represented constant.
     */
    private ConstantDefinition(final String name,
                               final String nativeExpr,
                               final CNumber number,
                               final boolean isEnum, final String enumName, final ASTLocusTag astLocus) {
        super(name);
        this.nativeExpr = nativeExpr;
        this.relaxedEqSem = TypeConfig.relaxedEqualSemanticsTest();
        if( null != number ) {
            this.number = number;
        } else {
            // Attempt to parse define string as number
            final CNumber iNum = decodeIntegerNumber(nativeExpr);
            if( null != iNum ) {
                this.number = iNum;
            } else {
                final CNumber fNum = decodeDecimalNumber(nativeExpr);
                if( null != fNum ) {
                    this.number = fNum;
                } else {
                    this.number = null;
                }
            }
        }
        this.isEnum = isEnum;
        this.enumName = enumName;
        this.astLocus = astLocus;
    }

    @Override
    public ASTLocusTag getASTLocusTag() { return astLocus; }

    /**
     * Hash by its given {@link #getName() name}.
     */
    @Override
    public final int hashCode() {
        return getName().hashCode();
    }

    /**
     * Equality test by its given {@link #getName() name}.
     */
    @Override
    public final boolean equals(final Object arg) {
        if (arg == this) {
            return true;
        } else  if ( !(arg instanceof ConstantDefinition) ) {
            return false;
        } else {
            final ConstantDefinition t = (ConstantDefinition)arg;
            return equals(getName(), t.getName());
        }
    }

    @Override
    public final int hashCodeSemantics() {
        // 31 * x == (x << 5) - x
        int hash = 31 + ( null != getName() ? getName().hashCode() : 0 );
        hash = ((hash << 5) - hash) + ( isEnum ? 1 : 0 );
        hash = ((hash << 5) - hash) + ( null != enumName ? enumName.hashCode() : 0 );
        hash = ((hash << 5) - hash) + ( null != number ? number.hashCode() : 0 );
        return ((hash << 5) - hash) + ( !relaxedEqSem && null != nativeExpr ? nativeExpr.hashCode() : 0 );
    }

    @Override
    public final boolean equalSemantics(final SemanticEqualityOp arg) {
        if (arg == this) {
            return true;
        } else  if ( !(arg instanceof ConstantDefinition) ) {
            return false;
        } else {
            final ConstantDefinition t = (ConstantDefinition) arg;
            if( !equals(getName(), t.getName()) ||
                isEnum != t.isEnum ||
                !equals(enumName, t.enumName) ) {
                return false;
            }
            if( null != number ) {
                if( number.isInteger ) {
                    return number.i == t.number.i;
                } else {
                    return number.f == t.number.f;
                }
            } else {
                // define's string value may be semantical equal .. but formatted differently!
                return relaxedEqSem || equals(nativeExpr, t.nativeExpr);
            }
        }
    }

    /** Returns the original [native] expression. */
    public String getNativeExpr() { return nativeExpr; }
    /**
     * Returns the parsed {@link CNumber} of the {@link #getNativeExpr() native expression},
     * or {@code null} if the latter does not comprise a single number,
     * i.e. is a complex expression.
     */
    public CNumber getNumber()  { return number; }
    /**
     * Returns {@code true} if this instance represents has a {@link #getNumber() number},
     * otherwise {@code false}.
     */
    public boolean hasNumber()  { return null != number; }

    /** Returns {@code null} if this definition was not part of an
        enumeration, or if the enumeration is anonymous. */
    public String getEnumName() { return enumName; }

    public boolean isEnum() { return isEnum; }

    @Override
    public String toString() {
        return "ConstantDefinition [name \"" + getName()
                + "\", expression \"" + nativeExpr
                + "\", number "+number
                + "], enum[is " + isEnum + ", name \"" + enumName + "\"]]";
    }

    private static boolean equals(final String s1, final String s2) {
        if (s1 == null || s2 == null) {
            if (s1 == null && s2 == null) {
                return true;
            }
            return false;
        }

        return s1.equals(s2);
    }

    /**
     * Computes the {@link JavaExpr java expression} based on this instance,
     * see {@link JavaExpr#create(ConstantDefinition)}.
     */
    public final JavaExpr computeJavaExpr(final Map<String, ConstantDefinition.JavaExpr> constMap) {
        return JavaExpr.compute(this, constMap);
    }

    //
    // Static utility functions for type detection
    //

    public static boolean isConstantExpression(final String value) {
        if( null != value && value.length() > 0 ) {
            // Single numeric value
            if ( isNumber(value) ) {
                return true;
            }
            // Find constant expressions like (1 << 3)
            // if found just pass them through, they will most likely work in java too
            // expressions containing identifiers are currently ignored (casts too)
            final String[] values = value.split("[\\s\\(\\)]"); // [ whitespace '(' ')' ]
            int numberCount = 0;
            for (final String s : values) {
                if( s.length() > 0 ) {
                    if( isCPPOperand(s) ) {
                        // OK
                    } else if ( isNumber(s) ) {
                        // OK
                        numberCount++;
                    } else {
                        return false;
                    }
                }
            }
            final boolean res = numberCount > 0;
            return res;
        }
        return false;
    }

    public static boolean isIdentifier(final String value) {
        boolean identifier = false;

        final char[] chars = value.toCharArray();

        for (int i = 0; i < chars.length; i++) {
            final char c = chars[i];
            if (i == 0) {
                if (Character.isJavaIdentifierStart(c)) {
                    identifier = true;
                }
            } else {
                if (!Character.isJavaIdentifierPart(c)) {
                    identifier = false;
                    break;
                }
            }
        }
        return identifier;
    }

    /**
     * Returns either {@link #decodeIntegerNumber(String)},
     * {@link #decodeDecimalNumber(String)} or {@code null}.
     * @param v
     */
    public static CNumber decodeANumber(final String v) {
        final CNumber iNumber = ConstantDefinition.decodeIntegerNumber(v);
        if( null != iNumber ) {
            return iNumber;
        }
        return ConstantDefinition.decodeDecimalNumber(v);
    }

    /**
     * If the given string {@link #isIntegerNumber(String)},
     * return the decoded integer value, represented as a {@code ANumber},
     * otherwise returns {@code null}.
     * <p>
     * Method strips off sign prefix {@code +}
     * and integer modifier suffixes {@code [uUlL]}
     * before utilizing {@link Long#decode(String)}.
     * </p>
     * @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}.
     * <p>
     * Method utilizes {@link Double#valueOf(String)}.
     * </p>
     * @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 for <code>floating point</code> number,
     * compatible and described in {@link Double#valueOf(String)}.
     */
    public static boolean isDecimalNumber(final String s) {
        return patternDecimalNumber.matcher(s).matches();
    }

    /**
     * Complete pattern for <code>floating point</code> <i>and</i> <code>integer</code> number,
     * covering {@link #patternDecimalNumber} <i>and</i> {@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 <code>hexadecimal</code> number,
     * including an optional sign {@code [+-]} and optional suffixes {@code [uUlL]}.
     */
    public static Pattern patternHexNumber;

    /**
     * Complete pattern for <code>floating point</code> number,
     * compatible and described in {@link Double#valueOf(String)}.
     */
    public final static Pattern patternDecimalNumber;

    /**
     * Complete pattern for <code>floating point</code> <i>and</i> <code>integer</code> number,
     * covering {@link #patternDecimalNumber} <i>and</i> {@link #patternIntegerNumber}.
     */
    public final static Pattern patternDecimalOrIntNumber;

    /**
     * Complete pattern for <code>integer</code> 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 >>}
     * <p>
     * Expression excludes {@link #patternDecimalOrIntNumber}.
     * </p>
     */
    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+")[\\+\\-\\*\\/\\|\\&\\(\\)]|(\\<\\<)|(\\>\\>)");
    }
}