+/*-----------------------------------------------------------------*/
+/* checkConstantRange: check a constant against the type */
+/*-----------------------------------------------------------------*/
+
+/* pedantic=0: allmost anything is allowed as long as the absolute
+ value is within the bit range of the type, and -1 is treated as
+ 0xf..f for unsigned types (e.g. in assign)
+ pedantic=1: -1==-1, not allowed for unsigned types (e.g. in compare)
+ pedantic>1: "char c=200" is not allowed (evaluates to -56)
+*/
+
+void checkConstantRange(sym_link *ltype, value *val, char *msg,
+ int pedantic) {
+ double max;
+ int warnings=0;
+ int negative=0;
+ long v;
+
+ max = pow ((double)2.0, (double)bitsForType(ltype));
+
+ if (SPEC_LONG(val->type)) {
+ if (SPEC_USIGN(val->type)) {
+ v=SPEC_CVAL(val->type).v_ulong;
+ } else {
+ v=SPEC_CVAL(val->type).v_long;
+ }
+ } else {
+ if (SPEC_USIGN(val->type)) {
+ v=SPEC_CVAL(val->type).v_uint;
+ } else {
+ v=SPEC_CVAL(val->type).v_int;
+ }
+ }
+
+
+#if 0
+ // this could be a good idea
+ if (options.pedantic)
+ pedantic=2;
+#endif
+
+ if (SPEC_NOUN(ltype)==FLOAT) {
+ // anything will do
+ return;
+ }
+
+ if (!SPEC_USIGN(val->type) && v<0) {
+ negative=1;
+ if (SPEC_USIGN(ltype) && (pedantic>1)) {
+ warnings++;
+ }
+ v=-v;
+ }
+
+ // if very pedantic: "char c=200" is not allowed
+ if (pedantic>1 && !SPEC_USIGN(ltype)) {
+ max = max/2 + negative;
+ }
+
+ if (v >= max) {
+ warnings++;
+ }
+
+#if 0 // temporary disabled, leaving the warning as a reminder
+ if (warnings) {
+ sprintf (message, "for %s %s in %s",
+ SPEC_USIGN(ltype) ? "unsigned" : "signed",
+ nounName(ltype), msg);
+ werror (W_CONST_RANGE, message);
+
+ if (pedantic>1)
+ fatalError++;
+ }
+#endif
+}