da Silva, Joe wrote:
What if you define your variable as -32767..32767, instead of -32768..32767? Does that work?
I know that the full range of a 16 bit, two's complement integer is indeed -32768..32767, but Pascal does not assume two's complement implementation, hence the range for an integer is -maxint..maxint, not -(maxint+1)..maxint. Therefore, GPC may at least in some cases regard the need for -32768 as an integer type whose maxint is 32768 or greater, in which case 16 bits would be deemed inadequate (with 32 bits chosen instead).
I don't think that's the case.
If the above analysis is true, then GPC has two bugs :
- It is inconsistent about whether -32768..32767 fits within 16 bits.
- It is allowing the "external" qualifier to circumvent checking for duplicate identifiers (as Maurice has already pointed out).
These are inconsistencies, though I'm not sure if you can call them bugs (I guess some will even call #2 a feature, though I'd call it a misfeature at best). After all, `external' declarations are non-standard and nowhere defined exactly (AFAIK), and also the size and layout of types is implementation dependent. A program which does such things is basically relying on undocumented behaviour.
Now, I don't think the current behaviour is very good, but I consider it a very low priority issue compared to the real bugs we have. (In particular, since `external' is meant for interfacing with C and other languages, rather than for side-stepping the module mechanism, and C doesn't have a type declaration like `packed foo .. bar', problem #1 is almost a non-issue IMHO.)
As for the duplicate declarations with `external', I'd like to disallow them in fact. I'm not sure, but the fact that GPC allows them currently may be a relic from ancient times when GPC didn't support modules/units, and this "C in Pascal programming" with header files was used as a work-around (that was before I first used GPC, so I don't know for sure; another explanation is that it's another feature that was inherited from the C compiler, and should have been disabled long time ago). I won't disable it now, so shortly before the 2.1 release, but I hope I can do it immediately afterwards (i.e., I hope there won't be protests from some of you who have millions of LOC which rely on this (mis)feature and cannot be changed for hysterical raisins etc.) ...
Actually, it seems that problem #1 is related to this. I haven't investigated it in detail, but I see it happen only when such duplicate declarations are involved. So, unless I've missed some other situations where it happens as well, the problem will simply disappear when we forbid those, so it's not worth the effort to try to fix it now I think.
BTW, Martin, is there a particular reason why you write `packed -32768..32767' rather than `Cardinal (16)' or `ShortCard'? Both alternatives don't exhibit this problem AFAICS, and at least the last one is less non-standard (i.e., no syntax extension, just another predefined identifier; though the size is not guaranteed, i.e., it might be bigger than 16 bits on some platforms).
Frank